バックグラウンドイベントがたくさんあります。
それらはすべてログを呼び出します。
private void log(string text, params object[] values)
{
if (editLog.InvokeRequired)
{
editLog.BeginInvoke(
(MethodInvoker)delegate
{
this.log(text, values);
});
}
else
{
text = string.Format(text, values) + Environment.NewLine;
lock (editLog)
{
editLog.AppendText(text);
editLog.SelectionStart = editLog.TextLength;
editLog.ScrollToCaret();
}
}
}
時々私はこれを取得しますが、そうでない場合もあります:
System.AccessViolationException was unhandled
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source=System.Windows.Forms
StackTrace:
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.RichTextBox.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, Int32 wParam, Object& editOle)
at System.Windows.Forms.TextBoxBase.ScrollToCaret()
at Program1.frmMain.log(String text, Object[] values) in
...
...
...
PD: 常にこの行で停止するとは限りません。ランダムに、editLog メソッド/プロパティが使用される 3 回のうちの 1 つになります。常に例外がスローされるとは限りません。時々物が凍るように見えます。ただし、メインの UI ではなく、メッセージの流れだけです (つまり、ログは二度と呼び出されないように見えます)。
アプリは、バックグラウンド プロセスを備えた単一のフォームです。私はこれで何が間違っているのかわかりません...
更新:
私は Mangist の提案を行います。これがコードです (タイマーは 100ms で起動します)。同じ結果:
private Queue<String> logs = new Queue<String>();
private void timerLog_Tick(object sender, EventArgs e)
{
lock (logs)
{
for (int i = 0; i < logs.Count; i++)
{
editLog.AppendText(logs.Dequeue());
editLog.SelectionStart = editLog.TextLength;
editLog.ScrollToCaret();
}
}
}
private void log(string text, params object[] values)
{
text = string.Format(text, values) + Environment.NewLine;
if (editLog.InvokeRequired)
{
editLog.BeginInvoke(
(MethodInvoker)delegate
{
lock (logs)
{
logs.Enqueue(text);
}
//this.log(text, values);
});
}
else
{
logs.Enqueue(text);
}
}