本文介绍了带委托的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!

推荐答案


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

09-13 00:26