Hi All,
I've been programming C# for a while now (I'm a Computer Science major), and have never had to implement threading.
I am currently building an application for a client that requires threading for a series of operations.
Due to the nature of the client/provider agreement, I cannot release the source I am working with. The code that I have posted is in a general form. Pretty much the basic idea of what I am implementing, excluding the content specific source.
The first Snippet demonstrates my basic structure,
The Progress class is a custom progress window that is displayed as a dialog to prevent user UI interaction.
I am currently using the code to complete database calls based on a collection of objects in another area of the application code.
For example, I have a collection of "Objects" of one of my custom classes, that I perform these database calls on or on behalf of. My current set up works just fine when I call the "GeneralizedFunction" one and only one time.
What I need to do is call this function once for every object in the collection. I was attempting to use a foreach loop to iterate through the collection, then I tried a for loop.
For both loops, the result was the same. The Async operation performs exactly as desired for the first item in the collection. This, however, is the only one it works for. For each subsequent item, the progress box (my custom window) displays and immediately closes.
In terms of the database calls, the calls for the first item in the collection are the only ones that successfully complete. All others aren't attempted.
I've tried everything that I know and don't know to do, but I just cannot seem to get it to work.
How can I get this to work for my entire collection?
Any and all help is very greatly appreciated.
Progress progress;
BackgroundWorker worker;
// Cancel the current process
private void CancelProcess(object sender, EventArgs e)
{
worker.CancelAsync();
}
// The main process ... "Generalized"
private void GeneralizedFunction()
{
progress = new Progress();
progress.Cancel += CancelProcess;
progress.Owner = this;
Dispatcher pDispatcher = progress.Dispatcher;
worker = new BackgroundWorker();
worker.WorkerSupportsCancellation = true;
object[] workArgs = { arg1, arg2, arg3};
worker.DoWork += delegate(object s, DoWorkEventArgs args)
{
/* All main logic here
*/
foreach(Class ClassObject in ClassObjectCollection)
{
//Some more logic here
UpdateProgressDelegate update = new UpdateProgressDelegate(updateProgress);
pDispatcher.BeginInvoke(update, arg1,arg2,arg3);
Thread.Sleep(1000);
}
};
worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
{
progress.Close();
};
worker.RunWorkerAsync(workArgs);
progress.ShowDialog();
}
public delegate void UpdateProgressDelegate(arg1,arg2,arg3);
private void updateProgress(arg1,arg2,arg3)
{
//Update progress
}