开发者

Thread Ready and Completed Events

开发者 https://www.devze.com 2023-02-07 07:06 出处:网络
I have some portion of code that needs to be executed in each thread (regardless of how it is spawned) running in a specific app domain just before the thread starts executing a code and after the exe

I have some portion of code that needs to be executed in each thread (regardless of how it is spawned) running in a specific app domain just before the thread starts executing a code and after the execution completes.

I found that the System.ComponentModel.BackgroundWorker class has the event same as the second one that I need (see the table).

┌───────────┬────────────────────────┬─────────────────────────────────────────────────────────────────────────┐
│ ? class   │ BackgroundWorker class │ Description                                                             │
├───────────┼────────────────────────┼─────────────────────────────────────────────────────────────────────────┤
│ Ready     │ -                      │ Operation execution is about to start.                                  │
├───────────┼────────────────────────┼─────────────────────────────────────────────────────────────────────────┤
│ ?         │ DoWork                 │ Execute operation.                                                      │
├───────────┼────────────────────────┼─────────────────────────────────────────────────────────────────────────┤
│ ?         │ ProgressChanged        │ Notify about progres开发者_JS百科s in execution of operation.                        │
├───────────┼────────────────────────┼─────────────────────────────────────────────────────────────────────────┤
│ Completed │ RunWorkerCompleted     │ Operation has completed, has been cancelled or has raised an exception. │
└───────────┴────────────────────────┴─────────────────────────────────────────────────────────────────────────┘

Are there events such as Ready and Completed from the previous table? I did not see such events neither on System.Threading.Thread nor System.AppDomain class.


If I understand correctly, you want an event at/before the beginning of DoWork.

The event-model does not ptovide this because you can easily call a method

  • just before RunWorkerAsync, from the calling thread
  • immediately at the start of DoWork from the worker thread.

So there was no clear case for such an event.


There are many different ways to start new threads. The thread pool is used not only by QueueUserWorkItem and BackgroundWorker, but also by any code that does asynchronous delegate invocations. Also, the Begin... and End... methods for doing asynchronous things like Stream.BeginRead and Stream.EndRead start and end threads. And, of course, there's the standard Thread.Start() when using managed threads. And then there's the Task Parallel Library . . .

And, of course, there are the myriad ways of starting threads directly with the Windows API.

Even if you ignore the Windows API possibilities, you're still asking to hook a number of different mechanisms so that you can monitor activity. You'd have to hook the managed thread functions as well as the thread pool functions that initiate and shut down work items.

A general solution to this problem probably isn't possible without some serious spelunking into the runtime library and doing all manner of nasty undocumented things in unmanaged code. Even then, I'm not convinced it can be done. The hooks you need just aren't exposed.

And if you want to include the Windows API possibilities ... well, good luck.

0

精彩评论

暂无评论...
验证码 换一张
取 消