【发布时间】:2011-03-15 02:36:04
【问题描述】:
我是多线程的新手,我一直在做概念验证,我还“发现”了 (VS2008) 线程窗口:
我的问题是:如何将正在运行的线程“链接”到我的代码? 例如,我将如何获取线程 ID(如线程窗口中所示)以便我可以记录它(例如); 或,BeginInvoke() 方法采用我设置的“id”参数(字符串)(在下面的示例中为“服务 A”),但我在线程窗口中看不到它。
我感兴趣的是我正在使用 AsyncCallbacks 和 BeginInvoke() 启动 三个 并行执行线程,但我只能在线程窗口中看到两个工作线程我想我应该看到三个。实际上我认为我可以 - 三个工作线程的“名称”为<No Name>。
以下是我正在使用的一些代码供参考:
// 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
...
}
}
【问题讨论】:
标签: c# .net visual-studio multithreading asynchronous