操作<>,等待额外操作完成

时间:2012-12-02 18:06:59

标签: c# .net asynchronous 7zip

here it is:

        Action<int, ProgressBar, Label, Label, int, Button> downloadFileAsync = (i, pb, label2, label1, ServID, button1) =>
    {
        var bd = AppDomain.CurrentDomain.BaseDirectory;
        var fn = bd + "/" + i + ".7z";
        var down = new WebClient();
        DownloadProgressChangedEventHandler dpc = (s, e) =>
        {
            label1.Text = "Download Update: " + i + " From: " + ServID;
            int rec =Convert.ToInt16(e.BytesReceived / 1024);
            int total =Convert.ToInt16(e.TotalBytesToReceive / 1024)  ;
            label2.Text = "Downloaded: " + rec.ToString() + " / " + total.ToString() + " KB";
            pb.Value = e.ProgressPercentage;
        };
        AsyncCompletedEventHandler dfc = null;  dfc = (s, e) =>
        {
            down.DownloadProgressChanged -= dpc;
            down.DownloadFileCompleted -= dfc;
            CompressionEngine.Current.Decoder.DecodeIntoDirectory(AppDomain.CurrentDomain.BaseDirectory + "/" + i + ".7z", AppDomain.CurrentDomain.BaseDirectory);
            File.Delete(fn);
               if (i == ServID)
                {

                   button1.Enabled = true;
                   label1.Text = "Game Is Up-To-Date.Have Fun!!";
                  label2.Text = "Done..";
               }
         down.Dispose();
        };

我现在唯一的问题是程序正在解压缩下载的文件

CompressionEngine.Current.Decoder.DecodeIntoDirectory(AppDomain.CurrentDomain.BaseDirectory + "/" + i + ".7z", AppDomain.CurrentDomain.BaseDirectory);

在某些文件中,需要时间来提取下载的文件 那我怎么能告诉程序要等到解压缩完成呢? (我使用BackgroundWorker)

1 个答案:

答案 0 :(得分:0)

到目前为止,处理此类处理的最优雅方式是使用TPL Dataflow。

http://msdn.microsoft.com/en-us/devlabs/gg585582.aspx

这需要您稍微重构现有代码。但是,它提供了一种出色的机制来表达对它们有秩序感的并行编程任务。如果您以任何频率编写此类代码,都值得花时间学习数据流。

如果这不是一个选项,请查看AutoResetEvent。它将允许一个任务在不消耗资源的情况下等待,直到它所依赖的任务完成为止。

  

通知等待的线程发生了一个事件

http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent.aspx

相关问题