C # Multi-threading, progress bar, send data to foreground in real time

Source: Internet
Author: User

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

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.