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


C# Job.ProcessUrls方法代码示例

本文整理汇总了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();
            }
        }
开发者ID:mastoj,项目名称:Netling,代码行数:84,代码来源:MainWindow.xaml.cs

示例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();
            }
        }
开发者ID:sergueik,项目名称:Netling,代码行数:93,代码来源:MainWindow.xaml.cs


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