Problem Description:
When our interface needs to constantly update data in the program's operation,
When the data in a TextBox needs to change,
For this question you can first refer to my other article
In order to let the program does not appear in the implementation of the interface card dead image, the best way is multithreading to solve
A main thread creates an interface, uses a child thread to execute the program, and updates the main interface
So there's no such thing as a dead card.
It's certainly not a problem,
But why in the process of using the same there will be a lot of places will appear card death, and there are users to tell me is my httphelper class problem, is not, and I once again declare my Httphelper class and multithreading does not matter. Don't tempted accused me, oh.
This problem is actually also sleepy or I very long, but today finally solved, and I found that many people have such a problem, so I share an example to facilitate the reference bar. Let's take a look at my interface first.
When I click
After starting execution
Is that the data is constantly being updated
The interface is not going to die at this time,
It's just that the data is constantly being updated
Now look at my code.
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 ();
}
Creates a delegate that is serviced for accessing the TextBox control.
public delegate void Updatetxt (string msg);
Define a delegate variable
Public Updatetxt Updatetxt;
The method that modifies the textbox value.
public void Updatetxtmethod (String msg)
{
Richtextbox1.appendtext (msg + "RN");
Richtextbox1.scrolltocaret ();
}
This is a calling method in a non-created thread, which is actually an invoke method that uses a TextBox.
public void Threadmethodtxt (int n)
{
This. BeginInvoke (Updatetxt, "thread begins execution, executes" + N + "times, executes once per second");
for (int i = 0; i < n; i++)
{
This. BeginInvoke (Updatetxt, i.ToString ());
Execute once a second
Thread.Sleep (1000);
}
This. BeginInvoke (Updatetxt, "thread End");
}
Open Thread
private void Button1_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)
{
Instantiating a delegate
Updatetxt = new Updatetxt (Updatetxtmethod);
}
}
}
The above is all the code convenient for everyone to refer to it
The first step is to define a delegate updatetxt
Creates a delegate that is serviced for accessing the TextBox control.
public delegate void Updatetxt (string msg);
Define a delegate variable
Public Updatetxt Updatetxt;
The main is to use a delegate to update the richTextBox1 of the interface
Instance methods are as follows
private void Form1_load_1 (object sender, EventArgs e)
{
Instantiating a delegate
Updatetxt = new Updatetxt (Updatetxtmethod);
}
The Updatetxtmethod method is as follows
The method that modifies the textbox value.
public void Updatetxtmethod (String msg)
{
Richtextbox1.appendtext (msg + "RN");
Richtextbox1.scrolltocaret ();
}
Here we define a loop to output a value, and call this delegate to update the RichTextBox1
This is a calling method in a non-created thread, which is actually an invoke method that uses a TextBox.
public void Threadmethodtxt (int n)
{
This. BeginInvoke (Updatetxt, "thread begins execution, executes" + N + "times, executes once per second");
for (int i = 0; i < n; i++)
{
This. BeginInvoke (Updatetxt, i.ToString ());
Execute once a second
Thread.Sleep (1000);
}
This. BeginInvoke (Updatetxt, "thread End");
}
And then you're using a subroutine to call it.
Open Thread
private void Button1_Click (object sender, EventArgs e)
{
Thread objthread = new Thread (new ThreadStart (delegate
{
Threadmethodtxt (Convert.ToInt32 (TextBox1.Text.Trim ()));
}));
Objthread.start ();
}
All right, that's it, basically.
The question is there now, in fact, in this sentence
This. BeginInvoke (Updatetxt, "thread End");
Everyone may have found out that I was writing this instead of
Updatetxt ("Thread End");
This is used directly in child threads.
I believe a lot of comrades have written this, but the mistake is right here.
If you directly use
Updatetxt ("Thread End");
Think about it and you know it.
Updatetxt is created in the main thread, and we use directly in the child thread, running more data, there will be card death, which is the reason for the interface information blocking death,
So even a delegate cannot be used directly in a child thread, but rather uses the BeginInvoke method to invoke the delegate
So that there is no such thing as a dead card.
The problem is solved.
All right, guys, support.