This section contains two examples. The first example shows how to create a thread that executes the static method. The second example shows how to create a thread that executes the instance method.
These examples show their output in the textblock of the UI thread. To access textblock from the callback thread, these examples use the dispatcher attribute to obtain the dispatcher object of textblock, and then use the dispatcher. begininvoke Method for cross-thread calling.
For more examples of creating a thread, see create a thread at startup and pass data. For examples of using the wait handle to coordinate thread operations, see eventwaithandle. For examples of using a critical section (lock in C # And synclock in Visual Basic) to coordinate thread operations, see monitor. For examples of how to use a thread pool thread, see backgroundworker, threadpool, and timer.
Example 1
The following example shows how to create a thread that executes the static method.
Note: |
To run this example, see the example of using the demo method and textblock control. |
C # VB
using System;using System.Threading;public class Example{ private static System.Windows.Controls.TextBlock outputBlock; public static void Demo(System.Windows.Controls.TextBlock outputBlock) { Example.outputBlock = outputBlock; // To start a thread using a static thread procedure, use the // class name and method name when you create the ThreadStart // delegate. C# expands the method name to the appropriate // delegate creation syntax: // New ThreadStart(Example.DoWork) // Thread newThread = new Thread(Example.DoWork); newThread.Start(); } // Simulate work. To communicate with objects on the UI thread, get the // Dispatcher for one of the UI objects. Use the Dispatcher object's // BeginInvoke method to queue a delegate that will run on the UI thread, // and therefore can safely access UI elements like the TextBlock. private static void DoWork() { outputBlock.Dispatcher.BeginInvoke(delegate () { outputBlock.Text += "Hello from a static thread procedure.\n"; }); }}/* This code example produces the following output:Hello from a static thread procedure. */
Example 2
The following example shows how to create a thread that executes the instance method.
Note: |
To run this example, see the example of using the demo method and textblock control. |
C # VB
using System;using System.Threading;public class Example{ public static void Demo(System.Windows.Controls.TextBlock outputBlock) { // To start a thread using an instance method for the thread // procedure, use the instance variable and method name when // you create the ThreadStart delegate. C# expands the object // reference and method name to the appropriate delegate // creation syntax: // New ThreadStart(AddressOf w.DoMoreWork) // Work w = new Work(); w.Data = 42; w.Output = outputBlock; Thread newThread = new Thread(w.DoMoreWork); newThread.Start(); }}public class Work{ public int Data; public System.Windows.Controls.TextBlock Output; // Simulate work. To communicate with objects on the UI thread, get the // Dispatcher for one of the UI objects. Use the Dispatcher object's // BeginInvoke method to queue a delegate that will run on the UI thread, // and therefore can safely access UI elements like the TextBlock. public void DoMoreWork() { Output.Dispatcher.BeginInvoke(delegate () { Output.Text += String.Format("Instance thread procedure. Data={0}\n", Data); }); }}// This code example produces the following output:////Instance thread procedure. Data=42