views:

226

answers:

2

[This appears to be a loooong question but I have tried to make it as clear as possible. Please have patience and help me...]

I have written a test class which supports an Async operation. That operation does nothing but reports 4 numbers:

class AsyncDemoUsingAsyncOperations
{
    AsyncOperation asyncOp;
    bool isBusy;

    void NotifyStarted () {
        isBusy = true;
        Started (this, new EventArgs ());
    }

    void NotifyStopped () {
        isBusy = false;
        Stopped (this, new EventArgs ());
    }

    public void Start () {
        if (isBusy)
            throw new InvalidOperationException ("Already working you moron...");

        asyncOp = AsyncOperationManager.CreateOperation (null);
        ThreadPool.QueueUserWorkItem (new WaitCallback (StartOperation));
    }

    public event EventHandler Started = delegate { };
    public event EventHandler Stopped = delegate { };
    public event EventHandler<NewNumberEventArgs> NewNumber = delegate { };

    private void StartOperation (object state) {
        asyncOp.Post (args => NotifyStarted (), null);

        for (int i = 1; i < 5; i++)
            asyncOp.Post (args => NewNumber (this, args as NewNumberEventArgs), new NewNumberEventArgs (i));

        asyncOp.Post (args => NotifyStopped (), null);
    }
}

class NewNumberEventArgs: EventArgs
{
    public int Num { get; private set; }

    public NewNumberEventArgs (int num) {
        Num = num;
    }
}

Then I wrote 2 test programs; one as console app and another as windows form app. Windows form app works as expected when I call Start repeatedly:

alt text

But console app has hard time ensuring the order:

alt text

Since I am working on class library, I have to ensure that my library works correctly in all app models (Haven't tested in ASP.NET app yet). So I have following questions:

  1. I have tested enough times and it appears to be working but is it OK to assume above code will always work in windows form app?
  2. Whats the reason it (order) doesn't work correctly in console app? How can I fix it?
  3. Not much experienced with ASP.NET. Will the order work in ASP.NET app?

[EDIT: Test stubs can be seen here if that helps.]

+1  A: 

Unless I am missing something then given the code above I believe there is no way of guaranteeing the order of execution. I have never used the AsyncOperation and AsyncOperationManager class but I looked in reflector and as could be assumed AsyncOperation.Post uses the thread pool to execute the given code asynchronously.

This means that in the example you have provided 4 tasks will be queued to the thread pool synchronously in very quick succession. The thread pool will then dequeue the tasks in FIFO order (first in first out) but it's entirely possible for one of later threads to be scheduled before an earlier one or one of the later threads to complete before an earlier thread has completed its work.

Therefore given what you have there is no way to control the order in the way you desire. There are ways to do this, a good place to look is this article on MSDN.

http://msdn.microsoft.com/en-us/magazine/dd419664.aspx

Ian Gibson
A: 

Hi,

I use a Queue you can then Enqueue stuff and Dequeue stuff in the correct order. This solved this problem for me.

Neil