在委托和线程重用上调用BeginInvoke [英] Calling BeginInvoke on delegates and thread reusage

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

问题描述

我们有一个应用程序,它执行从外部

应用程序提交的作业。它通过创建一个可以运行作业的

方法的委托,并在方法上调用BeginInvoke来异步执行它们。


整个工作本身,我们使用CallContext类来存储调用流中许多类所需的信息

。这很好用,并且
并发线程工作正常。


结果是,在完成一个工作后,另一个工作完成后,

有时候,新工作正在与

刚刚完成的工作相同的线程上执行(即,它们具有相同的线程ID)。因此看起来好像

线程正在被后续作业重用 - 因此

CallContext上的信息将从上一个作业(已完成)中遗留下来 - 作为<反对
反对被清除。


任何人都知道线程被重用的原因(特别是没有首先清除掉
)这样吗?

Hi, we have an app, that executes jobs submitted from an outside
application. It executes them asynchronusly, by creating a delegate to the
method that can run the job, and calling BeginInvoke on the method.

Throughout the job itself, we use the CallContext class to store information
that is needed by many classes in the call stream. This works well, and
concurrent threads are working fine.

What happens is, that after a job is done, and another one is started,
sometimes, the new job, is being executed on the same thread as the job that
just finished (i.e., they have the same thread id). So it looks like
threads are being reused by subsequent jobs - and so the information on the
CallContext is left over from the previous job (which has completed) - as
opposed to being cleared out.

Anyone know why the threads are being reused (especially without being
cleared out first) in this way?

推荐答案

Marina< so ***** @ nospam.com>写道:
Marina <so*****@nospam.com> wrote:
我们有一个应用程序,它执行从外部
应用程序提交的作业。它通过创建可以运行作业的
方法的委托,并在方法上调用BeginInvoke来异步执行它们。

在整个作业本身,我们使用CallContext类来存储信息
这是调用流中许多类所需要的。这很好用,并且并发线程工作正常。

在工作完成后,另一个工作完成后,会发生什么,
有时,新工作,正在与刚刚完成的作业相同的线程上执行(即,它们具有相同的线程ID)。因此看起来
线程正在被后续作业重用 - 所以
CallContext上的信息是从上一个作业(已完成)中遗留下来的 - 因为
反对被清除。

任何人都知道为什么线程被重用(特别是没有先被清除掉)?
Hi, we have an app, that executes jobs submitted from an outside
application. It executes them asynchronusly, by creating a delegate to the
method that can run the job, and calling BeginInvoke on the method.

Throughout the job itself, we use the CallContext class to store information
that is needed by many classes in the call stream. This works well, and
concurrent threads are working fine.

What happens is, that after a job is done, and another one is started,
sometimes, the new job, is being executed on the same thread as the job that
just finished (i.e., they have the same thread id). So it looks like
threads are being reused by subsequent jobs - and so the information on the
CallContext is left over from the previous job (which has completed) - as
opposed to being cleared out.

Anyone know why the threads are being reused (especially without being
cleared out first) in this way?




我不知道清除位,但线程被重用,因为

BeginInvoke使用ThreadPool。这通常是一件非常好的事情,因为创建线程是一项相对昂贵的任务。


-

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

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



I don''t know about the clearing bit, but threads are reused because
BeginInvoke uses the ThreadPool. This is usually a very good thing, as
creating threads is a relatively expensive task.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


使用threadpool(我遇到过)就是如果你的b / b
异步委托函数阻塞,你基本上会失去一个线程池

线程并且没有办法杀死据我所知,它。如果有,请

让我知道!


我意识到我不应该调用具有潜力的功能

永远阻止,但是这些阻塞调用发生在一个API中,我无法控制。


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

新闻:MP ************************ @ msnews.microsoft.c om ...
The problem with using the threadpool (that I''ve run into) is that if your
asynchronous delegate functions block, you basically lose a threadpool
thread and there''s no way to kill it, as far as I know. If there is, please
let me know!

I realize that I shouldn''t be calling functions that have the potential for
blocking forever, but these blocking calls are happening inside an API that
I have no control over.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Marina< so ***** @ nospam.com>写道:
Marina <so*****@nospam.com> wrote:
我们有一个应用程序,它执行从外部
应用程序提交的作业。它异步执行它们,通过创建
委托可以运行作业的方法,并在方法上调用BeginInvoke。

在整个作业本身,我们使用CallContext类来存储
调用流中许多类所需的信息。这很好用,并且并发线程工作正常。

在工作完成后,另一个工作完成后,会发生什么,
有时,新工作,正在与刚刚完成的作业
相同的线程上执行(即,它们具有相同的线程ID)。因此看起来
线程正在被后续作业重用 - 所以关于
的信息CallContext是从上一个作业(已经完成)遗留下来的 -
而不是被清除。

任何人都知道为什么线程被重用(特别是没有先被清除掉)?
Hi, we have an app, that executes jobs submitted from an outside
application. It executes them asynchronusly, by creating a delegate to the method that can run the job, and calling BeginInvoke on the method.

Throughout the job itself, we use the CallContext class to store information that is needed by many classes in the call stream. This works well, and
concurrent threads are working fine.

What happens is, that after a job is done, and another one is started,
sometimes, the new job, is being executed on the same thread as the job that just finished (i.e., they have the same thread id). So it looks like
threads are being reused by subsequent jobs - and so the information on the CallContext is left over from the previous job (which has completed) - as opposed to being cleared out.

Anyone know why the threads are being reused (especially without being
cleared out first) in this way?



我不是知道清除位,但线程被重用,因为BeginInvoke使用ThreadPool。这通常是一件非常好的事情,因为创建线程是一项相对昂贵的任务。

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



I don''t know about the clearing bit, but threads are reused because
BeginInvoke uses the ThreadPool. This is usually a very good thing, as
creating threads is a relatively expensive task.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



好的,谢谢,这已经解决了这个问题。


如果创建一个Thread类的实例,并以这种方式启动一个新的线程

,同样的问题会适用吗?


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

新闻:MP ************************ @ msnews.microsoft.c om ...
Ok, thanks, that has shed some light onto the issue.

If creating an instance of the Thread class, and starting up a new thread
that way, would the same issue apply?

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Marina< so ***** @ nospam.com>写道:
Marina <so*****@nospam.com> wrote:
我们有一个应用程序,它执行从外部
应用程序提交的作业。它异步执行它们,通过创建
委托可以运行作业的方法,并在方法上调用BeginInvoke。

在整个作业本身,我们使用CallContext类来存储
调用流中许多类所需的信息。这很好用,并且并发线程工作正常。

在工作完成后,另一个工作完成后,会发生什么,
有时,新工作,正在与刚刚完成的作业
相同的线程上执行(即,它们具有相同的线程ID)。因此看起来
线程正在被后续作业重用 - 所以关于
的信息CallContext是从上一个作业(已经完成)遗留下来的 -
而不是被清除。

任何人都知道为什么线程被重用(特别是没有先被清除掉)?
Hi, we have an app, that executes jobs submitted from an outside
application. It executes them asynchronusly, by creating a delegate to the method that can run the job, and calling BeginInvoke on the method.

Throughout the job itself, we use the CallContext class to store information that is needed by many classes in the call stream. This works well, and
concurrent threads are working fine.

What happens is, that after a job is done, and another one is started,
sometimes, the new job, is being executed on the same thread as the job that just finished (i.e., they have the same thread id). So it looks like
threads are being reused by subsequent jobs - and so the information on the CallContext is left over from the previous job (which has completed) - as opposed to being cleared out.

Anyone know why the threads are being reused (especially without being
cleared out first) in this way?



我不是知道清除位,但线程被重用,因为BeginInvoke使用ThreadPool。这通常是一件非常好的事情,因为创建线程是一项相对昂贵的任务。

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



I don''t know about the clearing bit, but threads are reused because
BeginInvoke uses the ThreadPool. This is usually a very good thing, as
creating threads is a relatively expensive task.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



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

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