I have a problem with changing the value of the ProgressBar in a Thread method.
Here is the code.This is a simple windows form with a progress bar and a cancel button to stop the progress.
using
System;using
System.Drawing;using
System.Collections;using
System.Windows.Forms;using
System.Data;using
OpenNETCF;using
OpenNETCF.Threading;using
System.Threading;namespace
ProgressBar{
/// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form{
private System.Windows.Forms.ProgressBar progressBar; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.MainMenu mainMenu1;public bool flag = false;
public int x = 0;
public Form1()
{
p = new Process();
InitializeComponent();
}
............
......
......
static void Main()
{
Application.Run(
new Form1());}
private void button1_Click(object sender, System.EventArgs e){
run();
}
private void button2_Click(object sender, System.EventArgs e){
stop();
}
public void run()
{
ThreadEx t =
new ThreadEx(new ThreadStart(loop)); this.progressBar.Minimum = 0; this.progressBar.Maximum = 50; t.Start();}
public void stop()
{
this.flag = true;}
{
while(!this.flag&&this.x<50){
System.Console.WriteLine("xxxxxxxx");
this
.progressBar.Value++; this.x++;}
}
}
}
When it starts the thread, it will be stuck at the line ' this.progressBar.Value++;' without an error. I have tried to put the adding value statement out of the loop() method, and it works. I don't know where is the problem. Help...
Thanks,
Justin

Thread and ProgressBar
cybercrypt13
Hi.
You should access your controls from the GUI-thread only. This is particularly true in Smart Device applications, since if you crash the .NET Framework on, say a smartphone, it is likely that it needs a hardware reset when your app is done with it.
-- MSDN: http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpref/html/frlrfsystemwindowsformscontrolclassbegininvoketopic1.asp
There are four methods on a control that are safe to call from any thread: Invoke, BeginInvoke, EndInvoke, and CreateGraphics. For all other method calls, you should use one of the invoke methods to marshal the call to the control's thread
-- eoMSDN
I have filed bugs concerning the use of CreateGraphics in .NET 2.0. So I avoid using that too.
I would not trust reading the value of the progressbar in other threads than the GUI-thread on Compact Framework. I would even consider that risky.
http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpguide/html/cpovrasynchronousprogrammingoverview.asp
Beware: developing multithreaded applications on these devices is time-consuming and difficult. Do not assume that things that works in the emulator will work the same way on a device. Testing, testing, testing.
Good luck.
Gorm Braarvig