Background worker

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • csharpula csharp

    #1

    Background worker

    Hello,
    Can you please tell me if I should use the background worker in the
    following way:
    if (worker.IsBusy)
    worker.CancelAs ync(); (or I can avoid this if)?

    And in case I want to restart the background worker ,should I do it this
    way?
    worker.RunWorke rAsync();

    Thank u !






    *** Sent via Developersdex http://www.developersdex.com ***
  • Marc Gravell

    #2
    Re: Background worker

    Regards the "if(worker.IsBu sy)", I imagine that yes, you can drop the
    if and just call CancelAsync. I say this because if you *had* to test
    IsBusy, you'd already have a race-condition (since the other thread
    could complete between the IsBusy and CancelAsync calls). However,
    including this test doesn't do much harm.

    Regards restarting, that sounds about right - but in this case you
    *do* want to check that it isn't already running since it throws an
    exception if you try to use it twice at once. There is no race
    condition here since it can only become "safer" (if you see what I
    mean).

    Mar

    Comment

    • =?Utf-8?B?UGV0ZXIgUml0Y2hpZSBbQyMgTVZQXQ==?=

      #3
      RE: Background worker

      You can "restart" the background worker by calling RunWorkerAsync, but you
      won't be able to call that until after you receive the RunWorkerComple ted
      event. Until you get that event you'll get an InvalidOperatio nException.

      --
      Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.

      Microsoft MVP, Visual Developer - Visual C#


      "csharpula csharp" wrote:
      Hello,
      Can you please tell me if I should use the background worker in the
      following way:
      if (worker.IsBusy)
      worker.CancelAs ync(); (or I can avoid this if)?
      >
      And in case I want to restart the background worker ,should I do it this
      way?
      worker.RunWorke rAsync();
      >
      Thank u !
      >
      >
      >
      >
      >
      >
      *** Sent via Developersdex http://www.developersdex.com ***
      >

      Comment

      • Creativ

        #4
        Re: Background worker

        A question:

        ThreadPool also provides the async execution of tasks. So when should
        I use ThreadPool and when BackgroundWorke r? What's the difference
        between them?

        Comment

        • Peter Duniho

          #5
          Re: Background worker

          On Mon, 24 Dec 2007 00:42:33 -0800, Creativ <GongXinrong@gm ail.comwrote:
          A question:
          >
          ThreadPool also provides the async execution of tasks. So when should
          I use ThreadPool and when BackgroundWorke r? What's the difference
          between them?
          BackgroundWorke r uses the ThreadPool. It adds to that the ability to deal
          with cross-thread issues, by providing an event-driven model for using
          it. In particular, the notification events like ProgressChanged and
          RunWorkerComple ted will be raised on the same thread that created the
          BackgroundWorke r instance. This means you can use the BackgroundWorke r
          class from a Form instance, creating it on the main GUI thread for the
          Form and not having to worry about using Invoke() or BeginInvoke() to call
          code that accesses the Form instance itself.

          If your background thread code needs to communicate with a GUI object like
          a Form- or Control-derived class, then the BackgroundWorke r class provides
          a nice simplified API to do that.

          Pete

          Comment

          Working...