我如何在C#中执行多线程

时间:2019-02-23 05:50:26

标签: c# multithreading winforms

我想通过按“ findResult”按钮提取数字的分母,并同时按另一个我称为“ count_Button”的按钮,以秒为单位开始计算以计算获得结果所需的时间长度,但是在按下第一个按钮,程序停止响应,并且不允许我按下第二个按钮,

问题是什么,如何执行多线程? 这是我写的代码

private void findResult_Click(object sender, EventArgs e)
    {
        for (int i = 1; i <= int.Parse(numberTextBox.Text); i++)
        {

            if (int.Parse(numberTextBox.Text) % i == 0)
            {
                ListBox.Items.Add(i);

                Thread wait = new Thread(stop);
                wait.Start();

            }
        }
    }

    public void stop()
    {
        Thread.Sleep(5000);
    }

    private void count_Button_Click(object sender, EventArgs e)
    {
        beginCounting.Enabled = true;
        beginCounting.Start();
    }

    private void secondsTextBox_TextChanged(object sender, EventArgs e)
    {

    }
    private void beginCounting_Tick(object sender, EventArgs e)
    {
        if (beginCounting.Enabled == true)
        {
            int count = 0;
            count = count + 1; secondsTextBox.Text = count.ToString();
        }
    }

0 个答案:

没有答案