如何取消线程c#

时间:2017-06-26 01:26:29

标签: c# multithreading

嘿,经过长时间的研究,我想出了一种取消线程的方法,但是当我取消线程时我面临一个问题,这是我的代码首先。例如,当我点击button3而形式工作它杀死过程但它给了我错误(chrome不可更改)

public void test1()
{
var dr = new chromdriver();
dr.navigate().GoToUrl("http://google.com");
if(xx != null)
 {
 IWebElement emal = dr.FindElement(By.XPath("//[@id=\"Email\"]"));
 emal.Sendkeys(email)
 }
 else{
  IWebElement emal = dr.FindElement(By.XPath("//[@id=\"Email\"]"));
 emal.Sendkeys(email)
}
}
 private void button2_Click(object sender, EventArgs e)
    {
   thread thr = new thread(test1);
   thr.Start();}


private void button3_Click(object sender, EventArgs e)
    {
   Process[] processes = Process.GetProcessesByName("chrome");
        Process[] array = processes;
        for (int i = 0; i < array.Length; i++)
        {
            Process process = array[i];
            process.Kill();
        }
        Thread thr = new Thread(review);
        thr.Abort();
    }

1 个答案:

答案 0 :(得分:0)

您的变量在错误的位置声明

        private Thread thr = null;
        public void test1()
        {
            var dr = new chromdriver();

            dr.navigate().GoToUrl("http://google.com");
            if(xx != null)
            {
                IWebElement emal = dr.FindElement(By.XPath("//[@id=\"Email\"]"));
                emal.Sendkeys(email);
            }
            else
            {
                IWebElement emal = dr.FindElement(By.XPath("//[@id=\"Email\"]"));
                emal.Sendkeys(email);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
           thr = new Thread(test1);
           thr.Start();
        }


        private void button3_Click(object sender, EventArgs e)
        {
            Process[] processes = Process.GetProcessesByName("chrome");
            Process[] array = processes;
            for (int i = 0; i < array.Length; i++)
            {
                Process process = array[i];
                process.Kill();
            }
            thr.Abort();
        }