Callbacks on GUI Thread

Posted by miguel on Stack Overflow See other posts from Stack Overflow or by miguel
Published on 2010-05-08T10:17:36Z Indexed on 2010/05/08 10:28 UTC
Read the original article Hit count: 211

Filed under:
|
|

We have an external data provider which, in its construtor, takes a callback thread for returning data upon.

There are some issues in the system which I am suspicious are related to threading, however, in theory they cannot be, due to the fact that the callbacks should all be returned on the same thread.

My question is, does code like this require thread synchronisation?

class Foo
{
  ExternalDataProvider _provider;

  public Foo()
  {
    // This is the c'tor for the xternal data provider, taking a callback loop as param
    _provider = new ExternalDataProvider(UILoop);
    _provider.DataArrived += ExternalProviderCallbackMethod;

  }
  public ExternalProviderCallbackMethod()
  {
    var itemArray[] = new String[4] { "item1", "item2", "item3", "item4" };
    for (int i = 0; i < itemArray.Length; i++)
    {
       string s = itemArray[i];
       switch(s)
       {
          case "item1":
               DoItem1Action();
               break;
          case "item2":
               DoItem2Action();
               break; 
          default:
               DoDefaultAction();
               break;              
       }

    }
  }
}

The issue is that, very infrequently, DoItem2Action is executingwhen DoItem1Action should be exectuing.

Is it at all possible threading is at fault here? In theory, as all callbacks are arriving on the same thread, they should be serialized, right? So there should be no need for thread sync here?

© Stack Overflow or respective owner

Related posts about c#

Related posts about threading