4.0 with Task.Factory.StartNew (() =>{});
4.0 below with ThreadPool.QueueUserWorkItem (() =>{})
More than 4.0 in Task.run (() =>{});
Multi-tasking:
task[] tasks = new Task[maxcurrent];
for (int i = Beginid; I <= maxid; i + = interval, counter++)
Tasks[counter] = new Task (worker. Testhandler, taskcreationoptions.longrunning);
var continuation = Task.Factory.ContinueWhenAll (
Tasks, (antecedents) =>{
Loginfo ("All Threads has loaded!");
});
foreach (Task t in Tasks)
T.start ();
Loginfo ("All threads has been queued. Waiting to complete ... ");
while (!continuation. iscompleted)
Thread.Sleep (1000);
static random _random = new Random ();
static void Main (string[] args)
{
ArrayList listthread = new ArrayList ();
ArrayList Listresult = new ArrayList ();
for (int i = 0; i <; i++)
{
Thread thread = new Thread (new Parameterizedthreadstart (Workthread));
Thread. Start (Listresult);
Listthread.add (thread);
}
foreach (thread thread in Listthread)
{
Thread. Join ();
}
foreach (int i in Listresult)
{
Console.WriteLine (i);
}
}
static void Workthread (Object list)
{
int cnt = _random. Next (1,10);
ArrayList listlocal = new ArrayList ();
for (int i = 0; i < cnt; i++)
{
Listlocal.add (CNT);
Thread.Sleep (100);
}
Lock (List)
{
(list as ArrayList). AddRange (listlocal);
}
}
C # Multithreading meets long time-consuming operations and multitasking