定义了2个委托,一个用线程启动,一个用backgroundworker启动,为什么我用线程启动的委托刚开始只执行了一遍,等到用backgroundworker启动的委托执行完成后才会继续执行线程的委托?这个难道还有什么优先级?private int i = 0;
private delegate void InvokeLabel();
private delegate void InvokeMainForm();
private void Form_Load(object sender, EventArgs e){
Thread setLabelTextThread = new Thread(setLabelText);
setLabelTextThread.Start();
this.backgroundWorker.RunWorkerAsync();
}
private void setLabelText()
{
InvokeLabel il = delegate(){
this.lable1.Text = i++.toString();
};
this.Invoke(il);
}
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e){
while(true){
InvokeMainForm imf = delegate(){
mainForm = new MainForm();
};
this.Invoke(imf);
}
} 程序运行i++运行了一次,然后等到mainForm实例化后会继续执行?为什么?
private delegate void InvokeLabel();
private delegate void InvokeMainForm();
private void Form_Load(object sender, EventArgs e){
Thread setLabelTextThread = new Thread(setLabelText);
setLabelTextThread.Start();
this.backgroundWorker.RunWorkerAsync();
}
private void setLabelText()
{
InvokeLabel il = delegate(){
this.lable1.Text = i++.toString();
};
this.Invoke(il);
}
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e){
while(true){
InvokeMainForm imf = delegate(){
mainForm = new MainForm();
};
this.Invoke(imf);
}
} 程序运行i++运行了一次,然后等到mainForm实例化后会继续执行?为什么?
this.Invoke改成this.BeginInvoke
void dowork()
{
1,work1()
Refresh()
2,work2()
Refresh()
3,work3()
Refresh()
4,work4()
Refresh()
}void Refresh()
{
//刷新界面进度条
}但是正常的需求是费时的加载操作放到子线程1去做,然后另一个子线程2轮询检查线程1工作的进度,然后更新UI。
public static i = 0;
public static Label label;
private delegate void InvokeMainForm();
public Form(){
InitializeComponent();
label = new Label();
this.Controls.Add(label);
}
private void Form_Load(object sender, EventArgs e){
this.backgroundWorker.RunWorkerAsync();
}
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e){
InvokeMainForm imf = delegate(){
mainForm = new MainForm();
};
this.Invoke(imf);
}
}
}
public class MainForm(){
public MainForm(){
InitializeComponent();
void1();
void2();
void3();
void4();
}
}主页面的每个方法里会有执行Form.label.Text = Form.i++.toString();但是Form界面上只会显示最后一次的赋值(void4里的赋值),为什么前几次方法的值都不显示?一般的程序启动都会有进度条(比如Eclipse)他加载程序时,进度条界面不是有文字一直在显示做什么,那个是什么原理?
void1();
void2();
void3();
void4();
这几个过程很快,那你的值很快就变过去了。不是没变,是变得太快了。
{
public static MainForm mainForm; private Thread progressBarThread; private int progressBarValue = 1; private bool flag; public delegate void InvokeMainForm(); public LoginLoading()
{
InitializeComponent();
}
private void LoginLoading_Load(object sender, EventArgs e)
{
progressBarThread = new Thread(RunProgressBar);
progressBarThread.Start();
this.backgroundWorker.RunWorkerAsync();
}
private void RunProgressBar()
{
flag = true;
while (flag)
{
if (progressBarValue < 99)
{
progressBarValue++;
} this.progressBar.Value = progressBarValue;
Thread.Sleep(100);
}
}
private void initMainForm()
{
if (mainForm == null)
{
mainForm = new MainForm();
}
}
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
if (this.InvokeRequired)
{
InvokeMainForm invoke = new InvokeMainForm(this.initMainForm);
this.Invoke(invoke);
} flag = false;
this.progressBar.Value = 100;
Thread.Sleep(100);
}
private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.Hide();
Thread.Sleep(500);
mainForm.Show();
}
}这个是我登录界面的代码,我的要求很简单,在登录界面上加个LABEL,显示我登录状态(类似Eclipse启动时那个进度框),但是上面的方法都试过了,无论是用委托给LABEL的TEXT赋值还是将LABEL传入主页面在每个方法前为其赋值都不行,各位有没有什么好办法?
然后在自增的地方用Thread.Sleep(100)
可否使用Timer控件