0

can someone help me wit this, why does my form becomes unresponsive when i execute this code:

private Thread trd;
private int y = 0;
delegate void MyDelegate(bool show);

private void button1_Click(object sender, EventArgs e)
{
	trd = new Thread(new ThreadStart(this.ThreadTask));
	trd.IsBackground = true;
	trd.Start();
}

private void ShowProgressBar(bool show)
{
	progressBar1.Visible = show;
	
	int x = 0;
	int stp;
	int newval;
	Random rnd = new Random();

	while (true)
	{
		stp = this.progressBar1.Step * rnd.Next(-1, 2);
		newval = this.progressBar1.Value + stp;

		if (newval > this.progressBar1.Maximum)
			newval = this.progressBar1.Maximum;
		else if (newval < this.progressBar1.Minimum)
			newval = this.progressBar1.Minimum;

		this.progressBar1.Value = newval;

		Thread.Sleep(100);
	}
}
		
private void ThreadTask()
{
	Invoke(new MyDelegate(ShowProgressBar), true);
}
3
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by serkan sendur
0

Because all your thread does is tell the main form to do stuff, and loop - it itself doesnt do the work.

What you should have done was put all the sleep etc in the threadtask, and have an invoke to update the progress bar with a new value.. and only that.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.