本文整理汇总了C#中Job.ProcessUrls方法的典型用法代码示例。如果您正苦于以下问题:C# Job.ProcessUrls方法的具体用法?C# Job.ProcessUrls怎么用?C# Job.ProcessUrls使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Job
的用法示例。
在下文中一共展示了Job.ProcessUrls方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: StartButton_Click
private void StartButton_Click(object sender, RoutedEventArgs e)
{
if (!running)
{
var timeLimited = false;
TimeSpan duration = default(TimeSpan);
int runs = 0;
var threads = Convert.ToInt32(Threads.SelectionBoxItem);
var durationText = (string)((ComboBoxItem)Duration.SelectedItem).Content;
StatusProgressbar.IsIndeterminate = false;
switch (durationText)
{
case "1 run":
runs = 1;
break;
case "10 runs":
runs = 10;
break;
case "100 runs":
runs = 100;
break;
case "10 seconds":
duration = TimeSpan.FromSeconds(10);
timeLimited = true;
break;
case "20 seconds":
duration = TimeSpan.FromSeconds(20);
timeLimited = true;
break;
case "1 minute":
duration = TimeSpan.FromMinutes(1);
timeLimited = true;
break;
case "10 minutes":
duration = TimeSpan.FromMinutes(10);
timeLimited = true;
break;
case "1 hour":
duration = TimeSpan.FromHours(1);
timeLimited = true;
break;
case "Until canceled":
duration = TimeSpan.MaxValue;
timeLimited = true;
StatusProgressbar.IsIndeterminate = true;
break;
}
var urls = Regex.Split(Urls.Text, "\r\n").Where(u => !string.IsNullOrWhiteSpace(u)).Select(u => u.Trim());
if (!urls.Any())
return;
Threads.IsEnabled = false;
Duration.IsEnabled = false;
Urls.IsEnabled = false;
cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;
var job = new Job<UrlResult>();
StatusProgressbar.Value = 0;
StatusProgressbar.Visibility = Visibility.Visible;
job.OnProgress += OnProgress;
if (timeLimited)
task = Task.Run(() => job.ProcessUrls(threads, duration, urls, cancellationToken));
else
task = Task.Run(() => job.ProcessUrls(threads, runs, urls, cancellationToken));
var awaiter = task.GetAwaiter();
awaiter.OnCompleted(JobCompleted);
StartButton.Content = "Cancel";
running = true;
}
else
{
if (cancellationTokenSource != null && !cancellationTokenSource.IsCancellationRequested)
cancellationTokenSource.Cancel();
}
}
示例2: StartButton_Click
private void StartButton_Click(object sender, RoutedEventArgs e)
{
if (!running)
{
var timeLimited = false;
TimeSpan duration = default(TimeSpan);
int runs = 0;
var threads = Convert.ToInt32(Threads.SelectionBoxItem);
var durationText = (string)((ComboBoxItem)Duration.SelectedItem).Content;
StatusProgressbar.IsIndeterminate = false;
switch (durationText)
{
case "1 run":
runs = 1;
break;
case "10 runs":
runs = 10;
break;
case "100 runs":
runs = 100;
break;
case "10 seconds":
duration = TimeSpan.FromSeconds(10);
timeLimited = true;
break;
case "20 seconds":
duration = TimeSpan.FromSeconds(20);
timeLimited = true;
break;
case "1 minute":
duration = TimeSpan.FromMinutes(1);
timeLimited = true;
break;
case "10 minutes":
duration = TimeSpan.FromMinutes(10);
timeLimited = true;
break;
case "1 hour":
duration = TimeSpan.FromHours(1);
timeLimited = true;
break;
case "Until canceled":
duration = TimeSpan.MaxValue;
timeLimited = true;
StatusProgressbar.IsIndeterminate = true;
break;
}
var urls = Regex.Split(Urls.Text, "\r\n").Where(u => !string.IsNullOrWhiteSpace(u)).Select(u => u.Trim());
if (!urls.Any())
return;
if (!timeLimited)
{
duration = TimeSpan.FromSeconds(0);
}
Threads.IsEnabled = false;
Duration.IsEnabled = false;
Urls.IsEnabled = false;
cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;
var job = new Job<UrlResult>();
StatusProgressbar.Value = 0;
StatusProgressbar.Visibility = Visibility.Visible;
job.OnProgress += OnProgress;
//MemoryStream argument_stream = new MemoryStream();
//DataContractJsonSerializer argument_serializer =
// new DataContractJsonSerializer(typeof(InvocationArgs));
//argument_serializer.WriteObject(argument_stream, new InvocationArgs() { threads = threads, runs = runs, duration = duration });
//task = Task.Run(() => job.ProcessUrls((Stream)argument_stream, urls, cancellationToken));
string argument_string = JsonConvert.SerializeObject(new InvocationArgs() { threads = threads, runs = runs, duration = duration });
task = Task.Run(() => job.ProcessUrls(argument_string, urls, cancellationToken));
// TaskAwaiter Structure
System.Runtime.CompilerServices.TaskAwaiter<JobResult<UrlResult>> awaiter = task.GetAwaiter();
awaiter.OnCompleted(JobCompleted);
StartButton.Content = "Cancel";
running = true;
}
else
{
if (cancellationTokenSource != null && !cancellationTokenSource.IsCancellationRequested)
cancellationTokenSource.Cancel();
}
}