C # thread updates UI

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.