本文介绍了使用委托进行多线程处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 在我的Win应用程序中,我正在使用代理,因为正在运行的进程耗时耗时且需要参数。我唯一的问题是 ,正在执行BeginInvoke之后的行(取决于结果的行 ),而漫长的过程是执行 。是否有可能告诉那些代码行不是 在BeginInvoke方法运行时执行并在 BeginInvoke完成执行后运行? loadAuditBrowseHandler thisLoadAuditBrowseHandler = new loadAuditBrowseHandler(this.Browse); thisLoadAuditBrowseHandler.BeginInvoke(null,null); / /在BeginInvoke运行时无法执行的更多行代码 $ div $ = h2_lin>解决方案 如果在委托完成之前不能执行这些代码行,为什么你首先异步调用它? 如果这是在UI线程中,你不应该在同一个方法中使用 代码的第二部分 - 而是提供一个回调委托 ,并从该委托调用 Control.Invoke或Control.BeginInvoke toget return到UI线程 并处理结果。 - Jon Skeet - < sk *** @ pobox.com> http://www.pobox.com/~skeet 如果回复群组,请不要给我发邮件 如果在委托完成之前不能执行这些代码行,那么为什么要首先异步调用它? 如果这是在UI线程中,你不应该在同一个方法中使用第二部分代码 - 而是提供一个回调委托,当委托完成时调用它,并从该委托调用 Control.Invoke或Control.BeginInvoke toget back到UI线程并处理结果。 - Jon Skeet - < sk *** @ pobox.com> http://www.pobox.com/~skeet 如果回复群组,请不要给我发邮件 评论中提到的所有位都需要使用另一种方法 - 回拨代表。 - Jon Skeet - < sk *** @ pobox.com> http://www.pobox.com/~skeet 如果回复小组,请不要给我发邮件 Hi,In my Win app, I''m using delegates because the process that''ll be running istime-consuming and it requires parameters. The only problem I''m having isthat the lines following the BeginInvoke (lines that depend on the outcomeof the method) are being executed while the lengthy process is beingexecuted. Would it be possible to "tell" those following lines of code notto execute while the BeginInvoke method is running and run after theBeginInvoke has finished executing ?loadAuditBrowseHandler thisLoadAuditBrowseHandler = newloadAuditBrowseHandler(this.Browse);thisLoadAuditBrowseHandler.BeginInvoke(null,null);// many more lines of code that cannot be executed while the BeginInvoke isrunning 解决方案If those lines of code can''t be executed until the delegate hasfinished, why are you invoking it asynchronously in the first place?If this is in the UI thread, you shouldn''t have the second portion ofcode in the same method - instead, provide a callback delegate which iscalled when the delegate has finished, and from that delegate callControl.Invoke or Control.BeginInvoke to "get back" to the UI threadand deal with the results.--Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeetIf replying to the group, please do not mail me too If those lines of code can''t be executed until the delegate has finished, why are you invoking it asynchronously in the first place? If this is in the UI thread, you shouldn''t have the second portion of code in the same method - instead, provide a callback delegate which is called when the delegate has finished, and from that delegate call Control.Invoke or Control.BeginInvoke to "get back" to the UI thread and deal with the results. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me tooAll the bit referred to in the comment needs to be in another method -the one called by the callback delegate.--Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeetIf replying to the group, please do not mail me too 这篇关于使用委托进行多线程处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-03 20:23
查看更多