在我的 dll 中,我公开了一个事件,如下所示:
public delegate void LogMsgEventHandler(object sender, LogEventArgs e);
public event LogMsgEventHandler newLogMessage;
public class LogEventArgs : EventArgs
{
public string logMsg;
}
protected virtual void OnChanged(LogEventArgs e)
{
if (newLogMessage != null)
newLogMessage(this, e);
}
各种方法触发事件以记录操作细节。在使用 dll 的主窗体上,我将任何日志消息输出到列表框:
slotUtil.newLogMessage += new slotUtils.LogMsgEventHandler(slotUtil_newLogMessage);
..
void slotUtil_newLogMessage(object sender, slotUtils.LogEventArgs e)
{
lbDebug.Items.Add(e.logMsg);
}
问题是如果日志事件触发得太快,表单就会冻结。我认为这是一个线程问题?我怎样才能修复这个表单更新流畅的设计?这是一个糟糕的设计吗?我的备用设计 ID 也将所有日志记录存储在 dll 中的私有字符串中,然后仅在调用特定方法时转储日志。想法?
谢谢!