views:

99

answers:

2

Hello there,

I'm not sure if this is good programming etiquette, anyway I have a normal method in which I update certain bits of data and UI elements such as textblocks etc.

Anyway, I want to create a thread that every X amount of Seconds it runs the Update Method but I cannot access it because from what I understand a thread can only run static methods.

What is the best way around this?

Thanks,

mg.

+3  A: 

from what I understand a thread can only run static methods.

This is simply not true. You can start a thread like this:

Thread thread = new Thread(() => { foo.bar(123, 456); });
thread.Start();

The method bar does not have to be static, but you do need to have a reference to an object to be able to call an instance method.

If you have a parameterless method you can also do this:

Thread thread = new Thread(bar);

You should note that you cannot modify the GUI from another thread than the main thread, so if all you want to do is update the GUI you shouldn't start a new thread. If you have a long running process and want to update the GUI occasionally to show progress without blocking the UI you can look at BackgroundWorker.

Alternatively you can update the GUI from a background thread using the Invoke pattern:

private void updateFoo()
{
    if (InvokeRequired)
    {
        Invoke(new MethodInvoker(() => { updateFoo(); }));
    }
    else
    {
        // Do the update.
    }
}

See this related question: C#: Automating the InvokeRequired code pattern

Mark Byers
Why the anonymous method? It's not nessacary: new Thread(foo).Start();
jgauffin
@jfgauffin: That's a special case that might not apply in the OP's situation. My answer can be used more generally. But you're right in some situations your approach would also work. I'll add it to my answer.
Mark Byers
Thanks for the response Mark but if I go;public MainPage() { InitializeComponent(); new Thread(() => { myTest(); }).Start(); }void myTest() { myTextBlock.Text = "Tester."; }I still get an UnauthorizedAccessException?
monkeyguy
@monkeyguy: Mixing threads and Windows Forms is a bit more tricky. You might want to look at a BackgroundWorker, or else call Invoke as appropriate.
Mark Byers
@monkeyguy: Also worth noting is that you can't actually update the GUI from another thread. Threads should generally be used to perform some other task, then when that task is finished you use Invoke to give control back to the main thread to update the GUI.
Mark Byers
@monkeyguy That's because you cannot call UI methods from another thread without wrecking havoc. You have to somehow make your GUI code run in the GUI thread. You do that by calling Invoke(..) or BeginInvoke on one of your GUI controls, and give it a method/delegate to ruun.
nos
Ok, thanks a lot for the help guys.
monkeyguy
A: 

If you want to update UI elements based on the progress of the thread, you should probably look into the BackgroundWorker class (http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx)

Jonathan