tags:

views:

165

answers:

2

Hi guys,

I use the system.timers.timer for my service.

Now I build a test-form in which I use it too. In the timer_Elapsed Event I do some work and want to stop the timer it need (xxx ms) and write it on the form control to show.

But when I access the listview, I get an cross thread error.

Any ideas?

+1  A: 

If you want to acces a control from a thread other than the main UI thread, you need to use the Invoke method on the control you want to access.

Konamiman
Thank you, but I never work with this before, can you give an example / a tutorial for this?
Kovu
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired.aspxThere's a code example at the bottom.
David
+1  A: 

Your method should look like:

public void foo(int value, string message)
    {
        if (InvokeRequired)
        {
            BeginInvoke(new Action<int, string>(foo), value, message);
        }
        else
        {
            // Stop the timer 
        }
    }
David