C # multithreaded asynchronous load formCategory: C #2014-05-28 16:57 1286 people read comments (0) favorite reports Asynchronous loading
When we are programming with windowform, we may be able to do something else when the form is loaded: 1. If the entire interface is slow before loading, and if it takes a long time, the page will come out long enough, 2. If it is written after loading , although the interface came out, this time, there appeared the body of suspended animation. But do not mess up, a little, there is a phenomenon of card death. Now we use async, and multithreading to control, and add the progress bar. To do an interface load when there is a lot of action. Example:
[CSharp]View Plaincopyprint?
- Using System.Windows.Forms;
- Using System.Threading;
- Using System;
- Namespace Threaddemo1
- {
- Public partial class Form1:form
- {
- Public Form1 ()
- {
- InitializeComponent ();
- }
- delegate void AA (string s); Create a proxy
- private void Form1_Load (object sender, EventArgs e)
- {
- Thread newthread = new Thread (new ThreadStart (Ttread));
- Newthread. Start ();
- }
- void Ttread ()
- {
- PRI ("77");
- }
- private delegate void progressbarshow (int i);
- private void pri (string p)
- {
- bool name = richtextbox1.invokerequired;
- //messagebox.show (name. ToString ());
- if (!richtextbox1.invokerequired)//Determine if a request for wake-up is required, if the control is in a thread with the main threads, it can be written as if (! invokerequired)
- {
- Loadthread ();
- }
- Else
- {
- //MessageBox.Show ("Not the same thread");
- AA A1 = new AA (PRI);
- Invoke (A1, new object[] {p}); To perform a wake-up operation
- }
- }
- private void Loadthread ()
- {
- string s = string. Empty;
- For (int b = 0; b < 10000; b++)
- {
- This . Showpro (b);
- s + = b.tostring ();
- }
- //messagebox.show ("Within the same thread");
- Loadrichebox (s);
- }
- void Send () {
- Thread.CurrentThread.Abort ();
- }
- private void Loadrichebox (string s)
- {
- richTextBox1.Text = s;
- }
- private void Showpro (int value)
- {
- if (this. invokerequired)
- {
- This . Invoke (new Progressbarshow (Showpro), value);
- }
- Else
- {
- this.progressBar1.Value = Value;
- This.label1.Text = value + "% processing ...";
- }
- }
- }
- }
C # multithreaded asynchronous load form