Home > Net >  C # call control - 4 methods across threads
C # call control - 4 methods across threads

Time:09-16

In the application of c # development, we often have to separate the UI thread and worker thread, prevent interface stopped responding, at the same time, we also need to update in the worker thread UI controls,
Here are several common methods
Invalid operation between threads
Interface has a button and a label, click on the button will start a thread to update the label value
 private void button1_Click (object sender, EventArgs e) 
{
Thread thread1=new Thread (new ParameterizedThreadStart (UpdateLabel));
Thread1. Start (" update Label ");
}

Private void UpdateLabel object (STR)
{
Enclosing label1. Text=STR. ToString ();
}


After the operation, the program will error "across threads operation is invalid, never is to create" label1 "threads access it"

This is because the.net banned across threads call control, or anyone can control operation, finally may cause errors,

Here are several across threads call control method

The first way: ban the compiler to across threads access do check this is the simplest way, the equivalent of a conflict between threads do not check, allowing individual threads just messing around, finally Lable1 controls what is the value of is unpredictable (do not recommend using this approach)

 public _click () 
{
InitializeComponent();
//to join the line
Control. CheckForIllegalCrossThreadCalls=false;
}


Second approach: use the delegate and invoke to calls from other threads invoke method control call control, can control controls, such as
 private void button2_Click (object sender, EventArgs e) 
{
Thread thread1=new Thread (new ParameterizedThreadStart (UpdateLabel2));
Thread1. Start (" update Label ");
}

Private void UpdateLabel2 object (STR)
{
If (label2. InvokeRequired)
{
//when a control InvokeRequired property value to true, that have a create it outside of the thread to access it
Action ActionDelegate=(x)=& gt; {this. Label2. Text=x.T oString (); };
//or
//Action ActionDelegate=delegate (string) {this. Label2. Text=TXT; };
This. Label2. Invoke (actionDelegate, STR);
}
The else
{
Enclosing label2. Text=STR. ToString ();
}
}



A third way: use the delegate and the BeginInvoke to control control from other threads as long as the above this. Label2. Invoke (actionDelegate, STR); It is ok to Invoke the BeginInvoke method instead of
Invoke method and the BeginInvoke method is the difference between the
Invoke method is synchronized, it will be waiting for the worker thread to finish,
The BeginInvoke method is asynchronous, it will finish the work on another thread to thread

A fourth way: use BackgroundWorker component BackgroundWorker is (it is recommended to use this method). The.net inside controls to perform multithreaded task, it allows programmers to perform some operation on a separate thread, time-consuming operation (e.g., download and database transactions), usage simple
 private void button4_Click (object sender, EventArgs e) 
{
Using (BackgroundWorker bw=new BackgroundWorker ())
{
Bw. RunWorkerCompleted +=new RunWorkerCompletedEventHandler (bw_RunWorkerCompleted);
Bw. DoWork +=new DoWorkEventHandler (bw_DoWork);
Bw. RunWorkerAsync (" Tank ");
}
}

Void bw_DoWork (object sender, DoWorkEventArgs e)
{
//this is a background thread, was done on another thread
//this is a real work of worker threads
Here//can do some time-consuming, complex operations
Thread.sleep (5000);
"E.R esult=e.A rgument +" worker thread to complete ";
}

Void bw_RunWorkerCompleted (object sender, RunWorkerCompletedEventArgs e)
{
//the background thread has been completed, and returned to the main thread, so you can directly use the UI controls the
This. Label4. Text="e.R esult. ToString ();
}



Turn from http://www.cnblogs.com/TankXiao/p/3348292.html
Turn from https://www.51halcon.com/forum.php? Mod=viewthread& Tid=450 & amp; Extra=page % 3 d2%26 filter % 3 dtypeid % 26 typeid % 3 d68

CodePudding user response:

Learned,,

CodePudding user response:

Thanks for sharing,

CodePudding user response:

Encourage,

Can also have other method, suggests two here, try it?
1, SynchronizationContext (WindowsFormsSynchronizationContext)
2, async await (await support context capture),

CodePudding user response:

In addition to the first one is nothing method,

To master a variety of methods and a kind of no difference,

CodePudding user response:

Only a few attributes will be an error

CodePudding user response:

Add a WPF
 
Private void XXXXXX ()
{
This. The Dispatcher. Invoke (DispatcherPriority. Normal, (ThreadStart delegate ()
{
XXXXXX
});
}


CodePudding user response:

Thank you for your bosses

But if use WPF Dispatcher. Invoke locked the main thread, need to construct the main thread to wait for a background thread, and the background thread calls the Dispatcher. Invoke can, because the calls to the Dispatcher. Invoke call is completed, will be waiting for the main thread this main thread is waiting for background threads, however, when two threads will wait for each other

CodePudding user response:

refer to 7th floor lindexi_gd response:

thanks for leaders
But if use WPF Dispatcher. Invoke locked the main thread, need to construct the main thread to wait for a background thread, and the background thread calls the Dispatcher. Invoke can, because the calls to the Dispatcher. Invoke call is completed, will be waiting for the main thread this main thread is waiting for background threads, however, when two threads will wait for each other

Yes, in use to sleep, or hang up

CodePudding user response:

Didn't think over the years, CSDN still struggling with threads,

Hope that most people use Task programming, give up Thread programming directly, so as not to tangle jam, dormancy and so on, hope to improve the level of asynchronous multi-threaded,

CodePudding user response:

refer to the eighth floor desperaso response:
Quote: refer to 7th floor lindexi_gd response:

Thank you for your bosses

But if use WPF Dispatcher. Invoke locked the main thread, need to construct the main thread to wait for a background thread, and the background thread calls the Dispatcher. Invoke can, because the calls to the Dispatcher. Invoke call is completed, will be waiting for the main thread this main thread is waiting for background threads, however, when two threads will wait for each other

Yes, in use to sleep, or hung up


nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related