About multithreaded execution show progress bar instance && inter-thread operation is invalid: Access it from a thread that is not creating the control "Rtxtentryno".

Source: Internet
Author: User

About multi-threaded execution shows an instance of the progress bar!

Before answering an article about how to operate the progress bar in the thread, it is estimated that some people do not see very clearly
Today is nothing, wrote a small example, very simple, on 2 files
The right to be a catalyst for better solutions to the people to express their views

2 controls on the interface, a button and a progress bar
Using System;
Using System.Threading;
Using System.Windows.Forms;

Namespace WindowsFormsApplication2
{
public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
}
The delegate that is called when the thread starts
Private delegate void maxvaluedelegate (int maxValue);
Delegate that is called in thread execution
Private delegate void nowvaluedelegate (int nowvalue);

private void Button1_Click (object sender, EventArgs e)
{
Threadmethod method = new Threadmethod ();
Subscribe to the event first
Method.threadstartevent + = new EventHandler (method_threadstartevent);
Method.threadevent + = new EventHandler (method_threadevent);
Method.threadendevent + = new EventHandler (method_threadendevent);

Thread thread = new Thread (new ThreadStart (Method.runmethod));
Thread. Start ();
}

<summary>
Thread Completion Events
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
void Method_threadendevent (object sender, EventArgs e)
{
MessageBox.Show ("I'm done!");
}

<summary>
Events in thread execution, setting progress bar current Progress
But I can't manipulate the progress bar directly, I need a delegate to do it for me.
</summary>
<param Name= The current value passed in the >threadmethod function of "sender" </param>
<param name= "E" ></param>
void Method_threadevent (object sender, EventArgs e)
{
int nowvalue = Convert.ToInt32 (sender);
Nowvaluedelegate now = new Nowvaluedelegate (Setnow);
This. Invoke (now, nowvalue);
}

<summary>
Thread Start event, setting the maximum bar value
But I can't manipulate the progress bar directly, I need a delegate to do it for me.
</summary>
<param name= The maximum value passed in the >threadmethod function of "sender" </param>
<param name= "E" ></param>
void Method_threadstartevent (object sender, EventArgs e)
{
int maxValue = Convert.ToInt32 (sender);
Maxvaluedelegate max = new Maxvaluedelegate (Setmax);
This. Invoke (max, maxValue);
}

<summary>
I was commissioned to call, specifically set the maximum value of the progress bar
</summary>
<param name= "MaxValue" ></param>
private void Setmax (int maxValue)
{
This.progressBar1.Maximum = MaxValue;
}

<summary>
I was commissioned to set the current value of the progress bar specifically
</summary>
<param name= "Nowvalue" ></param>
private void Setnow (int nowvalue)
{
This.progressBar1.Value = Nowvalue;
}
}
}

A different file

Using System;
Using System.Threading;

Namespace WindowsFormsApplication2
{
<summary>
Thread Handling methods
</summary>
public class Threadmethod
{
<summary>
Thread Start Event
</summary>
public event EventHandler Threadstartevent;
<summary>
Thread execution-Time events
</summary>
public event EventHandler Threadevent;
<summary>
Thread End Event
</summary>
public event EventHandler Threadendevent;

public void Runmethod ()
{
int count = 100; How many times do you execute
Threadstartevent.invoke (count, New EventArgs ());//notify the main interface, I started, count is used to set the maximum value of the progress bar
for (int i = 0; i < count; i++)
{
Thread.Sleep (100);//Rest 100 milliseconds, simulate the execution of large data volume operations
Threadevent.invoke (i, New EventArgs ());//notify the main interface I am executing, I represents the progress bar current progress
}
Threadendevent.invoke (New Object (), new EventArgs ());//notify the main interface I'm done.
}
}
}

For "Inter-thread operation invalid: Access it from a thread that is not creating the control" Rtxtentryno ". "Problems can be resolved with a delegate such as Invoke:

Delegate that is called when a thread is running
Private delegate void nowvaluedelegate (int nowvalue);
Private delegate void Nowstatusdelegate (string nowstatus);

The delegate that is called when the thread starts
Private delegate void Startdelegate ();
Private delegate void maxvaluedelegate (int maxValue);

The delegate that is called when the thread ends
Private delegate void Enddelegate ();

//<summary>
//is delegated, specifically sets the current value of the progress bar
//</summary>
//<param name= "Nowvalue" ></ Param>
private void Setnow (int nowvalue)
{
This.progressBar1.Value = nowvalue;
Lblcurrentamount.text = "currently compared to" + Nowvalue + "bar data";
}

<summary>
Called by the delegate to set the maximum value of the progress bar
</summary>
<param name= "Nowvalue" ></param>
private void Settotal (int total)
{
This.progressBar1.Maximum = total;
This.lblTotal.Text = "All" + Total + "bar data";
}
<summary>
Called by the delegate to set the current state specifically
</summary>
<param name= "Nowvalue" ></param>
private void SetStatus (string nowstatus)
{
This.lblCurrentStatus.Text = Nowstatus;
}
<summary>
The method that is called by the delegate when the thread finishes executing
</summary>
<param name= "Nowvalue" ></param>
private void Endthread ()
{
This.panel1.Visible = false;
}
<summary>
The method that is called when the thread starts
</summary>
<param name= "Nowvalue" ></param>
private void Startthread ()
{
This.panel1.Visible = true;
Lblcurrentstatus.text = "querying data ...";
}

Delegate that is called when the process starts
Startdelegate startthread=new startdelegate (startthread);
This. Invoke (Startthread);

Delegate set progress current status
Nowstatusdelegate Status0 = new Nowstatusdelegate (setStatus);
This. Invoke (STATUS0, "querying data ...");
Application.doevents (); In the loop body, even if you change the relevant values of the progress bar can not be refreshed (Message Queuing is sent out together after the end of the loop), so the use of application.doevents (), implementation of real-time delivery.

Delegate set progress current status
Nowstatusdelegate status = new Nowstatusdelegate (setStatus);
This. Invoke (status, "being compared to data ...");
Application.doevents ();

About multithreaded execution display progress bar instance (GO) && Inter-thread operation is invalid: Access it from a thread that is not creating the control "Rtxtentryno".

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.