好吧,我制作一个流程栏,这是有效的。现在,我在一个弹出窗口中有它,我希望它更新,而另一个线程(而不是主线程)正在完成一些单词。无论如何,我试图使用来自其他线程的信息更新进程,但它没有更新。我在另一个线程中使用for循环,我正在使用我,所以找到我剩下的百分比。在我的代码中:
void callPopUp()
{
new Thread(() => new PopUp().ShowDialog()).Start();
}
//___ other thread
bool running(int Baseline)
{
HomeMO();
tcClient.WriteAny(statenumber, Int16.Parse("11"));
while ( state != 11)
{
Thread.Sleep(500);
tcClient.WriteAny(statenumber, Int16.Parse("11"));
}
StepInfo step= get_StepInfo();
int point = step.Start_Step;
callPopUp();
stepPass = step.degress;
startStep = step.Start_Step;
stopStep = step.End_Step;
for ( int i =0; i < step.runcount; i++)
{
try
{
callPopUp(i, step);
...
现在这是我的主要形式。这是我的弹出窗体中的代码:
private void t_Tick(object sender, EventArgs e)
{
// graphics
g = Graphics.FromImage(bmp);
// clear grapthic
g.Clear(Color.LightBlue);
//draw progressbar
g.FillRectangle(Brushes.CornflowerBlue, new Rectangle(0, 0,(int)(percent * pbUNit), (int)pbHEIGHT));
// draw % complete
g.DrawString(percent + " %", new Font("Arial", (pbHEIGHT / 2F)), Brushes.Black, new PointF((pbWIDTH/2F)-20, (pbHEIGHT/2F)-20));
//load bitmap in picturebox
pictureBox1.Image = bmp;
if(percent > 100)
{
// dispose
g.Dispose();
t.Stop();
}
}
}
这是绘制进程条的计时器。但不知何故,百分比没有更新。这是整个表格:
public partial class PopUp : Form
{
Timer t = new Timer();
double pbUNit;
float pbWIDTH, pbHEIGHT, pbComplete;
Bitmap bmp;
Graphics g;
private int percent =0;
public string Title;
public int Percents
{
get { return percent; }
set { percent = value; }
}
public PopUp()
{
InitializeComponent();
this.Text = Title;
}
private void PopUp_Load(object sender, EventArgs e)
{
this.Text = Title;
pbWIDTH = pictureBox1.Width;
pbHEIGHT = pictureBox1.Height;
pbUNit = pbWIDTH / 100.0;
// pbComplete - this is equal to work completed in % [min = 0 max= 100]
pbComplete = 0;
bmp = new Bitmap((int)pbWIDTH, (int)pbHEIGHT);
//timer
t.Interval = 50;
t.Tick += new EventHandler(this.t_Tick);
t.Start();}
如果我只是在弹出窗口中放入一个计数器,这又有效。但是当我将数据发送到弹出窗口时它不起作用。它只是保持在0;
另外,我确实试过.show但是然后弹出窗体确实出现了,但我根本看不到我的进程条。
您无法从另一个线程显示弹出对话框,您只能访问UI线程中的UI元素。