尝试用实际执行的内容来解释(VS2008)线程窗口

发布于 2024-10-22 05:59:44 字数 1467 浏览 2 评论 0原文

我是多线程新手,我一直在进行概念验证,我还“发现”了(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:
enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒废 2024-10-29 05:59:44

您可以使用 Thread.Name 来设置线程的名称。设置名称后,它将出现在“线程”窗口的“名称”栏中。

例如,假设 Service A 是您希望出现在“线程”窗口的“名称”列中的名称,您可以在 PretendWorkingServiceCall 中执行类似的操作:

void PretendWorkingServiceCall(int sleepMilliseconds, string name)
{
    System.Threading.Thread.CurrentThread.Name = name;

    // your code goes here
}

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 in PretendWorkingServiceCall:

void PretendWorkingServiceCall(int sleepMilliseconds, string name)
{
    System.Threading.Thread.CurrentThread.Name = name;

    // your code goes here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文