尝试用实际执行的内容来解释(VS2008)线程窗口
我是多线程新手,我一直在进行概念验证,我还“发现”了(VS2008)线程窗口:
我的问题是:如何将正在运行的线程“链接”到我的代码? 例如,我如何获取线程 ID(如线程窗口中所示)以便我可以记录它(例如); 或,BeginInvoke() 方法采用我已设置的“id”参数(字符串)(在下面的示例中为“Service A”),但我在线程窗口中看不到它。
我感兴趣的是,我使用 AsyncCallbacks 和 BeginInvoke() 启动三个并行执行线程,但我只能在线程窗口中看到两个工作线程,而我认为我应该这样做见三。实际上我认为我可以 - 三个工作线程的“名称”为
。
作为参考,这里是我正在使用的一些代码:
// Creating the call back and setting the call back delegate
AsyncCallback callBackA = new AsyncCallback(AsyncOperationACompleted);
// callBackB ...
// callBackC ...
// Create instances of the delegate, which calls the method we want to execute
callerA = new DumbEndPoint.AsyncMethodCaller(DumbEndPoint.PretendWorkingServiceCall);
// callerB ...
// callerC ...
// sleep = thread sleep time in milliseconds
IAsyncResult resultA = callerA.BeginInvoke(sleep, "Service A", callBackA, null);
// resultB ...
// resultC ...
// I expect to see three threads in the Threads Window at this point.
然后我在回调委托中获取结果:
private void AsyncOperationACompleted(IAsyncResult result)
{
try
{
string returnValue = callerA.EndInvoke(result);
mySmartDTO.ServiceDataA = returnValue;
}
catch (Exception ex)
{
// logging
...
}
}
I'm new to multi-threading and I have been doing a Proof-of-Concept, I've also 'discovered' the (VS2008) Threads Window:
My Question is: how can I "link" the running threads to my code?
For example, how would I get the Thread ID (as shown in the Threads Window) so that I could log it (for example); or, the BeginInvoke() method takes a 'id' argument (string) which I have set (as "Service A" in the example below) but I can't see it in the Threads Window.
The thing that interests me is that I'm sparking up three parallel threads of execution using AsyncCallbacks and BeginInvoke() but I can only see two worker threads in the Threads Window at a point where i think I should see three. Actually I think I can - the three worker threads with the 'Name' as <No Name>
.
For reference here's some of the code I'm using:
// Creating the call back and setting the call back delegate
AsyncCallback callBackA = new AsyncCallback(AsyncOperationACompleted);
// callBackB ...
// callBackC ...
// Create instances of the delegate, which calls the method we want to execute
callerA = new DumbEndPoint.AsyncMethodCaller(DumbEndPoint.PretendWorkingServiceCall);
// callerB ...
// callerC ...
// sleep = thread sleep time in milliseconds
IAsyncResult resultA = callerA.BeginInvoke(sleep, "Service A", callBackA, null);
// resultB ...
// resultC ...
// I expect to see three threads in the Threads Window at this point.
I'm then getting the results in the call back delegate:
private void AsyncOperationACompleted(IAsyncResult result)
{
try
{
string returnValue = callerA.EndInvoke(result);
mySmartDTO.ServiceDataA = returnValue;
}
catch (Exception ex)
{
// logging
...
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 Thread.Name 来设置线程的名称。设置名称后,它将出现在“线程”窗口的“名称”栏中。
例如,假设
Service A
是您希望出现在“线程”窗口的“名称”列中的名称,您可以在PretendWorkingServiceCall
中执行类似的操作:You can use Thread.Name to set a name for the thread. After the name is set, it will appear in the "Name" column of the Threads window.
e.g., assuming that
Service A
is the name you'd like to appear in the Name column of the Threads window, you could do something like this inPretendWorkingServiceCall
: