';对象正在其他地方使用';InitializeComponent和invoke/lock上出错

本文关键字:invoke 出错 lock 对象 其他 方使用 InitializeComponent | 更新日期: 2023-09-27 18:24:19

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

我使用的代码:

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));
    }
}

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

我有一个更大的问题,在对表单进行应用程序调试时,在InitializeComponent中,我还得到"Object当前正在其他地方使用"。这是一个新对象,不会在其他任何地方使用。为什么在初始化组件时可能会出现此错误?

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()

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

';对象正在其他地方使用';InitializeComponent和invoke/lock上出错

我认为让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));
        }
    }
}