gpt4 book ai didi

c# - InitializeComponent 和调用/锁定时出现“对象正在别处使用”错误

转载 作者:行者123 更新时间:2023-11-30 18:39:44 27 4
gpt4 key购买 nike

我有一个带有文本框的应用程序,我需要从多个线程更新文本框。由于我正在从多个线程更新文本框,因此我使用以下代码来确保在必要时从主线程调用它 - 但即使使用此代码我仍然会收到错误 - 特别是“对象当前正在其他地方使用” .

我使用的代码:

private static readonly object setTextLockObject = new object();
delegate void SetTextCallBack(XtraForm Form, string ControlToUpdate, string ControlValue);

public void UpdateControlText(XtraForm Form, string ControlToUpdate, string ControlValue)
{
try
{
if (Form.Controls[ControlToUpdate].InvokeRequired)
{
SetTextCallBack callBackHandler = UpdateControlText;
IAsyncResult invokeResult = Form.Controls[ControlToUpdate].BeginInvoke(callBackHandler, Form, ControlToUpdate, ControlValue);
Form.Controls[ControlToUpdate].EndInvoke(invokeResult);
}
else
{
try
{
lock (setTextLockObject)
{
Form.Controls[ControlToUpdate].Text = ControlValue.Translate();
}
}
catch (Exception x)
{
UpdateStatus(string.Format("ControlText1: {0} ControlToUpdate={1}ControlText={2}", x.Message, ControlToUpdate, ControlValue));
}
}
}
catch (Exception ex)
{
UpdateStatus(string.Format("ControlText2: {0} ControlToUpdate={1} ControlText={2}", ex.Message, ControlToUpdate, ControlValue));
}
}

我正在用调用更新文本,并确保锁定对象,以便另一个线程在更新时不能访问对象。我希望第二个线程等待锁被释放,但我得到的是“对象当前正在使用中”。有人可以帮助我了解我在这里做错了什么吗?

我有一个更大的问题,即在表单上执行应用程序时 - 在 InitializeComponent 中,我还收到“对象当前正在别处使用”。这是一个新对象,未在其他任何地方使用。为什么在初始化组件时可能会出现此错误?

at System.Drawing.Graphics.get_PageUnit()
at DevExpress.Utils.Text.FontsCache.GetFontCacheByFont(Graphics graphics, Font font)
at DevExpress.Utils.Text.FontsCache.GetStringSize(Graphics graphics, String text, Font font, StringFormat stringFormat, Int32 maxWidth)
at DevExpress.Utils.Text.TextUtils.GetStringSize(Graphics g, String text, Font font, StringFormat stringFormat, Int32 maxWidth)
at DevExpress.Utils.Paint.XPaintMixed.CalcTextSize(Graphics g, String s, Font font, StringFormat strFormat, Int32 maxWidth)
at DevExpress.Utils.AppearanceObject.CalcTextSize(Graphics g, StringFormat sf, String s, Int32 width)
at DevExpress.XtraEditors.ViewInfo.LabelControlViewInfo.CalcSimpleTextSize(String Text, Boolean useHotkeyPrefix, LabelAutoSizeMode mode, Int32 predWidth)
at DevExpress.XtraEditors.ViewInfo.LabelControlViewInfo.CalcTextSize(String Text, Boolean useHotkeyPrefix, LabelAutoSizeMode mode, Int32 predWidth)
at DevExpress.XtraEditors.LabelControl.GetPreferredSize(Size proposedSize)
at DevExpress.XtraEditors.LabelControl.SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
at System.Windows.Forms.Control.System.Windows.Forms.Layout.IArrangedElement.SetBounds(Rectangle bounds, BoundsSpecified specified)
at System.Windows.Forms.Layout.CommonProperties.SetAutoSize(IArrangedElement element, Boolean value)
at System.Windows.Forms.Control.set_AutoSize(Boolean value)
at DevExpress.XtraEditors.LabelControl.set_AutoSizeMode(LabelAutoSizeMode value)
at AccessControl.frmRefillCard.InitializeComponent()

如有任何帮助,我们将不胜感激。

最佳答案

我认为让 UpdateControlText 方法在 UI 线程上调用自身会使事情复杂化。 BeginInvokeEndInvoke 也没有意义,因为您连续调用它们。

怎么样:

public void UpdateControlText(XtraForm Form, string ControlToUpdate, string ControlValue)
{
if (Form.Controls[ControlToUpdate].InvokeRequired)
{
try
{
Form.Controls[ControlToUpdate]
.Invoke(() => Form.Controls[ControlToUpdate].Text = ControlValue.Translate());
}
catch (Exception x)
{
UpdateStatus(string.Format("ControlText1: {0} ControlToUpdate={1}ControlText={2}",
x.Message, ControlToUpdate, ControlValue));
}
}
}

关于c# - InitializeComponent 和调用/锁定时出现“对象正在别处使用”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9254440/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com