Is there a way to see what threads are being created in .net C#

  • Thread starter Thread starter Nu2Csharp
  • Start date Start date
N

Nu2Csharp

Guest
I have the following simple program in an effort to understand threads, and when I register methods to be called upon cancellation of the original activity I see one additional thread without a name or anything before the application quits. Can someone provide more insight to what is going on in the following code? I greatly appreciate it.

using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ScratchPad
{
class Program
{
static void Main(string[] args)
{
CancellationTokenSource cts = new CancellationTokenSource();
cts.Token.Register(Method1);
String resp = String.Empty;
//Before you spin up a new thread ask if the user wants to proceed, and if they say no, cancel the operation
Console.WriteLine("Do you want to cancel the work? ");
resp = Console.ReadLine();
if (resp == "y") cts.Cancel();
Object someState=cts.Token;
ThreadPool.QueueUserWorkItem(o=>MyMethod(cts.Token));
Console.WriteLine("Back to main method");
Console.ReadLine();
}
private static void MyMethod(CancellationToken token)
{
//CancellationTokenSource myCancelationTokenSource = new CancellationTokenSource();
//myCancelationTokenSource.Token = state;
if (token.IsCancellationRequested)
Console.WriteLine("work was canceled");
else
Console.WriteLine("In my Method");
}
private static void Method1()
{
Console.WriteLine("Method1, a registered method, was called because the operation was cancelled ");
}
public void Method2 ()
{

}
}
}



I see this Thread when I put a break on line 20 of the code "if (resp == "y") cts.Cancel();" and debug the code.

Continue reading...
 
Back
Top