Results 1 to 10 of 10

Thread: background worker

  1. #1

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950

    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,
    Don't anthropomorphize computers -- they hate it

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  3. #3

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950

    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,
    Don't anthropomorphize computers -- they hate it

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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?

  5. #5

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950

    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.
    Don't anthropomorphize computers -- they hate it

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950

    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.
    Don't anthropomorphize computers -- they hate it

  9. #9
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: background worker

    no error or anything? Just ends?

  10. #10

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950

    Re: background worker

    yeah, that's the weird part. it just ends.
    Don't anthropomorphize computers -- they hate it

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width