带委托的BeginInvoke [英] BeginInvoke with delegate

查看:71
本文介绍了带委托的BeginInvoke的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今晚一直在阅读有关异步委托方法调用的一些内容,而且我不太清楚使用delegate.BeginInvoke()在幕后发生了什么,但我想知道是否有人可以告诉如果使用AsyncCallback并且不使用AsyncCallback这两个代码块之间存在任何性能差异,并考虑到我正在集中更新GUI winForm(每秒多次):



I''ve been doing some reading tonight on async delegate method invocations and I''m not too clear on what''s going on under the hood with delegate.BeginInvoke() but I wanted to know if someone can tell me if there are any performance differences between these two code blocks as far as using an AsyncCallback and not using an AsyncCallback and take into consideration that I''m updating the GUI winForm intensively (multiple times per sec):

public delegate string ProcDataDel(string data);

public ProcDataDel procData = new ProcDataDel(ProcDataMethod);
public AsyncCallBack procDataDone = new AsyncCallBack(ProcDataDoneMethod);

public string ProcDataMethod(string data)
{
   return data + " has been processed...";
}

public void ProcDataDoneMethod(IAsyncResult result)
{
   ProcDataDel _procData = (ProcDataDel)((AsyncResult)result).AsyncDeleate;
   string data = _proData.EndInvoke(result);
   
   if (listView1.InvokeRequired)
   {
      listView1.Invoke(new MethodInvoker(delegate() {listView1.Add(data);}));
   }
   else
      listView1.Add(data);
}

public void Button1_ClickEvent(object sender, EventArgs e)
{
   procData.BeginInvoke("some data",procDataDoneMethod,procDataMethod);
}





没有AsyncCallback:



Without AsyncCallback:

public delegate void ProcDataDel(string data);

public void ProcDataFunc(string data)
{
   if (listView1.InvokeRequired)
   {
       listView1.Invoke(new MethodInvoker(delegate() {listView1.Add(data);}));
   }
   else
      listView1.Add(data);
}

public void Button1_ClickEvent(object sender, EventArgs e)
{
   listView1.BeginInvoke(new ProcDataDel(ProcDataFunc),new object[]{"some data has been processed..."});
}





我正在使用生产项目中的底部代码我正在努力,但我''因为我的GUI正在更新时遇到性能问题,所以经常看起来没有跟上并且有点反应迟钝......我想知道使用AsyncCallback的代码到目前为止是否有任何不同。



任何反馈都将不胜感激!在此先感谢并祝新年快乐!



I''m using the bottom code in a production project I''m working on but I''m having performance issues with my GUI being updated so often seems not to be keeping up and a little unresponsive...I was wondering if the code using the AsyncCallback was any different as far a performance goes.

Any feedback would be appreciated! Thanks in advance and HAPPY NEW YEAR!

推荐答案

我试着用我过去的答案解释一下:



Control.Invoke()vs. Control.BeginInvoke() [ ^ ],

Treeview扫描仪和MD5的问题 [ ^ ]。



BeginInvoke Invoke 是这样的:首先立即返回,在委托实例进入UI线程的队列之前,调用并返回结果。因此,结果以延迟的方式提供。



此外,如果您阅读我实现调用机制的小文章,您将有机会深入了解内幕用于自定义(UI除外)线程。它包含源代码,说明和用法示例:

用于线程通信和线程间调用的简单阻塞队列 [ ^ ]。



新年快乐!



-SA
I tried to explain it in my past answers:

Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

There difference between BeginInvoke and Invoke is this: first returns immediately, before delegate instance goes in the queue of the UI thread, got invoked and returns the result. So, the result is delivered in a deferred manner.

Also, you get a good chance to look under the hood if you read my small article where I implement invocation mechanism for a custom (other than UI) threads. It is complete with source code, explanation and usage samples:
Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

Happy New Year!

—SA


这篇关于带委托的BeginInvoke的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆