private void but_Click(object sender, EventArgs e)
{
Thread thread = new Thread(new ThreadStart(Method));
thread.Start();
}
private void Method()
{
PictureBox pb = new PictureBox();
this.Controls.Add(pb);
pb.Image = imageList.Images[0];
while (true)
{
pb.Location = new Point(pb.Location.X+1,pb.Location.Y+1);
Thread.Sleep(1000);
}
}
最好不要用线程,而是用Winform的定时器来做。
如果你使用 Thread.Start 的目的就是为了能够弄个死循环在哪里,那么要是让我们的一位程序员“小女子”看你这个程序一定会说“真是醉了”,让我看则会说“回家上班吧”。
Invoke 跨线程,看野比喵的多线程,其中还提供了类库,可以直接用。
PictureBox _pb;
private void but_Click(object sender, EventArgs e)
{
_pb = new PictureBox();
this.Controls.Add(pb);
_pb.Image = imageList.Images[0];
Thread thread = new Thread(new ThreadStart(Method));
thread.Start();
}
private void safeChangePBLoc()
{
if (_pb.InvokeRequired)
{
_pb.Invoke (new Action (safeChangePBLoc));
return;
}
_pb.Location = new Point(_pb.Location.X+1,_pb.Location.Y+1);
}
private void Method()
{
while (true)
{
this.safeChangePBLoc ();
Thread.Sleep(1000);
}
}
创建控件,改变控件显示相关的属性都不能在子线程做;