本文整理汇总了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();
}
示例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();
}