Click here to Skip to main content
15,897,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,

In my latest c sharp application, I use a progress bar to demonstrate the progression of a task, but the problem is during the progression I can not access other buttons or close window like tasks until the progress bar reaches the maximum value. plz help

thanks
Posted

From the description your giving I suspect you don't use threads.
Without them your GUI will be locked for the duration of the progress so if you still want your GUI to respond you'll have to use threading.
 
Share this answer
 
Technically, you don't have to create a new thread. Instead, you can call Application.DoEvents() to force window messages (mouse clicks, key presses, and so on) to be processed. That would go something like this:
C#
// On a form with 2 buttons and a progress bar.
public partial class Form1 : Form
{
	bool closing = false;
	public Form1()
	{
		InitializeComponent();
	}
	private void button1_Click(object sender, EventArgs e)
	{
		for (int i = 0; i < 100; i++)
		{
			if (closing) break;
			progressBar1.Value = i;
			System.Threading.Thread.Sleep(100);
			Application.DoEvents();
		}
		if (!closing) MessageBox.Show("Done!");
	}
	private void button2_Click(object sender, EventArgs e)
	{
		MessageBox.Show("Did it");
	}
	private void Form1_FormClosing(object sender, FormClosingEventArgs e)
	{
		closing = true;
	}
}

However, I would recommend you learn how to use threads instead.
 
Share this answer
 
Application.DoEvents() is EVIL because it causes re-entrancy. Do NOT use it!

This might help: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx[^].

Nick
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900