views:

501

answers:

2

I have a background thread running that fires events, but how can I ensure a thread safe invocation of these events using NET CF?

I would use ISyncronizeInvoke on the NET platform, but I can't find this on NET CF. I'm sure there are an equivalent available.... or?

+3  A: 

The Compact Framework does have Control.Invoke/BeginInvoke, although I believe it's limited to the EventHandler delegate (with any other delegate throwing an exception at execution time).

Assuming your actual instance of ISynchronizeInvoke is going to be a UI control, I'd just pass the reference as Control to whatever needs it. If you really want to use an interface, you could always create your own ISynchronizeInvoke interface, and then an implementation which just wraps Control.

Jon Skeet
Correct, the CF does have Control.Invoke/BeginInvoke/InvokeRequired. But the are introduced in class Control, no Interface.
Henk Holterman
Thanks, will edit to make this more assertive :)
Jon Skeet
+1  A: 

Yes it is available in .NET CF, here is an extract from such a project:
SampleMethod() is called from another thread.

delegate void SimpleInvokeDelegate();
private void SampleMethod()
{
    if (InvokeRequired)
    {
        Invoke(new SimpleInvokeDelegate(SampleMethod));
    }
    else
    {
       // Update UI elements here.
    }
}
Philip Fourie