C # solution to the multi-thread Interface

Source: Internet
Author: User

C # solution to the multi-thread Interface

Problem description:

When our interface needs to constantly update data while the program is running,

When the data of a textbox needs to change,

For this question, refer to my other article.

The best solution is to use multiple threads to ensure that there is no such thing in the program execution.

A main thread is used to create the interface, and a subthread is used to execute the program and update the main interface.

In this way, there will be no stuck images.

This is certainly no problem,

But why is there a lot of freezing problems in the process of using it, and some users tell me that it is my Httphelper class, but it is not actually, again, I declare that my Httphelper class has nothing to do with multithreading. Don't trust me anymore.

This problem has actually been difficult for me for a long time, but I have finally solved it today, and I have found many people have such problems, so I will share an example for your reference. Let's take a look at my interface.

When I click

After execution

Data is constantly updated.

At this time, the interface will not be stuck,

Data is constantly updated.

See my code below

Using System;

Using System. Collections. Generic;

Using System. ComponentModel;

Using System. Data;

Using System. Drawing;

Using System. Linq;

Using System. Text;

Using System. Windows. Forms;

Using System. Threading;

Namespace WindowsFormsApplication3

{

Public partial class Form1: Form

{

Public Form1 ()

{

InitializeComponent ();

}

// Create a delegate to access the TextBox Control.

Public delegate void UpdateTxt (string msg );

// Define a delegate variable

Public UpdateTxt updateTxt;

// Modify the TextBox value.

Public void UpdateTxtMethod (string msg)

{

RichTextBox1.AppendText (msg + "rn ");

RichTextBox1.ScrollToCaret ();

}

// This is the call method in a non-creation thread. It actually uses the TextBox Invoke method.

Public void ThreadMethodTxt (int n)

{

This. BeginInvoke (updateTxt, "the thread starts to execute, execute" + n + "times, once every second ");

For (int I = 0; I <n; I ++)

{

This. BeginInvoke (updateTxt, I. ToString ());

// One-second execution

Thread. Sleep (1000 );

}

This. BeginInvoke (updateTxt, "end of thread ");

}

// Enable the thread

Private void button#click (object sender, EventArgs e)

{

Thread objThread = new Thread (new ThreadStart (delegate

{

ThreadMethodTxt (Convert. ToInt32 (textBox1.Text. Trim ()));

}));

ObjThread. Start ();

}

Private void Form1_Load_1 (object sender, EventArgs e)

{

// Instantiate the delegate

UpdateTxt = new UpdateTxt (UpdateTxtMethod );

}

}

}

The above is all the code for your reference.

The first step is to define a delegate updateTxt.

// Create a delegate to access the TextBox Control.

Public delegate void UpdateTxt (string msg );

// Define a delegate variable

Public UpdateTxt updateTxt;

It mainly uses a delegate to update the richTextBox1 interface.

The instance method is as follows:

Private void Form1_Load_1 (object sender, EventArgs e)

{

// Instantiate the delegate

UpdateTxt = new UpdateTxt (UpdateTxtMethod );

}

The UpdateTxtMethod method is as follows:

// Modify the TextBox value.

Public void UpdateTxtMethod (string msg)

{

RichTextBox1.AppendText (msg + "rn ");

RichTextBox1.ScrollToCaret ();

}

Next we will define a loop to output a value, and call this delegate to update richTextBox1

// This is the call method in a non-creation thread. It actually uses the TextBox Invoke method.

Public void ThreadMethodTxt (int n)

{

This. BeginInvoke (updateTxt, "the thread starts to execute, execute" + n + "times, once every second ");

For (int I = 0; I <n; I ++)

{

This. BeginInvoke (updateTxt, I. ToString ());

// One-second execution

Thread. Sleep (1000 );

}

This. BeginInvoke (updateTxt, "end of thread ");

}

Then a subthread is used to call it.

// Enable the thread

Private void button#click (object sender, EventArgs e)

{

Thread objThread = new Thread (new ThreadStart (delegate

{

ThreadMethodTxt (Convert. ToInt32 (textBox1.Text. Trim ()));

}));

ObjThread. Start ();

}

Okay, that's basically enough.

That's where the problem is.

This. BeginInvoke (updateTxt, "end of thread ");

You may have discovered that I wrote it like this, not

UpdateTxt ("End of thread ");

In this way, it is used directly in the Child thread,

I believe that many of my comrades write this statement. In fact, the error is true.

If you directly use

UpdateTxt ("End of thread ");

You should understand it after thinking about it,

UpdateTxt is created in the main thread, which is directly used in the child thread. If there is more running data, the interface information becomes stuck,

Therefore, even the delegate cannot be used directly in the Child thread, but the BeginInvoke method must be used to call the delegate.

In this way, there will be no stuck images.

The problem is solved.

Support

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.