简体   繁体   English

Delegate.BeginInvoke回调阻止调用线程?

[英]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. 我有一个循环,它调用委托的BeginInvoke方法。 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). 在上面的代码示例中,我在其中添加了注释,以指示代码执行的最后位置(无论如何,在VS调试器中)。

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. 作为进一步测试的一部分,我在没有EndInvoke调用的情况下尝试了此操作(我知道,实际上是个坏主意),但是行为没有改变-它仍然无法继续执行循环。

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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