what is this called "the thread 0x2ef0" and how can I display it in the command prompt? I tried executing the code and it was only display in the output box or the debugger code, is there a possible way to print this hex decimal thread code? to the command prompt? i was using an windows application with windows property but when i tried in windows application with console property the "the thread 0x2ef0" is not showing off so i want to display it in command prompt.
CodePudding user response:
Probably one of these:
using System;
using System.Diagnostics;
using System.Threading;
namespace Demo
{
internal static class Program
{
private static void Main()
{
Console.WriteLine("0x{0:x4}", Thread.CurrentThread.ManagedThreadId);
Console.WriteLine("0x{0:x4}", Thread.CurrentThread.GetHashCode());
Console.WriteLine("0x{0:x4}", AppDomain.GetCurrentThreadId());
Console.WriteLine("0x{0:x4}", Process.GetCurrentProcess().Threads[0].Id);
}
}
}
CodePudding user response:
Try using the CurrentThread property in order to get the data you are looking for.
Task<Double> t = Task.Run( () => { ShowThreadInformation("Main Task(Task #" Task.CurrentId.ToString() ")");
for (int ctr = 1; ctr <= 20; ctr )
tasks.Add(Task.Factory.StartNew(
() => { ShowThreadInformation("Task #" Task.CurrentId.ToString());
long s = 0;
for (int n = 0; n <= 999999; n ) {
lock (rndLock) {
s = rnd.Next(1, 1000001);
}
}
return s/1000000.0;
} ));
Task.WaitAll(tasks.ToArray());
Double grandTotal = 0;
Console.WriteLine("Means of each task: ");
foreach (var child in tasks) {
Console.WriteLine(" {0}", child.Result);
grandTotal = child.Result;
}
Console.WriteLine();
return grandTotal / 20;
} );
Console.WriteLine("Mean of Means: {0}", t.Result);