[英]How to draw from a thread, but smarter?
private void DisplayThread()
{
while (RunDisplayThread)
{
Thread.Sleep(30);
MPLastMessage.Text = MPTemplate.MPRET[(int)MP.MPReturn];
Invoked();
TickCount++;
TickCountLabel.Text = string.Format("Tick Count: {0}", TickCount);
}
}
private void Invoked()
{
while (MP.Drawing) { };
g.DrawImage(MP.offscreen, 50, this.Height-300);
}
So, this works, but after reading this, 所以,这可行,但是在阅读完之后,
Draw image on a form from a separate thread 从单独的线程在表单上绘制图像
I am clearly doing this in a less-than-intelligent way. 我显然以一种不那么智能的方式进行此操作。 Can anyone explain what the difference is between the suggested answer and what I'm doing?
谁能解释建议答案和我在做什么之间的区别? I've been teaching myself C#, and I'm at a loss for how that answer works.
我一直在自学C#,但对于答案的工作方式我感到茫然。
Although the link you provided is named "draw (...) from a separate thread", the drawing itself still happens on the Gui thread, using Control.Invoke()
( http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx ). 尽管您提供的链接被命名为“从单独的线程中绘制(...)”,但是使用
Control.Invoke()
( http://msdn.microsoft.com/zh-cn /library/zyzhdc6b.aspx )。
Your Invoked()
method runs on the "DisplayThread". 您的
Invoked()
方法在“ DisplayThread”上运行。 I don't know where the g
variable comes from though, is this a class variable set in the OnPaint
method? 我不知道
g
变量来自哪里,这是在OnPaint
方法中设置的类变量吗? If so chances are the DrawImage
would fail if it would take more time than the code in OnPaint
. 如果是这样,则
DrawImage
如果花费比OnPaint
的代码更多的时间,则将失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.