Made a WPF multi-threading, in real-world scenarios using multithreading Guarantee program does not die, performance improved
Start thread processing
Thread thread1 = new Thread (UPDATEBTN);
Thread1. IsBackground = true;//is set as a background thread, the background thread exits automatically when the main thread ends, or the program does not end
Thread1. Start ();
private void Updatebtn ()
{
When doing name is a DataTable loop value to the foreground txt append
for (int i = 0; i < name. Rows.Count; i++)
{
Action Action1 = () =
{
This.txt.AppendText (name. Rows[i][0]. ToString () + "");
This.txt.AppendText (name. ROWS[I][1]. ToString () + "");
This.txt.AppendText (name. ROWS[I][2]. ToString () + "");
This.txt.AppendText (name. ROWS[I][3]. ToString () + "\ r \ n");
Txt. Select (txt. Text.length, 0);
Keyboard.focus (TXT);
};
Calling a delegate on the main thread because the main thread is calling
This.bfb.Dispatcher.Invoke (
New Action (
Delegate
{
var s = math.round ((float) i/name. Rows.Count * 100, 2) + "%";
This.bfb.Content = s;
}));
This.txt.Dispatcher.BeginInvoke (Action1);
Setprogressbar (i);
If you do not set the wait, the program will cause the card to die
Thread.Sleep (50);
}
This.progressBar1.Dispatcher.Invoke (
New Action (
Delegate
{
Bol = true;
System.Windows.MessageBox.Show ("execution complete");
progressbar1.visibility = Visibility.hidden; Hide
BfB. Visibility = Visibility.hidden; Hide
}));
}
Incidental:
C # Multi-threading, progress bar, send data to foreground in real time