【问题标题】:C# Updating form (richTextBox and toolStripStatusLabel) from Program threadC# 从程序线程更新表单(richTextBox 和 toolStripStatusLabel)
【发布时间】:2012-10-28 04:15:53
【问题描述】:

我正在尝试使用一个控制台应用程序并为其构建一个 gui。我已经让我的 GUI 的主要功能正常工作,并将控制台应用程序文件添加到我的新项目中。我更改了文件的名称空间以匹配 winform 应用程序的名称空间,并将控制台应用程序的主要功能移动到 Program 类中,将其定义为一个函数,该函数采用从表单获取的 CSV 路径参数。

因此,它似乎可以运行该程序,但我无法更新表单。有一些 Console.WriteLine() 函数我想写入 toolStripStatusLabel 和其他到richTextBox。

我是 C# 新手,主程序不是由我编写的,但我正在尝试构建它。

程序.cs

Form1 Frm1 = new Form1();
Frm1.UpdateStatusBar("Sorted jobs by EDD....");
Frm1.Refresh();

Form1.cs

public void UpdateStatusBar(string status)
{
    Form1 Frm1 = new Form1();
    Frm1.toolStripStatusLabel1.Text = status;  
}

Pastebin Program.cs 见第 92 行。

Pastebin Form1.cs 见第 88 行。

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    正如@Thinhbk 所说,每次您想要更新工具条时都创建一个新的Form1 不是要走的路。您遇到的另一个问题是所有处理都在同一个线程中运行(我查看了您在 Pastebin 上发布的内容),这意味着您直到最后都不会看到进度更新。

    为了让它工作,我首先将public void AX1Program(string path) 方法的签名修改为:

    public void AX1Program(Form1 form1, ManualResetEvent resetEvent, string path)
    

    传递表单意味着我们可以访问UpdateStatusBar 方法,resetEvent 用于在线程完成时发出信号。 AX1Program 的正文变为:

    try
    {
        ...
        //Do work as per normal
        ...
        form1.UpdateStatusBar("some new status");
    }
    catch (Exception)
    {
        //Any exception handling/logging you may need.
    }
    finally
    {
        //Indicate that we are done.
        resetEvent.Set();
    }
    

    现在,要调用 AX1Program 方法,您目前有一些代码(它在您的 Writebutton_Click 方法中的几个地方):

    Program Pgm1 = new Program();
    Pgm1.AX1Program(CSVtextBox.Text);
    

    我们想异步调用它,所以我们会改为:

    RunAX1Program(CSVtextBox.Text);
    

    其中调用了以下两种方法:

    private void RunAX1Program(string text)
    {
        ManualResetEvent resetEvent = new ManualResetEvent(false);
        ThreadPool.QueueUserWorkItem(RunAX1ProgramDelegate,
            new object[] { resetEvent, text });
    }
    
    private void RunAX1ProgramDelegate(object state)
    {
        object[] stateArray = (object[])state;
        ManualResetEvent resetEvent = (ManualResetEvent)stateArray[0];
        string text = (string)stateArray[1];
    
        Program program = new Program();
        program.AX1Program(this, resetEvent, text);
        //Wait until the event is signalled from AX1Program.
        resetEvent.WaitOne();
    }
    

    因为我们现在想从另一个线程更新工具条,UpdateStatusBar 方法需要如下所示,以便我们可以安全地修改控件:

    public void UpdateStatusBar(string status)
    {
        if (this.InvokeRequired)
        {
            this.BeginInvoke((MethodInvoker)delegate
            {
                UpdateStatusBar(status);
            });
        }
        else
        {
            toolStripStatusLabel1.Text = status;
        }
    }
    

    然后您可以使用类似的模式来更新您的富文本框。

    【讨论】:

      【解决方案2】:

      我认为,问题在于:您在尝试 UpdateStatusBar() 时尝试创建新的 Form1。

      您可以通过以下方式解决此问题:

      public void UpdateStatusBar(string status)
      {    
          this.toolStripStatusLabel1.Text = status;  
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-09
        • 1970-01-01
        相关资源
        最近更新 更多