wenliang

my code is listed below:

private void button1_Click(object sender, EventArgs e)
{
myProgressBar.Value = 0;

}

private void button2_Click(object sender, EventArgs e)
{
myProgressBar.Value = 50;
}

private void button3_Click(object sender, EventArgs e)
{
myProgressBar.Value = 100;
}

private void button4_Click(object sender, EventArgs e)
{
//thread start
FormTest FormTestobject = new FormTest();
Thread mythreadtest = new Thread(FormTestobject.threadtest);
// 上 程
mythreadtest.Start();
}

private void threadtest()
{
myProgressBar.Value = 0;

for (int i = 0; i < 10; i++)
{
Thread.Sleep(1000);
myProgressBar.Value = i*10;
}

myProgressBar.Value = 100;

}

all the three function to set value of progressbar seperately can work well.but why i start a thread and make it refresh the value doesn't seem to have any work on progress bar

is there anything wrong with it

thanks!



Re: Windows Forms General why my progressbar doesn't work in thread?

wenliang

my code is listed below:

private void button1_Click(object sender, EventArgs e)
{
myProgressBar.Value = 0;

}

private void button2_Click(object sender, EventArgs e)
{
myProgressBar.Value = 50;
}

private void button3_Click(object sender, EventArgs e)
{
myProgressBar.Value = 100;
}

private void button4_Click(object sender, EventArgs e)
{
//thread start
FormTest FormTestobject = new FormTest();
Thread mythreadtest = new Thread(FormTestobject.threadtest);
mythreadtest.Start();
}

private void threadtest()
{
myProgressBar.Value = 0;

for (int i = 0; i < 10; i++)
{
Thread.Sleep(1000);
myProgressBar.Value = i*10;
}

myProgressBar.Value = 100;

}

all the three function to set value of progressbar seperately can work well.but why i start a thread and make it refresh the value doesn't seem to have any work on progress bar

is there anything wrong with it

thanks!





Re: Windows Forms General why my progressbar doesn't work in thread?

Ðãv? S. Â???????

You're doing Cross-Thread Violations in your code. You cannot directly access a control from a thread that it was not created on. You need to invoke the control using Control.Invoke(). Below is an example.

Thread thr = new Thread(thr_DoWork);
thr.IsBackground = true;
thr.Start();

private void thr_DoWork()
{
for (int i = 0; i <= 100; i++)
{
progressBar1.Invoke(...);
}
}

Look on MSDN for the full example on invoking a control.





Re: Windows Forms General why my progressbar doesn't work in thread?

wenliang

thanks, but i don't think this is the cause.

because i didn't set the check illegal crossthreadcall property , and i remember that in .net 2.0, the runtime will check this and tell me when i make a cross thread call.

so...





Re: Windows Forms General why my progressbar doesn't work in thread?

wjousts

You can only update the progress bar on the GUI thread that created it. To get around this you should use Control.Invoke.





Re: Windows Forms General why my progressbar doesn't work in thread?

Rong-Chun Zhang - MSFT

Hi wenliang,

Try something like the following:

Code Snippet

delegate void setprocess();

private void button4_Click(object sender, EventArgs e)

{

//thread start

//FormTest FormTestobject = new FormTest();

Thread mythreadtest = new Thread(threadtest);

mythreadtest.Start();

}

private void threadtest()

{

if (this.myProgressBar.InvokeRequired)

{

setprocess pro = new setprocess(threadtest);

this.myProgressBar.Invoke(pro);

}

else

{

myProgressBar.Value = 0;

for (int i = 0; i < 100; i++)

{

Thread.Sleep(1000);

myProgressBar.Value = i;

}

myProgressBar.Value = 100;

}

}

Hope this helps.

Regards






Re: Windows Forms General why my progressbar doesn't work in thread?

wenliang

thank you very much .

i'll try it.





Re: Windows Forms General why my progressbar doesn't work in thread?

wenliang

but your code snippet hang the UI thread.

when i click on the UI, it not corresponding...





Re: Windows Forms General why my progressbar doesn't work in thread?

Rong-Chun Zhang - MSFT

Hi wenliang,

I have tested it, it worked well on my side. Could you please show me the code you have used Thanks.

With regards.






Re: Windows Forms General why my progressbar doesn't work in thread?

wenliang

the code will be as simple as this:

Code Snippet

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Test2
{
public partial class Form1 : Form
{
delegate void setprocess();

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Thread mythreadtest = new Thread(threadtest);
mythreadtest.Start();

}

private void threadtest()
{
if (this.myProgressBar.InvokeRequired)
{
setprocess pro = new setprocess(threadtest);
this.myProgressBar.Invoke(pro);
}
else
{
myProgressBar.Value = 0;
for (int i = 0; i < 100; i++)
{
Thread.Sleep(1000);
myProgressBar.Value = i;
}
myProgressBar.Value = 100;
}
}

}
}

but when the button was clicked,the thread is running ,so as the progress bar. if i click the ui , not matter where. it will not responding and the progress bar stop increasing...

i don't konw why... thanks





Re: Windows Forms General why my progressbar doesn't work in thread?

Rong-Chun Zhang - MSFT

Hi wenliang,

Try the following code:

Code Snippet

delegate void setprocess(int a);

private void button4_Click(object sender, EventArgs e)

{

//thread start

//FormTest FormTestobject = new FormTest();

Thread mythreadtest = new Thread(new ThreadStart(tt));

mythreadtest.Start();

}

private void tt()

{

threadtest(0);

for (int i = 0; i < 100; i++)

{

Thread.Sleep(1000);

threadtest(i);

}

threadtest(100);

}

private void threadtest(int val)

{

if (this.myProgressBar.InvokeRequired)

{

setprocess pro = new setprocess(threadtest);

this.Invoke(pro,val);

}

else

{

this.myProgressBar.Value = val;

}

}

Hope this helps.

Regards






Re: Windows Forms General why my progressbar doesn't work in thread?

Rong-Chun Zhang - MSFT

Hi wenliang,

If you use multithreading to improve the performance your Windows Forms applications, you must be careful to make calls to your controls in a thread-safe way. Like wjousts have said, you should use Control.Invoke(). The link below provides you an sample.

http://msdn2.microsoft.com/en-us/library/ms171728(VS.80).aspx

Hope this helps.

Regards






Re: Windows Forms General why my progressbar doesn't work in thread?

wenliang

it looks like a very simple question.

why make me so ...

thanks ,everybody .