当前位置: 首页>>代码示例>>C#>>正文


C# BackgroundWorker.Execute方法代码示例

本文整理汇总了C#中BackgroundWorker.Execute方法的典型用法代码示例。如果您正苦于以下问题:C# BackgroundWorker.Execute方法的具体用法?C# BackgroundWorker.Execute怎么用?C# BackgroundWorker.Execute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在BackgroundWorker的用法示例。


在下文中一共展示了BackgroundWorker.Execute方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TestCallDoWorkOnBackgroundThreadAndThenCallOnSuccessOnMainThread

 public void TestCallDoWorkOnBackgroundThreadAndThenCallOnSuccessOnMainThread()
 {
     var strategy = new MockBackgroundWorkerStrategy();
     var operation = new BackgroundWorker<object>(strategy);
     operation.Execute();
     strategy.CheckThatOnDoWorkWasCalled();
 }
开发者ID:d20021,项目名称:LiveSDK-for-Windows,代码行数:7,代码来源:BackgroundWorkerTest.cs

示例2: downloadPodcastClicked

        /// <summary>
        /// Downloads the podcast clicked.
        /// </summary>
        /// <param name='sender'>Sender.</param>
        /// <param name='e'>E.</param>
        private void downloadPodcastClicked(object sender, EventArgs e)
        {
            Episode myEpisode = EpisodeList.SelectedEpisode;
              // instantiate it within the onCreate method
              ProgressDialog downloadProgress = new ProgressDialog(this);
              downloadProgress.SetMessage(myEpisode.PodcastTitle);
              downloadProgress.Indeterminate = false;
              downloadProgress.SetTitle("Downloading Episode");
              downloadProgress.Max = 100;
              downloadProgress.SetProgressStyle(ProgressDialogStyle.Horizontal);

              long fileLength = 0;

              // create a background worker to download everything
              BackgroundWorker downloadInBackground = new BackgroundWorker(delegate(ref bool stop){
            RunOnUiThread(() => downloadProgress.Show());
            // we will read data via the response stream
            string outputPath = PortaPodderDataSource.GetEpisodeLocation(myEpisode);

            // check to see if the output path
            if(File.Exists(outputPath)) {
              return;
            }

            Directory.CreateDirectory(Path.GetDirectoryName(outputPath));

            // prepare the web page we will be asking for
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(myEpisode.Url.ToString());

            // execute the request
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            fileLength = response.ContentLength;
            downloadProgress.Max = (int)(fileLength / 1024);
            // used on each read operation
            byte[] buf = new byte[1024 * 20];

            using(System.IO.Stream resStream = response.GetResponseStream(), output = new FileStream(outputPath, FileMode.CreateNew, FileAccess.Write, FileShare.None, buf.Length)) {
              int count = 0;
              long total = 0;

              do {
            // check for the stop condition
            if(stop){
              return;
            }

            // fill the buffer with data
            count = resStream.Read(buf, 0, buf.Length);

            // make sure we read some data
            if(count != 0) {
              total += count;
              downloadProgress.Progress = (int)(total / 1024);
              output.Write(buf, 0, count);
            }
              } while (count > 0); // any more data to read?
            }
              });
              downloadInBackground.Completed += delegate(Exception exc){
            RunOnUiThread(() => downloadProgress.Dismiss());
            string outputPath = PortaPodderDataSource.GetEpisodeLocation(myEpisode);

            // log the exception if there was one
            if(exc != null){
              PortaPodderApp.LogMessage(exc);
            }

            // delete the file if the download is incomplete or if there was an error
            if(File.Exists(outputPath) && (new FileInfo(outputPath).Length < fileLength || exc != null)){
              File.Delete(outputPath);
            }

            setGUIForDownloadState(File.Exists(outputPath));
              };

              // if the progress bar is canceled, then the stop signal needs to be given
              downloadProgress.CancelEvent += delegate(object cancelSender, EventArgs cancelEvent) {
            downloadInBackground.Stop = true;
              };
              downloadInBackground.Execute();
        }
开发者ID:brianbourke75,项目名称:PortaPodder,代码行数:86,代码来源:EpisodeDetails.cs


注:本文中的BackgroundWorker.Execute方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。