Typically when you spawn a new thread, you want to give it a name to facilitate debugging. For example:
using System.Threading;
//.. other stuff....
Thread thread = new Thread(new ThreadStart(DoSomething);
thread.Name = "DoingSomething";
threat.Start();
The code in the method DoSomething (not shown) will run on a thread named "DoingSomething."
Now suppose you're writing a socket server using the asynchronous programming model. You might write something that looks like the following:
using System.Net.Sockets;
using System.Threading;
ManualResetEvent allDone = new ManualResetEvent(false);
public static void Main()
{
Socket socket = new Socket(...); //you get the idea
while(true)
{
allDone.Reset();
socket.BeginAccept(new AsyncCallback(OnSocketAccept), socket);
allDone.WaitOne();
}
}
public void OnSocketAccept()
{
Thread.CurrentThread.Name = "SocketAccepted";
allDone.Set();
// Some socket operation.
}
In the example above, we're setting up a...