c# – BeginInvoke的性能影响
我继承了从主线程调用BeginInvoke的代码(不是后台线程,通常是模式).我试图了解它在这种情况下的实际作用.
在BeginInvoke中调用的方法是否符合到窗口的消息?文档说是异步的,所以这是我的假设. 框架如何确定何时启动BeginInvoke调用的方法? 编辑:代码如下所示: System.Action<bool> finalizeUI = delegate(bool open) { try { // do somewhat time consuming stuff } finally { Cursor.Current = Cursors.Default; } }; Cursor.Current = Cursors.WaitCursor; BeginInvoke(finalizeUI,true); 这发生在Form_Load事件中. 解决方法
编辑
现在我们看到了代码,很明显这只是一种将一些初始化移出Form_Load的方法,但在用户可以与表单交互之前仍然会发生这种情况. 对BeginInvoke的调用在Form_load中,并且不在另一个对象上调用,因此这是对Form.BeginInvoke的调用.所以这是怎么回事. > Form_Load将委托传递给Form.BeginInvoke,这会在表单的消息队列中放置一条消息,该消息排在所有用户输入消息之前.它将光标设置为等待光标. 原帖在下面 我依赖于你调用BeginInvoke的对象.如果对象派生自Control,则Control.BeginInvoke将在创建控件的线程上运行.见JaredPar的回答. 但是使用BeginInvoke还有另一种模式.如果对象是委托,则BeginInvoke在单独的线程上运行回调,该线程可以专门为此目的创建. public class Foo { ... public Object Bar(object arg) { // this function will run on a separate thread. } } ... // this delegate is used to Invoke Bar on Foo in separate thread,this must // take the same arguments and return the same value as the Bar method of Foo public delegate object FooBarCaller (object arg); ... // call this on the main thread to invoke Foo.Bar on a background thread // public IAsyncResult BeginFooBar(AsyncCallback callback,object arg) { Foo foo = new Foo(); FooBarCaller caller = new FooBarCaller (foo.Bar); return caller.BeginInvoke (arg); } 此模式是从主线程而不是从后台线程调用BeginInvoke的一个原因. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |