何らかの理由で、「InvalidOperationException:オブジェクトは現在他の場所で使用されています」というメッセージが表示されます。
以下のカスタムOnPaintの実行中(実際には、コードの行コピーのほぼ1行です...そこにはほとんどありません)。
以下の例外ハンドラーにログインして、UI以外のスレッドからOnPaintを呼び出しているかどうかを検出します...これはトリップされませんが、エラーがログに記録されます(以下のスタックトレースを参照)。
これらのエラーが発生しているマシンでは、他のコントロールからの恐ろしいRed Xの運命も見られます(おそらく、OnPaintsの周りにtry / catchがありません)。
それらはおそらく関連していますが、このコードがUIスレッドからのみ呼び出された場合、そのエラーの原因を特定することはできません。
何か案は?
これはスタックトレースです。
System.InvalidOperationException:オブジェクトは現在他の場所で使用されています。
System.Drawing.Graphics.CheckErrorStatus(Int32 status)
at System.Drawing.Graphics.DrawRectangle(Pen pen、Int32 x、Int32 y、Int32 width、Int32 height)
at System.Windows.Forms.ControlPaint.DrawBorderSimple(Graphics graphics、
System.Windows.Forms.ControlPaint.DrawBorder(グラフィックグラフィックス、長方形の境界、色の色、ButtonBorderStyleスタイル)の
MyUserControl.OnPaint(PaintEventArgs e)の長方形の境界、色の色、ButtonBorderStyleスタイル)
これはクラスです:
public class MyUserControl : UserControl
{
// Override this to set your custom border color
protected Color mBorderColor = SystemColors.ControlDarkDark;
public MyeUserControl()
: base()
{
this.BorderStyle = BorderStyle.None;
this.Padding = new Padding(1);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
try
{
ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, mBorderColor, ButtonBorderStyle.Solid);
}
catch (Exception ex)
{
// check if we're not on the UI thread, and if not, log it
// log exception
}
}
}