开发者

Trying to interpret (VS2008) Threads Window with whats actually executing

I'm new to multi-threading and I have been doing a Proof-of-Concept, I've also 'discovered' the (VS2008) Threads Window:

Trying to interpret (VS2008) Threads Window with whats actually executing

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 t开发者_如何学Gohat 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
            ...
        }
    }


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
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜