-4

我有两个名为mainFormhelperForm的表单。在 mainForm 上我有一个按钮,在 helperForm 上我有一个richTextBox。我想做的是;单击 mainForm 上的按钮后,我想显示 helperForm,以及richtextbox 上的一些文本。使用下面的代码,我可以看到帮助表单,但是在按钮完成buttonClick事件内部的所有过程之后出现的文本......

主窗体

public partial class Form : Form 
{
   public HelperForm helperForm;

   public MainForm()
   {
     InitializeComponent(); 
   }  

   public void button_Click(object sender, EventArgs e)
   {
     helperForm= new HelperForm ();
     helperForm.Show();    

     helperForm.richTextBox1.AppendText("Program started");

     //doing  process1  
     helperForm.richTextBox1.AppendText("Program start to check process1");

     //doing process2  
     helperForm.richTextBox1.AppendText("Program start to check process2");

     //doing process3  
     helperForm.richTextBox1.AppendText("Program start to check process3");

     //doing process2  
     helperForm.richTextBox1.AppendText("All the process are done!");

     helperForm.Close();
}
4

2 回答 2

0

正确的解决方案是使用后台线程来实现“进度条”类型的显示,从而防止锁定 UI。从后台线程命令 UI 操作时,您需要调用这些操作以防止异常 - 不允许直接从后台线程更新 UI。

以下代码可以满足您的需求,但您需要阅读线程、调用和委托,否则这将没有任何意义。如果我们可以看到更多您的代码,则可能有一种使用 Application.DoEvents() 的方法,但这在循环中更合适,它提供了一个重复命令的好地方。除非我将一个小型实用程序放在一起,否则我仍然更喜欢后台线程。

public partial class Form1 : Form
{
    private HelperForm helperForm;
    private Thread processRunner;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (processRunner == null || processRunner.ThreadState == ThreadState.Stopped)
        {
            helperForm = new HelperForm();
            helperForm.Show();

            processRunner = new Thread(new ThreadStart(runProcesses));
            processRunner.Start();
        }
    }

    private void runProcesses()
    {


        this.Invoke(new Action( () => { helperForm.richTextBox1.AppendText("Program started"); }));

        //represents  process1
        Thread.Sleep(2000);

        this.Invoke(new Action( () => { helperForm.richTextBox1.AppendText("Program start to check process1"); }));

        //represents process2  
        Thread.Sleep(2000);

        this.Invoke(new Action( () => { helperForm.richTextBox1.AppendText("Program start to check process2"); }));

        //represents process3  
        Thread.Sleep(2000);

        this.Invoke(new Action(() => { helperForm.richTextBox1.AppendText("Program start to check process3"); }));

        //represents process4  
        Thread.Sleep(2000);

        this.Invoke(new Action(() => { helperForm.richTextBox1.AppendText("All the process are done!"); }));

        // to let the message display
        Thread.Sleep(2000);

        helperForm.Invoke(new Action(() => { helperForm.Close(); }));
    }
}
于 2013-09-22T14:00:22.733 回答
0

尝试这个;

    public void button_Click(object sender, EventArgs e)
    {
        helperForm= new HelperForm (); 
        Timer countdown=new Timer();
        countdown.Interval=1000;//1000 Milliseconds
        countdown.Tick+=CountDownTickEvent;

        switch(condition to check which text to append):
        {
        case 1:
        helperForm.richTextBox1.AppendText("Program started");
        break;

        case 2:
        //doing  process1  
        helperForm.richTextBox1.AppendText("Program start to check process1");
        break;

        case 3:
        //doing process2  
        helperForm.richTextBox1.AppendText("Program start to check process2");
        break;

        case 4:
        //doing process3  
        helperForm.richTextBox1.AppendText("Program start to check process3");
        break; 

        case 5:
        //doing process2  
        helperForm.richTextBox1.AppendText("All the process are done!");
        break;
        } 

        helperForm.Show();

     }

您正在无模式地显示一个表单,这意味着这个不会阻塞调用者线程,因此没有理由在Form.Close()之后调用 6 个语句Form.Show(),这没有任何意义,这只是显示表单以供 flash 使用,然后表单消失。如果你需要在指定的持续时间内显示表单最好使用Timer.

编辑:要在指定的时间内显示表格,请使用以下功能;

    int counter=0;
    public void button_Click(object sender, EventArgs e)
    {
        helperForm= new HelperForm (); 
        Timer countdown=new Timer();//New instance of Timer class.
        countdown.Interval=1000;//1000 Milliseconds,change as needed.
        countdown.Tick+=CountDownTickEvent;//Event handler for tick event of Timer.

        //Same as above.

        helperForm.Show();
        countdown.Start();//Start the timer.
     }

然后创建CountDownTickEvent如下一个;

    private void CountDownTickEvent(object sender, EventArgs e)
    {
        counter++;
        if (counter == 5)//5 seconds have been passed since the timer is running.Change as needed.
        {
            helperForm.Close();//Displayed for 5 seconds,form should close now.
            counter=0;
        }
    }

好吧,这会给你一个带有文本的helperForm 5 秒钟,然后它会自动关闭。

于 2013-09-22T13:17:47.253 回答