CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Threaded View

  1. #1
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    OnAskForProgressReport - BackgroundWorker

    I have been using BackgroundWorker components recently. I have a long process and through OnReportProgress, every now and again it lets my foreground thread (Form) know how it's doing...

    In some scenarios though, my User may get impatient and demand the BackgroundWorker tell them how it is doing right that moment. They want a [Tell Me!] button.

    Assuming I have a component defined as follows:
    Code:
    public class BackgroundMoveFinder : BackgroundWorker
    {
        //
    }
    What techniques are there for communicating from the foreground thread to the background worker?

    Is this good? What other options are there?

    Code:
    public class BackgroundMoveFinder : BackgroundWorker
    {
        private Boolean _sendProgress = false;
    
        public void TellMe()
        {
            lock (_sendProgress )
            {
                _sendProgress = true;
            }
        }
    
        // then regularly in my RunWorkerAsync...
    
        Boolean fireEvent = false;
    
        lock (_sendProgress )
        {
            if (_sendProgress )
            {
                _sendProgress = false;
                fireEvent = true;
            }
        }
    
        if (fireEvent)
            OnReportProgress(....);
    
        ...
    }
    
    // then call, from the foreground thread
    myMoveFinder.TellMe();
    Also, I'm not 100% sure if that's the best way to lock or even whether locking is required in this case?
    Last edited by rliq; April 30th, 2010 at 01:40 AM.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

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

Featured