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

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.

THANKS A LOT LizR!!!

someone has to mark this thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.