0

我想要做的是在运行单元测试时动态创建表单。实际的表单只会充当进度条。我的单元测试使用 Selenium 库来测试网站,最好有某种进度条。我试图在测试中间创建新表单,但是在测试执行时,表单只是挂在那里。谢谢!

 [TestMethod]
    public void test1()
    {     
        using(Form1 form=new Form1())
        {
            form.Show();
            form.RunAsync();
        }

        Assert.IsTrue(doSomeThing());
    }

这就是 form 现在要做的事情。这只是一个例子!backgroundworker 应该填充进度条,但是,一旦表单出现,它就会冻结

BackgroundWorker bw;

 private void RunAsync()
    {
        bw.RunWorkerAsync();
    }


void bw_DoWork(object sender, DoWorkEventArgs e)
    {
       or (int i = 0; i < 100; i++)
        {
            Thread.Sleep(100);                     
            bw.ReportProgress(i);              
        }           
        bw.ReportProgress(100);
    }
4

1 回答 1

0

这是一个简单的进度条示例

public Form1()
{
    InitializeComponent();
     backgroundWorker1.DoWork += backgroundWorker1_DoWork_1;
     backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged_1;
     Load += Form1_Load_1;
}

private void Form1_Load(object sender, System.EventArgs e)
{
    // Start the BackgroundWorker.
    backgroundWorker1.RunWorkerAsync();
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    for (int i = 1; i <= 100; i++)
    {
    // Wait 100 milliseconds.
    Thread.Sleep(100);
    // Report progress.
    backgroundWorker1.ReportProgress(i);
    }
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    // Change the value of the ProgressBar to the BackgroundWorker progress.
    progressBar1.Value = e.ProgressPercentage;
    // Set the text.
    this.Text = e.ProgressPercentage.ToString();
}

注意区别:您的后台工作人员做了一些工作并报告它,但没有人注意到它,或者您没有显示所有代码。无论如何,您可以使用此代码

于 2013-08-01T12:57:18.997 回答