【问题标题】:Console and form in one and data transfer?控制台和表格合二为一和数据传输?
【发布时间】:2016-02-24 13:19:40
【问题描述】:

有一个代码代表控制台应用程序,其中我为表单完成了新线程并在我们的新线程上显示了一个 CustomForm,我也尝试了某种数据传输但我没有成功。

Program.cs 代码...

class Program {
    public static CustomForm _customForm {
        get {
            return customForm;
        }
        set {
            customForm = value;
            customForm.Show();
        }
    }

    private static CustomForm customForm;
    /// <summary>
    /// Static method which constains all the magic for the console!
    /// </summary>
    /// <param name="args"></param>
    static void Main(string[] args) {
        // Declaring Thread for the FormThread.
        Thread formThread = new Thread(new ThreadStart(FormThread));
        // Fires out the work of the thread.
        formThread.Start();
        Console.ReadKey();
        // And console is still running?
        // Thread formThread is still running too, thats the reason bruh!
    }

    /// <summary>
    /// Static method which constains all the magic for the form!
    /// </summary>
    static void FormThread() {
        customForm.lbl.Text = "Yolo, it wurks!";
        Application.Run(new CustomForm());
    }
}

CustomForm.cs 代码 ...

public partial class CustomForm : Form {
    public string lblText {
        get {
            return lbl.Text;
        }
        set {
            lbl.Text = value;
        }
    }

    /// <summary>
    /// Just initializer, something what we'll never understand.
    /// </summary>
    public CustomForm() {
        InitializeComponent();
    }

    /// <summary>
    /// When the form is loaded.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void OnLoad(object sender, EventArgs e) {
        Program._customForm = this;
    }
}

我唯一想做的就是调用 lbl 的 text 属性并在 program.cs 中设置一些值,而不是在 customform.cs 中

有时表单不会显示或表单中的 lbl 未更改。

【问题讨论】:

  • 你在哪里初始化customForm

标签: c# multithreading winforms console-application


【解决方案1】:

customForm.lbl.Text = "Yolo, it wurks!"; 在您创建 CustomForm 之前执行。

可能,您需要在 main 中创建表单并将其传递给 Application.Run(CustomForm);

   static void Main(string[] args) {
        // Declaring Thread for the FormThread.
        Thread formThread = new Thread(new ThreadStart(FormThread));
        // Fires out the work of the thread.
        customForm = new CustomForm();
        formThread.Start();
        Console.ReadKey();
        // And console is still running?
        // Thread formThread is still running too, thats the reason bruh!
    }

此外,您不能从其他线程更改控件属性。为了从其他线程更改属性,请使用Invoke 方法。

 public partial class CustomForm : Form {
     public string lblText
        {
            get
            {
                return lbl.Text;
            }
            set
            {
                if (lbl.InvokeRequired)
                    lbl.Invoke((MethodInvoker) (() => lbl.Text = value));
                else
                    lbl.Text = value;
            }
        }

 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多