當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。