locked
How do I make a program work in the background? RRS feed

  • Question

  • Hello,

    Up to now, Ive worked out how to apply the notifyicon component however, I dont know how to get my program to work in the background.

    My program listens for a particular combination of 'keydowns', the whole program works great but I want it to work when another window has the focus.

    I realise that somehow theres got to be various threads involved. What is the easiest way of achieving this though?

    Thanks, Ant...
    Wednesday, January 30, 2008 7:14 PM

Answers

  • Code Snippet

    my_function();



    is the function that will run on a seperate thread - so, just replace my_function with your function you would like to run on a sepearate thread.  You can also add other functions, etc...  This is a basic example but should get you started on threading...

    Thanks,
    Andrew
    Thursday, January 31, 2008 5:08 AM

All replies

  • Hi Antony,

    I use background workers, listed under toolbox - components.  Once you add the object to your form, you can use this method to start your thread:

    Code Snippet

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {

    if (this.InvokeRequired)
    {
    SetCallback d1 = new SetCallback(my_function);
    this.Invoke(d1);

    }
    else
    {
    this.my_function();
    }

    }



    and to report progress, you use these methods:

    Code Snippet

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {

    }


    Code Snippet

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {

    }



    Remember to create the delegate function:

    Code Snippet

    delegate void SetCallback();


    Hope this helps...

    ~Andrew
    Wednesday, January 30, 2008 7:32 PM
  • Hmm, I dont understand it!
    How does it work?

    Thanks, Ant...
    Wednesday, January 30, 2008 8:56 PM
  • Code Snippet

    my_function();



    is the function that will run on a seperate thread - so, just replace my_function with your function you would like to run on a sepearate thread.  You can also add other functions, etc...  This is a basic example but should get you started on threading...

    Thanks,
    Andrew
    Thursday, January 31, 2008 5:08 AM