I am working on this example, but I am unable to mesh the callback and the threading.
What I want is this.
1) Press button
2) Start the progress bar running
3) Call to a new thread to perform some long running process
4) A callback on the long running process should trigger the progress bar to stop.
Below I have something...Although the callback parameter for DoSomethingInThread comes in as null. The StopProgressBar() acts on the ProgressBar control, so it cannot be static.
static bool done;
static readonly object locker = new object();
static ParameterizedThreadStart threadStarter = new ParameterizedThreadStart(DoSomethingInThread);
private Thread workerThread = new Thread(threadStarter);
public delegate void StopProgressBarCallback()
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
StartProgressBar();
workerThread.Start();
}
static void DoSomethingInThread(object callback)
{
StopProgressBarCallback stopper = callback as StopProgressBarCallback;
lock (locker)
{
Thread.Sleep(5 * 1000);
}
stopper();
}
private void StartProgressBar()
{
progressBar1.MarqueeAnimationSpeed = 30;
progressBar1.Style = ProgressBarStyle.Marquee;
}
public void StopProgressBar()
{
progressBar1.Style = ProgressBarStyle.Continuous;
}
Have a look at the backgroundworker class, it is more suitable for what you are trying to do and a whole lot easier to get to grips with!
Generally, you shouldn't ever be 'newing' up Thread instances. It is better to use the thread pool, a background worker or if you're on .net 4, a task object from the thread parallel library.