C # Go To The windowform progress bar

Source: Internet
Author: User

The progress bar is one of the considerations of software humanization. It gives the user the feeling that the internal actions of the program are not stopped, so that the user does not know what the program is doing!

I read several WinForm programs and found that they completely lost the progress bar. They all use Timer for processing. When the thread ends, the progress bar of direct assignment reaches 100%. The progress bar is calculated and updated based on the overall data and after each step of the WebForm program. After reading these WinForm programs, I was wondering: Are all WinForm programs unable to ensure real-time progress display in progress bar processing?

In fact, Timer is used for processing, and constantly updating the progress bar is just a method that programmers are lazy. Of course, the advantage is that the progress bar can be simplified, the code is small, not prone to errors, and debugging is convenient.

Another way is to update the progress bar data in time. That is to say, we adopt an event-driven mechanism to monitor and update the Set events during complicated processing in subthreads in a timely manner! View the Code directly:
Program code using System;
Using System. ComponentModel;
Using System. Windows. Forms;
Namespace WindowsApplication1
{
/// <Summary>
/// Form1 class
/// </Summary>
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
Private void button#click (object sender, EventArgs e)
{
// Work 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 (your complex processing class)
/// </Summary>
Public class Downloader
{
// Delegate
Public delegate void dDownloadProgress (long total, long current );
// Event
Public event dDownloadProgress onDownLoadProgress;
// Start the simulation
Public void Start ()
{
For (int I = 0; I <100; I ++)
{
If (onDownLoadProgress! = Null)
OnDownLoadProgress (100, I );
System. Threading. thread. Sleep (100 );
}
}
}
}
The above article was posted on a blog. I think it is still feasible. I haven't tried it yet. I also saw a reply on the csdn Forum. The content is as follows:

Question about the waiting box displayed during the query statistics process !!!

Hbxtlhx on the first floor (for civilian users-self-built, full-size food) replied to-12-30 11:45:46 score 70

Timer can be executed during asynchronous processing, and timer can be disabled after asynchronous execution.

Because your program is synchronized, other threads of the program are processed before the query statistics are completed and are executed only after the query statistics are completed, therefore, your timer will not be able to achieve the desired effect after the query statistics are completed. top

Hbxtlhx on the second floor (for civilian users-self-built, full-clothing and foot food) replied to 11:51:01,-12-30, with a score of 0
For example, the following code is executed asynchronously:
Delegate object dlexecutequery ();
Private void button#click (Object sender, eventargs E)
{
Dlexecutequery de = new dlexecutequery (this. Query ());
Iasyncresult IR = de. begininvoke (null, null );

Form F = new form ()
F. showdialog (this );
Application. doevents ();
While (! IR. iscompleted)
{
Application. doevents ();
}
Object OBJ = de. endinvoke (IR );
F. Close ();
}

Private object query ()
{
// Long operation
}

 
Another method: conversion from: csdn yuweng Column

Recently I have read many people asking this question. I used to write a blog about how to control the progress bar in the Child thread. However, in most environments, a pop-up window is required to display the progress bar. You only need to make a slight modification on the basis of the previous settings.
The first is the progress bar form. You need to add the progress bar above and remove controlbox. In addition, you need to add a method to control the increase of the progress bar, as shown below:

/// <Summary>

/// Increase process bar

/// </Summary>

/// <Param name = "nvalue"> the value increased </param>

/// <Returns> </returns>

Public bool increase (INT nvalue)

{

If (nvalue> 0)

{

If (prcbar. Value + nvalue <prcbar. Maximum)

{

Prcbar. Value + = nvalue;

Return true;

}

Else

{

PrcBar. Value = prcBar. Maximum;

This. Close ();

Return false;

}

}

Return false;

}
The next step is the main form. To perform operations, you must first define two private members and one delegate. One private member is to save the current progress bar form object, and the other is to save the delegate method (that is, to add the progress bar scale), as follows:

Private frmprocpolicar myProcessBar = null;

Private delegate bool increasehandle (INT nvalue );

Private increasehandle myincrease = NULL;
Then provide a function in the main form to open the progress bar form, as shown below:

/// <Summary>

/// Open process bar window

/// </Summary>

Private void showprocessbar ()

{

Myprocessbar = new frmprocessbar ();
// Init increase event

Myincrease = new increasehandle (myprocessbar. increase );

Myprocessbar. showdialog ();

Myprocessbar = NULL;

}
Now you can start to create a thread for running, as shown below:

/// <Summary>

/// Sub thread function

/// </Summary>

Private void ThreadFun ()

{

MethodInvoker mi = new MethodInvoker (ShowProcessBar );

This. BeginInvoke (mi );
Thread. Sleep (1000); // Sleep a while to show window
Bool blnIncreased = false;

Object objReturn = null;

Do

{

Thread. Sleep (50 );

ObjReturn = this. Invoke (this. myIncrease,

New object [] {2 });

BlnIncreased = (bool) objReturn;

}

While (blnIncreased );

}

Note that when you open the progress bar form and add the progress bar, one is BeginInvoke and the other is Invoke. the difference here is that BeginInvoke does not need to wait for the method to finish running, invoke is waiting for the method to run. Another point is that the return value is used to determine whether the progress bar has reached the header. If you need other controls, you can use the preceding Method for expansion.
The startup thread can be as follows:

Thread thdsub = new thread (New threadstart (threadfun ));

Thdsub. Start ();
In this way, the progress bar form is opened in the mode.

Related Article

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.