使用委托进行多线程处理 [英] using delegates for multithreading

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

问题描述



在我的Win应用程序中,我正在使用代理,因为正在运行的进程耗时耗时且需要参数。我唯一的问题是

,正在执行BeginInvoke之后的行(取决于结果的行

),而漫长的过程是执行

。是否有可能告诉那些代码行不是

在BeginInvoke方法运行时执行并在

BeginInvoke完成执行后运行?


loadAuditBrowseHandler thisLoadAuditBrowseHandler = new

loadAuditBrowseHandler(this.Browse);

thisLoadAuditBrowseHandler.BeginInvoke(null,null);


/ /在BeginInvoke运行时无法执行的更多行代码
$ div $ = h2_lin>解决方案

VM< vo ** ****@yahoo.com>写道:

在我的Win应用程序中,我正在使用代理,因为将要运行的进程非常耗时且需要参数。我遇到的唯一问题是,当执行漫长的过程时,正在执行BeginInvoke之后的行(依赖于方法结果的行)。是否有可能告诉在BeginInvoke方法运行并且在BeginInvoke执行完毕后运行后执行以下代码行的那些代码?





loadAuditBrowseHandler thisLoadAuditBrowseHandler = new
loadAuditBrowseHandler(this 。浏览);
thisLoadAuditBrowseHandler.BeginInvoke(null,null);

//在BeginInvoke运行时无法执行的更多行代码




如果在委托完成
之前不能执行这些代码行,为什么你首先异步调用它?


如果这是在UI线程中,你不应该在同一个方法中使用

代码的第二部分 - 而是提供一个回调委托<当委托完成时调用br />
,并从该委托调用

Control.Invoke或Control.BeginInvoke toget return到UI线程

并处理结果。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复群组,请不要给我发邮件


因为它是一个MDI应用程序,用户可以在运行时使用任何其他单独的

窗口。这是在UI线程中运行的。


你会如何创建一个回调委托,让我回到主用户界面

线程?

这是我窗口的基本结构:


私有委托void loadAuditBrowseHandler();

private void btn_filter_Click(object sender,System .EventArgs e)

{

loadAuditBrowseHandler thisLoadAuditBrowseHandler = new

loadAuditBrowseHandler(this.Browse);

thisLoadAuditBrowseHandler。 BeginInvoke(null,null);

//过滤数据表中的数据并将表附加到网格的代码.b
网格。其他misc也是。东西

}


private void浏览()

{

//代码填充a私有成员数据表有几百行 -

可能需要几秒钟

/ *在此过程中,我不希望应用程序看起来像它/

冻结* /

}


再次感谢。

" Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...

VM< vo ****** @ yahoo.com>写道:

在我的Win应用程序中,我正在使用代理,因为运行'
的过程非常耗时且需要参数。我有
的唯一问题是,在执行漫长的过程时,正在执行BeginInvoke之后的行(依赖于方法的
结果的行)。是否有可能告诉那些以下代码行
在BeginInvoke方法运行时不执行并在
BeginInvoke完成执行后运行?

loadAuditBrowseHandler thisLoadAuditBrowseHandler = new
loadAuditBrowseHandler(this 。浏览);
thisLoadAuditBrowseHandler.BeginInvoke(null,null);

//在BeginInvoke
运行时无法执行的更多代码行



如果在委托完成之前不能执行这些代码行,那么为什么要首先异步调用它?

如果这是在UI线程中,你不应该在同一个方法中使用第二部分代码 - 而是提供一个回调委托,当委托完成时调用它,并从该委托调用< br。> Control.Invoke或Control.BeginInvoke toget back到UI线程
并处理结果。

- Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复群组,请不要给我发邮件



VM< vo ****** @ yahoo.com>写道:

由于它是一个MDI应用程序,用户可以在运行时使用任何其他单独的窗口。这是在UI线程中运行的。

如何创建一个回调委托,让我回到主UI?
线程?


只需使用与AsyncCallback具有相同签名的东西,在传递给你的IAsyncResult上调用

EndInvoke,然后调用

在另一个委托上调用或BeginInvoke来做任何你需要做的事情

在UI中。

这是我窗口的基本结构:
private void btn_filter_Click(object sender,System.EventArgs e)
{/ / loadAuditBrowseHandler thisLoadAuditBrowseHandler = new
loadAuditBrowseHandler(this.Browse);
thisLoadAuditBrowseHandler.BeginInvoke(null,null);
//过滤数据表中数据并将表附加到网格的代码。其他misc也是。东西
}




评论中提到的所有位都需要使用另一种方法 -

回拨代表。


-

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 is
time-consuming and it requires parameters. The only problem I''m having is
that the lines following the BeginInvoke (lines that depend on the outcome
of the method) are being executed while the lengthy process is being
executed. Would it be possible to "tell" those following lines of code not
to execute while the BeginInvoke method is running and run after the
BeginInvoke has finished executing ?

loadAuditBrowseHandler thisLoadAuditBrowseHandler = new
loadAuditBrowseHandler(this.Browse);
thisLoadAuditBrowseHandler.BeginInvoke(null,null);

// many more lines of code that cannot be executed while the BeginInvoke is
running

解决方案

VM <vo******@yahoo.com> wrote:

In my Win app, I''m using delegates because the process that''ll be running is
time-consuming and it requires parameters. The only problem I''m having is
that the lines following the BeginInvoke (lines that depend on the outcome
of the method) are being executed while the lengthy process is being
executed. Would it be possible to "tell" those following lines of code not
to execute while the BeginInvoke method is running and run after the
BeginInvoke has finished executing ?

loadAuditBrowseHandler thisLoadAuditBrowseHandler = new
loadAuditBrowseHandler(this.Browse);
thisLoadAuditBrowseHandler.BeginInvoke(null,null);

// many more lines of code that cannot be executed while the BeginInvoke is
running



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 too


Since it''s an MDI application, the user can use any of the other separate
windows while this is running. This is running within the UI thread.

How would you create a callback delegate that''ll get me back to the main UI
thread?
This is the basic structure of my window:

private delegate void loadAuditBrowseHandler();
private void btn_filter_Click(object sender, System.EventArgs e)
{
loadAuditBrowseHandler thisLoadAuditBrowseHandler = new
loadAuditBrowseHandler(this.Browse);
thisLoadAuditBrowseHandler.BeginInvoke(null,null);
//Code that filters the data in the datatable and attaches the table to
the grid. Also does other misc. stuff
}

private void Browse()
{
// Code that fills a private member datatable with several hundred rows -
may take several seconds
/* During this process, I don''t want the application to look like it
froze */
}

Thanks again.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...

VM <vo******@yahoo.com> wrote:

In my Win app, I''m using delegates because the process that''ll be running is time-consuming and it requires parameters. The only problem I''m having is that the lines following the BeginInvoke (lines that depend on the outcome of the method) are being executed while the lengthy process is being
executed. Would it be possible to "tell" those following lines of code not to execute while the BeginInvoke method is running and run after the
BeginInvoke has finished executing ?

loadAuditBrowseHandler thisLoadAuditBrowseHandler = new
loadAuditBrowseHandler(this.Browse);
thisLoadAuditBrowseHandler.BeginInvoke(null,null);

// many more lines of code that cannot be executed while the BeginInvoke is running



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 too



VM <vo******@yahoo.com> wrote:

Since it''s an MDI application, the user can use any of the other separate
windows while this is running. This is running within the UI thread.

How would you create a callback delegate that''ll get me back to the main UI
thread?
Just use something with the same signature as AsyncCallback, call
EndInvoke on the IAsyncResult which is passed to you, and then call
Invoke or BeginInvoke on another delegate to do whatever you need to do
in the UI.
This is the basic structure of my window:

private delegate void loadAuditBrowseHandler();
private void btn_filter_Click(object sender, System.EventArgs e)
{
loadAuditBrowseHandler thisLoadAuditBrowseHandler = new
loadAuditBrowseHandler(this.Browse);
thisLoadAuditBrowseHandler.BeginInvoke(null,null);
//Code that filters the data in the datatable and attaches the table to
the grid. Also does other misc. stuff
}



All 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/~skeet
If replying to the group, please do not mail me too


这篇关于使用委托进行多线程处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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