C # progress bar (backgroudworker)

Source: Internet
Author: User

  C # progress bar (backgroudworker)

UsingSystem;

UsingSystem. Collections. Generic;

UsingSystem. componentmodel;

UsingSystem. Data;

UsingSystem. drawing;

UsingSystem. text;

UsingSystem. Windows. forms;

Namespace Progress bar 2

{

Public Partial Class Form1:Form

{

Private BackgroundworkerWorker =New Backgroundworker();

IntN = 0;

PublicForm1 ()

{

Initializecomponent ();

Worker. workerreportsprogress =True;

Worker. workersuppscanscancellation =True;

Worker. dowork + =New Doworkeventhandler(Dowork );

Worker. progresschanged + =New Progresschangedeventhandler(Progesschanged );

Worker. runworkercompleted + =New Runworkercompletedeventhandler(Completework );

}

Public VoidDowork (ObjectSender,DoworkeventargsE)

{

E. Result = computefibonacci (worker, e );

}

Public VoidProgesschanged (ObjectSender,ProgresschangedeventargsE)

{

Progressbarx1.value = E. progresspercentage;

IntV = (Int) (E. progresspercentage/N );

Progressbarx1.text =Convert. Tostring (v) +"%";

}

Public VoidCompletework (ObjectSender,RunworkercompletedeventargsE)

{

Progressbarx1.text ="Processed! ";

}

Private IntComputefibonacci (ObjectSender,DoworkeventargsE)

{

For(IntI = 0; I <= 92800; I ++)

{

If(Worker. cancellationpending)

{

E. Cancel =True;

Return-1;

}

Else

{

Worker. reportprogress (I );//Raise the progesschanged event

}

}

Return-1;

}

Private VoidBtnstart_click (ObjectSender,EventargsE)

{

N = 92800/100;

Progressbarriers 1.maximum = 92800;

Worker. runworkerasync ();//Start background operations

}

Private VoidBtnpause_click (ObjectSender,EventargsE)

{

Worker. cancelasync ();//Request to suspend background operations

}

 }

}

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.