【发布时间】: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