-
background worker
Hey,
I'm using a background worker.
In the DoWork event, I call my routine that is to be executed.
Within that routine, I call another routine in my application at which point the program ends.
Am I not allowed to call another routine from within the DoWork routine?
is that the limitation of the background worker?
Thanks,
-
Re: background worker
In these routines, are you accessing any objects which were actually created on the main UI thread?
Unless the object is thread safe, if it was created on the UI thread, it should be left there and not touched. If you need to touch some object in the UI thread, you should set your WorkerReportsProgress property to true on the BGWorker, and call the ReportProgress method of the BGWorker in the DoWork thread when you want to alert the UI thread that the BGWorker hit a certain point in its operation. The DoWork routine is not thread safe, but the ProgressChanged event is.
When I say UI thread, I just mean anything on the main thread of the app.
-
Re: background worker
I do have that propert set to true (WorkerReportsProgress)
This is the dowork code
Code:
void _worker_DoWork(object sender, DoWorkEventArgs e)
{
Manager.ProcessBatch(_worker, DataItems, Action, BatchId);
if (_worker.CancellationPending)
{
e.Cancel = true;
}
}
then within ProcessBatch I have a switch statement and call 1 of 2 routines based on the parameter passed.
Code:
internal static void ProcessBatch(System.ComponentModel.BackgroundWorker worker, DataItemCollection items, actionType action, Guid batchId)
{
switch (action.BatchMode)
{
case BatchMode.MailMerge:
ProcessMergeBatch(worker, items, action, batchId);
break;
case BatchMode.SingleDocument:
ProcessSingleDocumentBatch(worker, items,action, batchId);
break;
}
}
That is when the program ends. (either ProcessMergeBatch or ProcessSingleDocumentBatch)
Thanks,
-
Re: background worker
tisk tisk, should be in the C# forum ;)
Anyway, this line of code:
Manager.ProcessBatch(_worker, DataItems, Action, BatchId);
where do you define manager? Where do you define DataItems, Action, and BatchID?
Some of those objects (like integers and what not) may be thread safe (look up a given type on MSDN help and there should be a section for thread safety and it will tell you if it is or not) while I am sure others are not. Why are you passing the background worker into the processbatch routine also?
-
Re: background worker
oops, sorry, I completely forgot this is in C# code.
Manager is simply a class with bunch of static method.
I pass in worker in order to be able to report progress in the process routines.
DataItems is a user type and not built in type.
-
Re: background worker
So DataItems is a structure? Are all the structures elements thread safe?
I guess I can only suggest setting a break point and trying to see where things go bad. You should be able to report progress by passing the worker in like that, so I don't think that is an issue.
-
Re: background worker
Your check for cancellation is useless because, by the time it's made, the entire background operation is complete anyway. For it to be worthwhile you'd have to be checking for cancellation in the methods that are actually doing the work. Note that, even then, each synchronous operation will have to complete before you can test for cancellation. If you call a synchronous operation on a thread then that thread can do nothing else, including check for cancellation, until the synchronous operation completes.
As to your issue, kleinma is quite right. Debugging a multi-threaded app employs the very same techniques as debugging a single threaded app. Synchronising multiple threads can be a tricky proposition while debugging but that's not an issue in this case. You simply place a break point and step through the code, exactly as you would for any other app.
-
Re: background worker
I did go through it line by line, as soon as I get to the line that calls the procedure, the program ends.
Also, DataItems is a class with 2 integer members.
-
Re: background worker
no error or anything? Just ends?
-
Re: background worker
yeah, that's the weird part. it just ends.