开发者

How to avoid having a Thread.Sleep(Int32.MaxValue) when waiting for an asynchronous operation to end in a Console app?

I have the following code that will download a file asynchronously to my hard-drive, shouting to the console the current progress and quitting with a goodbye message in the end:

webClient.DownloadProgressChanged.Add(fun args ->
      if (currentPercentage < args.ProgressPercentage) then
        Console.WriteLine(args.ProgressPercentage.ToString() + "%")

      currentPercentage <- args.ProgressPercentage
  )

webClient.DownloadFileCompleted.Add(fun args ->
  Console.WriteLine("Download finished!")
  Environment.Exit 0
)

webClient.DownloadFileAsync(new Uri(url_to_download),  file_name)

Thread.Sleep Int32.MaxValue

I was wondering, though, whether there could be any more elegant way of achieving this without having to resort to "sleeping forever" in the main thread, having the program end though an Environment.Exit(). I have no prejudice towards using Environment.Exit() but I'd like to avoid it, if possible! The only way I can think of avoiding this would be to spawn a new thread and then waiting for it to die, but that does seem cumbersome. An开发者_开发知识库y simpler way to accomplish this?


You can use a ResetEvent like this:

webClient.DownloadProgressChanged += (f,a) => ...
AutoResetEvent resetEvent = new AutoResetEvent(false);
webClient.DownloadFileCompleted += (f, a) => resetEvent.Set();
webClient.DownloadDataAsync(new Uri(url_to_download), file_name);
resetEvent.WaitOne();
Console.WriteLine("Finished");


simply use a waithandle derived class like the mutex to signal you're ready to close down. signal it in your download completed method and wait for it at the end of your app. as it becomes signalled your app will exit naturally.


In case you are a fan of the Reactive extensions library (Rx), then this process can be modeled in terms of observable like:

    public static IObservable<int> DownloadURL(string url,string fname)
    {
        return Observable.Defer(() =>
        {
            var sub = new Subject<int>();
            var wc = new WebClient();
            wc.DownloadProgressChanged += delegate(object sender, DownloadProgressChangedEventArgs e)
            {
                sub.OnNext(e.ProgressPercentage);
                if (e.ProgressPercentage == 100)
                    sub.OnCompleted();
            };
            wc.DownloadFileAsync(new Uri(url), fname);
            return sub;
        });
    }

    public static void Main(string[] str)
    {
        foreach (var i in DownloadURL("http://www.google.com", "g:\\google.html").DistinctUntilChanged().ToEnumerable())
            Console.WriteLine(i);
    }


In C# you could write an extension method for WebClient that waits for the download to complete, while still pitching update events:

static class WebClientEx {
    public static void DownloadSemiSync(this WebClient webClient, Uri address, string filename) {
        var evt = new AutoResetEvent(false);
        webClient.DownloadFileCompleted += (s, e) => evt.Set();
        webClient.DownloadFileAsync(address, filename);
        evt.WaitOne();
    }
}

This'd allow you to define whatever progress event you want on it and then use it as a synchronous function, reducing your main code to this:

    static void Main() {
        var webClient = new WebClient();
        webClient.DownloadProgressChanged += (s, args) => {..};
        webClient.DownloadSemiSync(new Uri("http://.."), "test.bin");
        Console.WriteLine("DownloadFinished");
    }

Throws all the events, but then waits to exit.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜