C # a simple example of progress bar for neutron thread control

Source: Internet
Author: User

This is a question from the community. The Code will be retained for future response.

Using
System;

Using
System. componentmodel;

Using
System. Windows. forms;

Namespace
Windowsapplication4

...
{


/**/
///
 
<Summary>



///
Gui


///
 
</Summary>



Public
 
Partial
 
Class
Form1: Form


...
{


Public
Form1 ()


...
{

Initializecomponent ();

}




Private
 
Void
Button#click (
Object
Sender, eventargs E)


...
{


//
Working with subthreads



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 ();

}




//
Synchronously update the UI



Void
Downloader_ondownloadprogress (
Long
Total,
Long
Current)


...
{


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


...
{


//
Delegate



Public
 
Delegate
 
Void
Ddownloadprogress (
Long
Total,
Long
Current );


//
Event



Public
 
Event
Ddownloadprogress ondownloadprogress;


//
Start Simulation



Public
 
Void
Start ()


...
{


For
(
Int
I
=
 
0
; I
<
 
100
; I
++
)


...
{


If
(Ondownloadprogress
! =
 
Null
)

Ondownloadprogress (
100
, 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.