多线程应用 [英] Multi Thread application
问题描述
伙计们,我在backgroundWorker1_DoWork
中有一个像这样的程序(有后台工作者).例如,我左右移动按钮:
hey guys I''ve a program like this (with a background worker) in backgroundWorker1_DoWork
. For example I move a button to left and right:
public partial class Form1 : Form
{
bool f = false;
//--------------------------------------------------------------
public Form1()
{
InitializeComponent();
}
//--------------------------------------------------------------
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Y > 40)
{
this.BeginInvoke((ThreadStart)delegate()
{
MenuShow();
});
}
else if (e.Y < 100)
{
this.BeginInvoke((ThreadStart)delegate()
{
MenuHide();
});
}
//am I used this correctly?:~
backgroundWorker1.RunWorkerAsync();
}
//--------------------------------------------------------------
private void MenuHide()
{
while (menuStrip1.Top != 0)
{
menuStrip1.Top++;
Thread.Sleep(10);
menuStrip1.Update();
}
}
//--------------------------------------------------------------
private void MenuShow()
{
while (menuStrip1.Top != -25)
{
menuStrip1.Top--;
Thread.Sleep(10);
}
}
public delegate void InvokeDelegate();
//--------------------------------------------------------------
private void Invoke_Click(object sender, EventArgs e)
{
textBox1.BeginInvoke(new InvokeDelegate(InvokeMethod));
}
//--------------------------------------------------------------
public void InvokeMethod()
{
textBox1.Text = "Executed the given delegate";
}
//--------------------------------------------------------------
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
f = true;
}
//--------------------------------------------------------------
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
while(!f) //is this good way to stop the action?:~
{
{
this.BeginInvoke((ThreadStart)delegate()
{
//Invoke is a button name
while (InvokeButton.Left != 30)
{
InvokeButton.Left--;
Thread.Sleep(10);
}
while (InvokeButton.Left != 120)
{
InvokeButton.Left++;
Thread.Sleep(10);
}
});
}
}
}
}
仅按钮移动有效.显示/隐藏菜单不起作用.
Only the button movement works. The show/hide menu doesn''t work. How can I make these works do separately?
推荐答案
首先,您不需要变量f-稍后将了解原因.
其次,您的BackgroundWorker
对象应支持取消操作,以便在工作人员忙碌时用户执行某些操作以取消表单时可以正确清除该对象.
while语句应如下所示:
First, you don''t need the variable f - you''ll see why in a moment.
Second, yourBackgroundWorker
object should support cancellation so that it cleans up correctly if the user does something to dismiss the form while the worker is busy.
The while statement should look something like this:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
while(!worker.CancellationPending)
{
// In here, invoke a MoveButton method that actually moves the
// button in the desired direction
}
}
第三, ALL UI更新应该通过委托来执行(使用Invoke
).根据要完成的工作,您可能需要多个.
最后,名称"Invoke"对于您的按钮控件来说是一个糟糕的选择,特别是因为有Invoke()
方法.将其更改为buttonInvoke
或类似名称.
Third, ALL UI updates should be performed through delegates (using Invoke
). You may need more than one depending on what you want to accomplish.
Lastly, the name "Invoke" is a poor choice for your button control, especially since there is an Invoke()
method. Change it to buttonInvoke
, or something like that.
这篇关于多线程应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!