111,131
社区成员
发帖
与我相关
我的任务
分享
public class TankClient : Form
{
int xPos = 30;
int yPos = 30;
public static void Main()
{
TankClient tc = new TankClient();
tc.LaunchForm();
}
protected void LaunchForm()
{
this.Size = new Size(800, 600);
Thread t = new Thread(new ThreadStart(Run));
t.Start();
this.ShowDialog();
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
SolidBrush sb=new SolidBrush(Color.Red);
g.FillEllipse(sb,xPos, yPos,50,50);
}
private void Run()
{
while (true)
{
yPos += 5;
Thread.Sleep(50);
//怎么重画?
}
}
}
//重绘代理
private delegate void RefreshDele();
//重载一下重绘方法
public override void Refresh()
{
if (!this.IsDisposed && this.InvokeRequired)
{
//调用反射方法
this.Invoke(new RefreshDele(Refresh));
}
else
{
base.Refresh();
}
}
static void InvokeDelegate(Delegate del, object[] args)
{
ISynchronizeInvoke synchronizer = del.Target as ISynchronizeInvoke;
if (synchronizer != null)//Requires thread affinity
{
if (synchronizer.InvokeRequired)
{
synchronizer.Invoke(del, args);
return;
}
}
//Not requiring thread afinity or invoke is not required
del.DynamicInvoke(args);
}
Control.CheckForIllegalCrossThreadCalls = false;public partial class Form1 : Form
{
int xPos = 30;
int yPos = 30;
//线程定义
Thread t = null;
public Form1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
SolidBrush sb = new SolidBrush(Color.Red);
g.FillEllipse(sb, xPos, yPos, 50, 50);
}
private void Run()
{
while (true)
{
yPos += 5;
Thread.Sleep(50);
//怎么重画?
this.Refresh();
}
}
//重绘代理
private delegate void RefreshDele();
//重载一下重绘方法
public override void Refresh()
{
if (!this.IsDisposed && this.InvokeRequired)
{
this.Invoke(new RefreshDele(Refresh));
}
else
{
base.Refresh();
}
}
private void Form1_Load(object sender, EventArgs e)
{
//启动线程
this.Size = new Size(800, 600);
t = new Thread(new ThreadStart(Run));
t.Start();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//关闭线程
if (t.ThreadState != ThreadState.Stopped)
{
t.Abort();
}
}
} public delegate void dTest();
void invokeThread()
{
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Reset();
sw.Start();
if (this.InvokeRequired)
{
this.Invoke(new dTest(CreateTreeView));
}
else
{
CreateTreeView();//方法名
}
sw.Stop();
}