Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How do I know BackgroundWorker work complete?

How do I know BackgroundWorker work complete?

Scheduled Pinned Locked Moved C#
question
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mimimimilaw
    wrote on last edited by
    #1

    I would like to upload file one by one using BackgroundWorker. But it seems the application using multi-thread to uplaod. Is there any methods like Thread.join on backgroundWorker? Thanks, Mimi

    N realJSOPR 2 Replies Last reply
    0
    • M mimimimilaw

      I would like to upload file one by one using BackgroundWorker. But it seems the application using multi-thread to uplaod. Is there any methods like Thread.join on backgroundWorker? Thanks, Mimi

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      Do you want to wait till background worker finishes? Use waithandles. Call WaitOne() when process starts. Handle RunWorkerCompleted event and signal your waithandle.

      All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

      M 1 Reply Last reply
      0
      • N N a v a n e e t h

        Do you want to wait till background worker finishes? Use waithandles. Call WaitOne() when process starts. Handle RunWorkerCompleted event and signal your waithandle.

        All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

        M Offline
        M Offline
        mimimimilaw
        wrote on last edited by
        #3

        can you show me the example how waithandles work with BackgroundWorker. Thanks.

        N 1 Reply Last reply
        0
        • M mimimimilaw

          can you show me the example how waithandles work with BackgroundWorker. Thanks.

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          Implementing waithandles is trivial. Here is a console application.

          class Program{
          static EventWaitHandle handle = new AutoResetEvent(false);
          static void Main(string[] args) {
          BackgroundWorker worker = new BackgroundWorker();
          worker.DoWork += new DoWorkEventHandler(worker_DoWork);
          worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
          worker.RunWorkerAsync();

               Console.WriteLine("Main thread waiting");
               **handle.WaitOne();** // This blocks main thread
               Console.WriteLine("Main thread released.");
               Console.Read();
          }
          
          static void worker\_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
               Console.WriteLine("Worker finished. Releasing main thread");
               **handle.Set();**
          }
          
          static void worker\_DoWork(object sender, DoWorkEventArgs e) {
               Thread.Sleep(1000); // Simulate a long running job
           }
          

          }

          Hope it is clear now

          All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

          1 Reply Last reply
          0
          • M mimimimilaw

            I would like to upload file one by one using BackgroundWorker. But it seems the application using multi-thread to uplaod. Is there any methods like Thread.join on backgroundWorker? Thanks, Mimi

            realJSOPR Offline
            realJSOPR Offline
            realJSOP
            wrote on last edited by
            #5

            That's because a BackgroundWorker is a thread. If you want to use a thread to upload a file (presumably to keep the UI responsive during the upload), a join is pointless. Set your backgroundworker to report its progress, add the appropriate event handler, and add a control that's updated when the backgroundworker reports its progress. If you want the program to not allow input during the upload, don't use a thread (or async upload).

            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups