C# 异步调用没有 EndInvoke?

发布于 2024-12-06 01:44:56 字数 526 浏览 0 评论 0原文

以下面的类为例。

public class A
{
   // ...
   void Foo(S myStruct){...}
}

public class B
{
   public A test;
   // ...
   void Bar()
   {
      S myStruct = new S();
      test.Foo(myStruct);
   }
}

现在,我希望方法调用 test.Foo(myStruct) 成为异步调用(“即发即忘”)。 bar-method 需要尽快返回。有关委托、BeginInvoke、EndInvoke、ThreadPool 等的文档并不能帮助我找到解决方案。

这是一个有效的解决方案吗?

     // Is using the `EndInvoke` method as the callback delegate valid?
     foo.BeginInvoke(myStruct, foo.EndInvoke, null);

Take the following classes as an example.

public class A
{
   // ...
   void Foo(S myStruct){...}
}

public class B
{
   public A test;
   // ...
   void Bar()
   {
      S myStruct = new S();
      test.Foo(myStruct);
   }
}

Now, I want the method-call test.Foo(myStruct) to be an asynchronous call ('fire-and-forget'). The bar-method needs to return as soon as possible. Documentation around delegates, BeginInvoke, EndInvoke, the ThreadPool etc. isn't helping me find a solution.

Is this a valid solution?

     // Is using the `EndInvoke` method as the callback delegate valid?
     foo.BeginInvoke(myStruct, foo.EndInvoke, null);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空‖城人不在 2024-12-13 01:44:57

您可以使用@什么是AsyncCallback?解释的回调模型

,这样您的EndInvoke就不会位于 bar() 中,但位于单独的回调方法中。

例子中的EndRead(对应EndInvoke是在回调方法中调用CompleteRead而不是bar对应的调用方法TestCallbackAPM)

You can use the Callback model explained @ What is AsyncCallback?

That way your EndInvoke will not be in bar(), but in a separate callback method.

In the example, the EndRead (corresponding to EndInvoke is in the callback method called CompleteRead rather than the calling method TestCallbackAPM corresponding to bar)

请帮我爱他 2024-12-13 01:44:57

这是一个选项:

ThreadPool.QueueUserWorkItem(bcl =>
{
    var bcList = (List<BarcodeColumn>)bcl;
    IAsyncResult iftAR = this.dataGridView1.BeginInvoke((MethodInvoker)delegate
    {
        int x = this.dataGridView1.Rows[0].Cells.Count - 1;
        for (int i = 0; i < this.dataGridView1.Rows.Count - 1; i++)
        {
            try
            {
                string imgPath = bcList[i].GifPath;
                Image bmpImage = Image.FromFile(imgPath);
                this.dataGridView1.Rows[i].Cells[x].Value =bmpImage;
            }
            catch (Exception)
            {
                continue;
            }
        }
    }); 
    while (!iftAR.IsCompleted) { /* wait this*/  }
}, barcodeList);

This is an option:

ThreadPool.QueueUserWorkItem(bcl =>
{
    var bcList = (List<BarcodeColumn>)bcl;
    IAsyncResult iftAR = this.dataGridView1.BeginInvoke((MethodInvoker)delegate
    {
        int x = this.dataGridView1.Rows[0].Cells.Count - 1;
        for (int i = 0; i < this.dataGridView1.Rows.Count - 1; i++)
        {
            try
            {
                string imgPath = bcList[i].GifPath;
                Image bmpImage = Image.FromFile(imgPath);
                this.dataGridView1.Rows[i].Cells[x].Value =bmpImage;
            }
            catch (Exception)
            {
                continue;
            }
        }
    }); 
    while (!iftAR.IsCompleted) { /* wait this*/  }
}, barcodeList);
倒数 2024-12-13 01:44:56

您不需要调用 EndInvoke;不调用它仅仅意味着:

  • 您没有从该方法获得返回值。
  • 方法执行期间抛出的任何异常都会消失。

听起来您想“一劳永逸”,因此最简单的方法是使用匿名委托,例如:

var del = new Action(foo.Bar);
del.BeginInvoke(iar =>
{
   try
   {
      del.EndInvoke(iar);
   }
   catch (Exception ex)
   {
      // Log the message?
   }
}, null);

执行此代码时会发生以下情况:

  1. 分配一个新线程(简单地说)为代表。
  2. 该线程被赋予委托 del 和匿名委托 (iar => ...)。
  3. 线程执行del
  4. 当执行完成(或发生异常)时,结果或异常将被存储,并执行匿名委托。
  5. 在匿名委托内部,当调用 EndInvoke 时,要么返回该方法的结果,要么引发异常(如果发生)。

请注意,上面的示例与:

// This is pointless and is still, essentially, synchronous.
del.EndInvoke(del.BeginInvoke(null, null));

编辑:您应该始终调用End*。我从未发现不调用它会出现问题的场景,但这是一个实现细节,并且是 依赖于未记录的行为。

最后,如果抛出异常,您的解决方案将使进程崩溃,如果您不关心异常,您可以简单地将 null 作为委托传递(del.BeginInvoke(myStruct, null, null);). 因此,作为最后一个示例,您正在寻找的可能是:

public class A
{
    // ...
    void Foo(S myStruct){...}
    void FooAsync(S myStruct)
    {
        var del = new Action<S>(Foo);
        del.BeginInvoke(myStruct, SuppressException, del);
    }

    static void SuppressException(IAsyncResult ar)
    {
        try
        {
            ((Action<S>)ar.AsyncState).EndInvoke(ar);
        }
        catch
        {
            // TODO: Log
        }
    }
}

You are not required to call EndInvoke; not calling it merely means:

  • You don't get the return value from the method.
  • Any exceptions thrown during the method execution will simply disappear.

It sounds like you want to 'fire-and-forget', so the easiest way to do this is to use an anonymous delegate, for example:

var del = new Action(foo.Bar);
del.BeginInvoke(iar =>
{
   try
   {
      del.EndInvoke(iar);
   }
   catch (Exception ex)
   {
      // Log the message?
   }
}, null);

This is what happens when you execute this code:

  1. A new thread is allocated (put simply) for the delegate.
  2. The thread is given the delegate del and the anonymous delegate (iar => ...).
  3. The thread executes del.
  4. When it is finished executing (or an exception occurs) the result or exception is stored and the anonymous delegate is executed.
  5. Inside the anonymous delegate, when EndInvoke is called the result from the method is either returned, or the exception is thrown (if one occurred).

Note that the above example is very different from:

// This is pointless and is still, essentially, synchronous.
del.EndInvoke(del.BeginInvoke(null, null));

Edit: You should always call End*. I've never found a scenario where not calling it presents a problem, however that is an implementation detail and is relying on undocumented behavior.

Finally your solution would crash the process if an exception is thrown, you can simply pass null as the delegate if you don't care about the exception (del.BeginInvoke(myStruct, null, null);). So as a final example what you are looking for is probably:

public class A
{
    // ...
    void Foo(S myStruct){...}
    void FooAsync(S myStruct)
    {
        var del = new Action<S>(Foo);
        del.BeginInvoke(myStruct, SuppressException, del);
    }

    static void SuppressException(IAsyncResult ar)
    {
        try
        {
            ((Action<S>)ar.AsyncState).EndInvoke(ar);
        }
        catch
        {
            // TODO: Log
        }
    }
}
你在看孤独的风景 2024-12-13 01:44:56

我想说,最好的选择是使用 ThreadPool:

void bar()
{
    ThreadPool.QueueUserWorkItem(o=>
    {
        S myStruct = new S();
        test.foo(myStruct);
    });
}

这会将片段排队以在单独的线程中执行。现在您还必须小心其他事情:如果您有多个线程访问 A 的同一实例,并且该实例修改了变量,那么您必须确保对该变量进行正确的同步。

public class A
{
    private double sum;
    private volatile bool running;
    private readonly object sync;
    public A()
    {
        sum = 0.0;
        running = true;
        sync = new object();
    }

    public void foo(S myStruct)
    {
        // You need to synchronize the whole block because you can get a race
        // condition (i.e. running can be set to false after you've checked
        // the flag and then you would be adding the sum when you're not 
        // supposed to be).
        lock(sync)
        {
            if(running)
            {
                sum+=myStruct.Value;
            }
        }
    }

    public void stop()
    {
        // you don't need to synchronize here since the flag is volatile
        running = false;
    }
}

I would say that your best option is to use the ThreadPool:

void bar()
{
    ThreadPool.QueueUserWorkItem(o=>
    {
        S myStruct = new S();
        test.foo(myStruct);
    });
}

This will queue the snippet for execution in a separate thread. Now you also have to be careful about something else: if you have multiple threads accessing the same instance of A and that instance modifies a variable, then you must ensure that you do proper synchronization of the variable.

public class A
{
    private double sum;
    private volatile bool running;
    private readonly object sync;
    public A()
    {
        sum = 0.0;
        running = true;
        sync = new object();
    }

    public void foo(S myStruct)
    {
        // You need to synchronize the whole block because you can get a race
        // condition (i.e. running can be set to false after you've checked
        // the flag and then you would be adding the sum when you're not 
        // supposed to be).
        lock(sync)
        {
            if(running)
            {
                sum+=myStruct.Value;
            }
        }
    }

    public void stop()
    {
        // you don't need to synchronize here since the flag is volatile
        running = false;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文