简体   繁体   中英

Delegate.BeginInvoke Callback Blocking Calling Thread?

I'm not really sure where to look with this problem as I'm not particularly familiar with asynchronous programming. I have a loop which invokes a delegate's BeginInvoke method. When the delegate's callback is executed the loop ceases to execute (it shouldn't). I'm guessing that somehow the thread it's running on is being blocked but I really don't know for sure. Here's a simplified version of the code:

public class TestClass
{
    private readonly IService service;
    private delegate void TestDelegate();
    private bool conditionIsMet = true;

    public TestClass( IService service )
    {
        this.service = service;
    }

    public void PerformTask()
    {
        while ( conditionIsMet )
        {
            var testDelegate = new TestDelegate( service.DoSomething );
            testDelegate.BeginInvoke( TestCallback, null );

            Thread.Sleep( 1 );
        }
    }

    private void TestCallback( IAsyncResult result )
    {
        var asyncResult = ( AsyncResult ) result;
        var testDelegate = ( TestDelegate ) asyncResult.AsyncDelegate;
        testDelegate.EndInvoke( asyncResult );

        // After exiting this method the loop in PerformTask() ceases to execute.
        // Is it being blocked here somehow?
    }
}

In practice there is a bit more to the code but the essential components involved are all here so far as I can tell. In the code sample above I've put a comment in there to indicate the last place the code executes (in the VS debugger, anyway).

I assume that I'm making some sort of fundamental error in the way I'm doing the delegate async invocation but I can't find any docs that explain it to me. Any idea why this is happening?

UPDATE

As part of further testing, I tried this without the EndInvoke call (I know, bad idea in practice) but there was no change in behaviour - it still failed to continue executing the loop.

It works ok for me I think. Are you running it in a console application?

You would need to stop that exiting.

class Program
{
    static void Main(string[] args)
    {
        TestClass t = new TestClass(new Service());
        t.PerformTask();

        Console.ReadKey();
    }
}

public class Service : IService
{
    public void DoSomething()
    {
        Console.WriteLine("Doing something");
    }
}

public class TestClass
{
    private readonly IService service;
    private delegate void TestDelegate();
    private bool conditionIsMet = true;

    public TestClass(IService service)
    {
        this.service = service;
    }

    public void PerformTask()
    {
        while (conditionIsMet)
        {
            var testDelegate = new TestDelegate(service.DoSomething);
            testDelegate.BeginInvoke(TestCallback, null);

            Thread.Sleep(1);
        }
    }

    private void TestCallback(IAsyncResult result)
    {
        var asyncResult = (AsyncResult)result;
        var testDelegate = (TestDelegate)asyncResult.AsyncDelegate;
        testDelegate.EndInvoke(asyncResult);

        // After exiting this method the loop in PerformTask() ceases to execute.
        // Is it being blocked here somehow?
    }
}

public interface IService
{
    void DoSomething();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM