A simple example of the C # Neutron thread control progress bar

Source: Internet
Author: User

This question comes from a community question, and the code keeps a copy for later answers.

Using System;
Using System.ComponentModel;
Using System.Windows.Forms;
Namespace WindowsApplication4
... {
/**////<summary>
GUI class
</summary>
public partial class Form1:form
... {
Public Form1 ()
... {
InitializeComponent ();
}
private void Button1_Click (object sender, EventArgs e)
... {
Working with child threads
New System.Threading.Thread (New System.Threading.ThreadStart (Startdownload)). Start ();
}
Start download
public void Startdownload ()
... {
Downloader downloader = new Downloader ();
Downloader.ondownloadprogress + = new downloader.ddownloadprogress (downloader_ondownloadprogress);
Downloader. Start ();
}
Synchronizing Update UI
void Downloader_ondownloadprogress (long, long)
... {
if (this. invokerequired)
... {
This. Invoke (New Downloader.ddownloadprogress (downloader_ondownloadprogress), new object[] ... {total, current});
}
Else
... {
This.progressBar1.Maximum = (int) total;
This.progressBar1.Value = (int) current;
}
}
}

/**////<summary>
Download class
</summary>
public Class Downloader
... {
Commissioned
public delegate void Ddownloadprogress (long total,long current);
Event
public event ddownloadprogress Ondownloadprogress;
Start simulation work
public void Start ()
... {
for (int i = 0; i < i++)
... {
if (ondownloadprogress!= null)
Ondownloadprogress (i);
System.Threading.Thread.Sleep (100);
}
}
}
}

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.