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

Threaded View

  1. #1
    Join Date
    May 2003
    Posts
    18

    Question Why the "RunWorkerCompleted" never comes after pressing ENTER?

    Code:
    using System;
    using System.ComponentModel ;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Reflection;
    
    
    namespace Retriever
    {
        class Program
        {
            static void Main(string[] args)
            {
                BackgroundWorker bw = new BackgroundWorker();
                bw.WorkerSupportsCancellation = true;
                
    
                bw.DoWork += new DoWorkEventHandler(bw_DoWork);
                bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
                bw.RunWorkerAsync();
                Console.WriteLine("Press ENTER to exit...");
                Console.ReadLine();
                bw.CancelAsync();
                Thread.Sleep(5000);
            }
    
            static void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                if (e.Cancelled )
                    cls.Suspend ();
            }
    
            static void bw_DoWork(object sender, DoWorkEventArgs e)
            {
     for (int i = 0; i < 10000; i++){ Thread.Sleep(1000); Console.WriteLine(i); if (e.Cancel){e.Result = "done"; return ;}}        }
        }
    }

    If to hit Enter after running, the bw_RunWorkerCompleted() will be never invoked. Why?
    Last edited by senglory; August 4th, 2009 at 07:37 PM. Reason: BackgroundWorker RunWorkerCompleted CancelAsync

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