C#以多线程selenium窗口形式处理用户输入

时间:2016-11-16 16:57:43

标签: c# multithreading selenium webdriver

在功能' processAccounts'我想用用户输入动态创建一个url字符串。我绝对不知道从哪里开始。我确实尝试了一些对话框但是使用某种不会冻结不等待用户输入的线程的队列会更加清晰。

    private void processAccounts(object currentAccount)
    {
        ChromeOptions options = new ChromeOptions();
        options.AddArgument("ignore-certificate-errors");
        var chromeDriverService = ChromeDriverService.CreateDefaultService();
        chromeDriverService.HideCommandPromptWindow = true;

        ChromeDriver x = new ChromeDriver(chromeDriverService, options);
        x.Navigate().GoToUrl("https://www.google.com/");

        string input = ""; // how do I take input dynamically here?
        x.Navigate().GoToUrl("https://www.google.com/search?as_q=" + input);
    }



    private void goClick(object sender, EventArgs e)
    {

        string[] accountList = { "acc1:pass1", "acc2:pass2", "acc2:pass2", "acc3:pass3", "acc4:pass4", "acc5:pass5", "acc6:pass6", "acc7:pass7", "acc8:pass8", "acc9:pass9" };

        CancellationTokenSource tokenSource = new CancellationTokenSource();
        CancellationToken ct = tokenSource.Token;

        Task task = Task.Factory.StartNew(delegate
        {
            ct.ThrowIfCancellationRequested();

            Parallel.ForEach(accountList, new ParallelOptions { MaxDegreeOfParallelism = 3 }, currentAccount =>
            {
                if (ct.IsCancellationRequested)
                {
                    ct.ThrowIfCancellationRequested();
                }

                processAccounts(currentAccount);

                BeginInvoke((Action)(() =>
                {
                    WriteProgress(currentAccount);
                }));
            });
        }, tokenSource.Token);

        task.ContinueWith((t) =>
                BeginInvoke((Action)(() =>
                {
                    SignalCompletion();
                }))
        );
    }


    void WriteProgress(string fileName)
    {
        progressBar.Visible = true;
        progressBar.Value += 1;
    }

    private void SignalCompletion()
    {
                lblFinished.Text = "done";
    }

0 个答案:

没有答案