我有一个应用程序,在窗体上绘制一些(只是一个测试应用程序)。我遇到了Thread.Sleep的问题。我使用每100毫秒启动一次DrawinArea重绘的线程。它工作正常,但有时此线程不能从This.Sleep(100)调用返回。我检查了调试器,并且我确定这个问题出现在Thread.Sleep()中。Thread.Sleep永不返回
注意:它需要Gtk#才能运行。
using System;
using Gtk;
using Cairo;
using System.Threading;
using System.Timers;
public partial class MainWindow: Gtk.Window
{
Thread redrawThread = new Thread(Redrawer);
//System.Timers.Timer timer = new System.Timers.Timer();
static int calls = 0;
public MainWindow()
: base(Gtk.WindowType.Toplevel)
{
Build();
this.drawingarea2.ExposeEvent += OnExpose;
redrawThread.Name = "Redraw Thread";
redrawThread.Start(this);
/*
timer.Interval = 100;
timer.Elapsed += (sender, e) =>
{
drawingarea2.QueueDraw();
};
timer.AutoReset = true;
timer.Start();*/
}
protected void OnDeleteEvent(object sender, DeleteEventArgs a)
{
redrawThread.Abort();
Application.Quit();
a.RetVal = true;
}
static void Redrawer(object wnd) {
var area = (MainWindow)wnd;
while (true)
{
Thread.Sleep(100);
area.QueueDraw();
}
}
int x = 200;
int x_mod = 10;
int y = 150;
int y_mod = 10;
void OnExpose(object sender, ExposeEventArgs args) {
var area = (DrawingArea)sender;
if (x + 10 >= drawingarea2.Allocation.Width || x - 10 < 0)
x_mod = -x_mod;
if (y + 10 >= drawingarea2.Allocation.Height || y - 10 < 0)
y_mod = -y_mod;
x += x_mod;
y += y_mod;
var ny = Math.Abs(y - drawingarea2.Allocation.Height);
using (var ctx = Gdk.CairoHelper.Create(area.GdkWindow))
{
ctx.LineWidth = 9;
ctx.SetSourceRGB(0.7, 0.2, 0.0);
ctx.Arc(x, ny, 10, 0, 2*Math.PI);
ctx.StrokePreserve();
ctx.SetSourceRGB(0.3, 0.4, 0.6);
ctx.Fill();
ctx.GetTarget().Dispose();
}
}
}
一旦遇到这个问题,我决定切换到System.Timers.Timer,但我也遇到了问题。计时器在一段时间后停止发射事件。我搜索了这个问题,发现如果没有对它的引用,GC可以销毁定时器。我的计时器是一个类成员,因此参考总是存在,但无论如何,它会停止。问题是什么?
我想不出一件GTK#可以做到这一点,将阻止'Thread.Sleep'调用返回。你说你确定这是睡眠呼叫,你是怎样使用调试器来发现这个问题的? –
首先,永远不要为UI线程使用'thread.Sleep'。正如你提到的问题,切换到*计时器*路线,并问你的问题。 –
@ L.B Thread.Sleep不在UI线程中使用,它是并行使用的,使UI重绘。在我的情况下,UI始终是响应式的。 – Xanx