ParameterizedThreadStart task

using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

AsyncAwait namespace
{
class Program
{
//http://www.cnblogs.com/sheng-jie/p/6471986.html
// 3.3. What is the foreground thread
// By default, use Thread.Start () method creates thread is the foreground thread. Foreground thread can prevent the end application, only if all of the foreground thread is finished, CLR to close the application (application domain unload that is hosted). Foreground thread also belong to the worker thread.

//3.4. What is the background thread
// background thread does not affect the application of the end, when all foreground thread is finished, regardless of whether the background thread is finished, will be terminated. General background thread to do trivial tasks (such as mailbox from time to time to go check the next e-mail, weather application from time to time to update the weather). Background thread also belong to the worker thread.

static void Main(string[] args)
{


// C # async keyword is used to specify a method, Lambda expressions or anonymous method for automatically invoked in an asynchronous manner.
Console.WriteLine ( "main thread starts, the current thread is:" + Thread.CurrentThread.ManagedThreadId);
var = getLengthAsync Task ();

Console.WriteLine ( "back to the main thread, the current thread is:" + Thread.CurrentThread.ManagedThreadId);

Console.WriteLine ( "Thread {" + Thread.CurrentThread.ManagedThreadId + "] sleep 5s:");
Thread.Sleep (5000); // main thread to sleep 5s

The Stopwatch new new Timer = var ();
timer.start (); // calculation time

Console.WriteLine ( "task return value is" + task.Result);

timer.Stop (); // end point, as well as additional StopWatch Reset method, can be reset.
Console.WriteLine ( "waiting for:" + timer.Elapsed.TotalSeconds + "s"); // time display

Console.WriteLine ( "main thread ends, the current thread is:" + Thread.CurrentThread.ManagedThreadId);


//Console.WriteLine ( "main thread begins!");

//ForeBackGround();
//ThreadPoolTest();
Console.ReadKey();
}

the async static the Task Private <int> getLengthAsync ()
{
Console.WriteLine ( "getLengthAsync () starts executing the current thread is:" + Thread.CurrentThread.ManagedThreadId);

var str = await GetStringAsync();

Console.WriteLine ( "GetLengthAsync () is finished, the current thread is:" + Thread.CurrentThread.ManagedThreadId);

return str.Length;
}

static the Task Private <String> GetStringAsync ()
{
Console.WriteLine ( "GetStringAsync () starts executing the current thread is:" + Thread.CurrentThread.ManagedThreadId);
return Task.Run (() =>
{
Console.WriteLine ( "Asynchronous mission started, the current thread is: "+ Thread.CurrentThread.ManagedThreadId);

Console.WriteLine ( "Thread {" + Thread.CurrentThread.ManagedThreadId + "] sleep 10s");
Thread.Sleep (10000); // asynchronous task threads sleep 10s

return "GetStringAsync () is finished";

});

}

public static void TaskDemo2()
{
Console.WriteLine("主线程ID:" + Thread.CurrentThread.ManagedThreadId);
Task.Run(() => Console.WriteLine("Task对应线程ID:" + Thread.CurrentThread.ManagedThreadId));
Console.ReadLine();
}

public static void TaskDemo()
{
Console.WriteLine("主线程ID:" + Thread.CurrentThread.ManagedThreadId);

Task.Factory.StartNew(() => Console.WriteLine("Task对应线程ID:" + Thread.CurrentThread.ManagedThreadId));
Console.ReadKey();
}

/// <summary>
/// ThreadPool(线程池)
/// </summary>
public static void ThreadPoolTest()
{
WaitCallback workItem = state => Console.WriteLine("当前线程Id为:" + Thread.CurrentThread.ManagedThreadId);

for (int i = 0; i < 10; i++)
{
ThreadPool.QueueUserWorkItem(workItem);
}

}

/// <Summary>
/// foreground thread, the background thread
/// </ Summary>
Private static void ForeBackGround ()
{
// Create foreground worker thread
the Thread = T1 new new the Thread (Task1);
t1.Start ();

// Create Background worker thread
the Thread T2 = the Thread new new (new new ParameterizedThreadStart (Task2));
t2.IsBackground = to true; // set the background thread
t2.Start ( "mass participation");
}

static void Task1 Private ()
{
the Thread.Sleep (1000); // analog consuming operations, sleep lS
Console.WriteLine ( "foreground thread is called!");
}

static void Task2 Private (Object Data)
{
the Thread.Sleep (2000); // analog consuming operations, sleep 2S
Console.WriteLine ( "background thread is called!" + Data);
}

}
}

Guess you like

Origin www.cnblogs.com/Jeely/p/11001736.html