在一个控制台应用程序,我有一个名为MyPolling其启动时,监控外部资源,直到停止类。里面的监视循环中,我使用注入实施ILogger的,该服务使用写消息对轮询类的状态。为什么我的窗口没有从我的线程更新?
有时我会想ILogger的实现写入到控制台,其他时间我可能要显示一个窗口,也写输出到窗口除了控制台。
然而,当我运行showLog = TRUE,没有被更新visualLog窗口下面的(纺“等待”图标)。对于showLog = {true,false}控制台记录器正在被正确更新。
很显然,我有某种线程的问题,但我不知道我哪里错了。
static void Main(string[] args)
{
bool showLog = true; //obviously will set this from args
VisualLogger visualLogger = null;
ILogger logger;
if (showLog)
{
//create the visual logger, and use that to create the debug logger
visualLogger = new VisualLogger();
logger = new DebugLogger(visualLogger);
}
else
{
//create the "normal" debug logger
logger = new DebugLogger();
}
//create the polling class and inject the logger and then start the thread
var svc = new MyPolling(logger);
var pollThread = new Thread(new ThreadStart(svc.BeginPolling));
pollThread.Start();
//if the visualLogger wasnt created, don't show it.
if (visualLogger!=null)
visualLogger.Show();
Console.ReadLine();
svc.Dispose();
}
public interface ILogger
{
void Write(string message);
}
public class DebugLogger : ILogger
{
private ILogger _baseLogger;
public DebugLogger(ILogger logger = null)
{
_baseLogger = logger;
}
public void Write(string message)
{
if (_baseLogger != null)
_baseLogger.Write(message);
Console.WriteLine(message);
}
}
,我的实VisualLogger的:
public partial class VisualLogger : Form, ILogger
{
public VisualLogger()
{
InitializeComponent();
txtLog.Clear();
}
public void Write(string message)
{
txtLog.Text += message + Environment.NewLine;
}
}
你得到任何异常?如果您在与UI线程不同的线程上工作,则需要在显示消息之前调用UI线程。 –