Form1_Load 未被调用(尝试拥有一个 BackgroundWorker)
Form1_Load is not called (attempt to have a BackgroundWorker)
我正在用 C# 开发简单的 Windows 表单应用程序
我想显示一个 GUI,然后在后台启动一些任务。我想在表格中使用文本标签显示这些任务的进度。
Program.cs 非常简单:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form mainForm = (new Form1());
Application.Run(mainForm);
}
对于我使用绑定的标签
partial class Form1 : INotifyPropertyChanged
{
public string Title
{
get { return _title; }
set
{
_title = value;
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Title"));
}
}
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this, "Title", true));
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(46, 17);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(763, 255);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
private string _title = "xxgg";
private System.Windows.Forms.Label label1;
public event PropertyChangedEventHandler PropertyChanged;
然后我想运行一个后台工作者,但是由于某些原因
Form1_Load 方法没有被执行。
private void Form1_Load(object sender, System.EventArgs e)
{
System.Console.WriteLine("Form loaded");
var bw = new BackgroundWorker();
bw.DoWork += bw_DoWork;
bw.RunWorkerAsync(); //Start the worker
}
void bw_DoWork(object sender, DoWorkEventArgs e)
{
InfoText it = new InfoText();
FileMover fm = new FileMover(it);
foreach(string path in sourcePaths)
{
fm.MoveFrom(path);
this.Title = it.text;
}
}
如您所见,我想在 运行后台工作人员更新标题。
我的 Form1_Load 没有被调用的原因是什么?谢谢
编辑:
我在 Form1 class 中添加了 BackgroundWorker 代码,如下所示:
public partial class Form1 : Form
{
BackgroundWorker bw;
public Form1()
{
InitializeComponent();
it = new InfoText();
bw = new BackgroundWorker();
bw.DoWork += bw_DoWork;
bw.WorkerReportsProgress = true;
bw.ProgressChanged += Bw_ProgressChanged;
bw.RunWorkerCompleted += Bw_RunWorkerCompleted;
bw.RunWorkerAsync(); //Start the worker
}
private void Bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Console.WriteLine("done");
this.Title = it.text;
}
private void Bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
Console.WriteLine("progress changed " + e.ProgressPercentage);
this.Title = it.text;
}
void bw_DoWork(object sender, DoWorkEventArgs e)
{
FileMover fm = new FileMover(it);
foreach(string path in sourcePaths)
{
fm.MoveFrom(path);
bw.ReportProgress(1);
}
}
}
现在它完全符合我的要求。
添加
this.Load += new System.EventHandler(Form1_Load);
Inside InitializeComponent... 或 class 构造函数。
我想通过GUI设计器来添加,这样更安全也更容易。
我正在用 C# 开发简单的 Windows 表单应用程序
我想显示一个 GUI,然后在后台启动一些任务。我想在表格中使用文本标签显示这些任务的进度。
Program.cs 非常简单:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form mainForm = (new Form1());
Application.Run(mainForm);
}
对于我使用绑定的标签
partial class Form1 : INotifyPropertyChanged
{
public string Title
{
get { return _title; }
set
{
_title = value;
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Title"));
}
}
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this, "Title", true));
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(46, 17);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(763, 255);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
private string _title = "xxgg";
private System.Windows.Forms.Label label1;
public event PropertyChangedEventHandler PropertyChanged;
然后我想运行一个后台工作者,但是由于某些原因 Form1_Load 方法没有被执行。
private void Form1_Load(object sender, System.EventArgs e)
{
System.Console.WriteLine("Form loaded");
var bw = new BackgroundWorker();
bw.DoWork += bw_DoWork;
bw.RunWorkerAsync(); //Start the worker
}
void bw_DoWork(object sender, DoWorkEventArgs e)
{
InfoText it = new InfoText();
FileMover fm = new FileMover(it);
foreach(string path in sourcePaths)
{
fm.MoveFrom(path);
this.Title = it.text;
}
}
如您所见,我想在 运行后台工作人员更新标题。
我的 Form1_Load 没有被调用的原因是什么?谢谢
编辑: 我在 Form1 class 中添加了 BackgroundWorker 代码,如下所示:
public partial class Form1 : Form
{
BackgroundWorker bw;
public Form1()
{
InitializeComponent();
it = new InfoText();
bw = new BackgroundWorker();
bw.DoWork += bw_DoWork;
bw.WorkerReportsProgress = true;
bw.ProgressChanged += Bw_ProgressChanged;
bw.RunWorkerCompleted += Bw_RunWorkerCompleted;
bw.RunWorkerAsync(); //Start the worker
}
private void Bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Console.WriteLine("done");
this.Title = it.text;
}
private void Bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
Console.WriteLine("progress changed " + e.ProgressPercentage);
this.Title = it.text;
}
void bw_DoWork(object sender, DoWorkEventArgs e)
{
FileMover fm = new FileMover(it);
foreach(string path in sourcePaths)
{
fm.MoveFrom(path);
bw.ReportProgress(1);
}
}
}
现在它完全符合我的要求。
添加
this.Load += new System.EventHandler(Form1_Load);
Inside InitializeComponent... 或 class 构造函数。
我想通过GUI设计器来添加,这样更安全也更容易。