Getting Started--------------------------------------------------------------------------------
Overview and Concepts
A C # program starts with a single thread, which is created automatically by the CLR and the operating system (also known as the "Main thread").
Create and start using multithreading
Public Window1 ()
{
Main thread
Code ...
To start a child thread by using an anonymous method
Thread t = new Thread (delegate () {Code ...});
T.start ();
}
Child Process Create instance
//1. Create Child Threads
public Window1 ()
{
/create child processes
Thread Threada = new Thread (new ThreadStart (Workmethod));
Threada.start ();
}
//child process launcher
void Workmethod ()
{
This.textBox1.Dispatcher.Invoke (Dispatcherpriority.normal, (ThreadStart) delegate
{
Textbox1.appendtext (Environment.NewLine);
This.textBox1.AppendText ("sub-thread execution complete!");
});
}
//2. Use anonymous delegates to start child threads
public Window1 ()
{
thread t = new Thread (delegate () {
This.textBox1.Dispatcher.Invoke (Dispatcherpriority.normal, (ThreadStart) delegate
{
Textbox1.appendtext (Environment.NewLine);
This.textBox1.AppendText ("Child thread execution completed!");
});
});
T.start ();
}
//3. Use anonymous delegates to start child threads
public Window1 ()
{
InitializeComponent ();
textbox1.appendtext ("Main thread execution complete");
New Thread (() =>
{
this. Dispatcher.invoke (New Action () =>
{
Textbox1.appendtext ( Environment.NewLine);
Textbox1.appendtext ("Sub-thread Execution complete");
});
}). Start ();
}
Thread Synchronization Base--------------------------------------------------------------------------------
Synchronization Essentials
Lock and Thread safety
Blocks the current thread for a specified time
Thread.Sleep (Timespan.fromseconds (30));
Interrupt and Abort
Thread state
Wait handle
Synchronization environment
Use multi-threaded--------------------------------------------------------------------------------
Cell mode and Windows Forms
BackgroundWorker class
ReaderWriterLock class
Thread pool
Asynchronous delegate
Timer
Public Window1 ()
{
Timer Settimeinterval
System.Windows.Threading.DispatcherTimer t = new System.Windows.Threading.DispatcherTimer ();
T.interval = new TimeSpan (0, 0, 0, 0, 100);
T.tick + = new EventHandler (timer_interval);
T.start ();
}
Public Window1 ()
{
Timer SetTimeOut
New System.Threading.Timer (New TimerCallback (Timer_callback), this, 5000, 0);
}
Local storage
Advanced Topics--------------------------------------------------------------------------------
Non-blocking synchronization
Wait and Pulse
Suspend and resume
Terminating a thread
Synchronizing Thread instances
Public Window1 ()
{
InitializeComponent ();
This. Windowstartuplocation = Windowstartuplocation.centerowner;
This.textBox1.Dispatcher.Invoke (Dispatcherpriority.normal, (ThreadStart) delegate
{
Textbox1.appendtext (Environment.NewLine);
This.textBox1.AppendText ("Synchronous thread execution completed!");
});
}
Asynchronous Thread Instances
Public Window1 ()
{
TextBox1.Dispatcher.BeginInvoke (Dispatcherpriority.normal, (Action) () =
{
Textbox1.appendtext (Environment.NewLine);
Textbox1.appendtext ("Asynchronous Thread Execution Complete");
}));
}
Thread rendering control instance-can pass parameters
Public Window1 ()
{
InitializeComponent ();
System.Windows.Threading.DispatcherTimer t = new System.Windows.Threading.DispatcherTimer ();
T.interval = new TimeSpan (0, 0, 0, 0, 1000);
T.tick + = new EventHandler (Dispatcher_timer);
T.start ();
}
Private delegate void Dispatcherdelegate (string msg);
private void OutPut (String msg)
{
TextBox1.Dispatcher.Invoke (New Dispatcherdelegate (Delegatemethod), msg);
}
private void Delegatemethod (String msg)
{
Textbox1.appendtext (msg);
Textbox1.appendtext (DateTime.Now.ToString ("Yyyy-mm-dd hh:mm:ss"));
Textbox1.appendtext (Environment.NewLine);
}
void Dispatcher_timer (object sender, EventArgs e)
{
OutPut ("Control Rendering:");
}
Thread rendering control instance-using anonymous delegates
Public Window1 ()
{
InitializeComponent ();
This. Windowstartuplocation = Windowstartuplocation.centerowner;
System.Windows.Threading.DispatcherTimer t = new System.Windows.Threading.DispatcherTimer ();
T.interval = new TimeSpan (0, 0, 0, 0, 1000);
T.tick + = new EventHandler (Dispatcher_timer);
T.start ();
}
void Dispatcher_timer (object sender, EventArgs e)
{
TextBox1.Dispatcher.BeginInvoke (Dispatcherpriority.normal, (Action) () =
{
Textbox1.appendtext ("Control Rendering:");
Textbox1.appendtext (DateTime.Now.ToString ("Yyyy-mm-dd hh:mm:ss"));
Textbox1.appendtext (Environment.NewLine);
}));
}
Thread renders the control instance-anonymous method and returns the parameter
Public Window1 ()
{
InitializeComponent ();
System.Windows.Threading.DispatcherTimer t = new System.Windows.Threading.DispatcherTimer ();
T.interval = new TimeSpan (0, 0, 0, 0, 1000);
T.tick + = new EventHandler (Dispatcher_timer);
T.start ();
}
void Dispatcher_timer (object sender, EventArgs e)
{
String strmsg = (string) textBox1.Dispatcher.Invoke (new func<string> () =
{
Textbox1.appendtext ("Control Rendering:");
Textbox1.appendtext (DateTime.Now.ToString ("Yyyy-mm-dd hh:mm:ss"));
Textbox1.appendtext (Environment.NewLine);
return TextBox1.Text;
}));
}
Threads: main thread, child thread synchronization thread, asynchronous thread single threaded, multithreaded System.Threading and System.Windows.Threading