简体   繁体   English

调用线程无法访问此对象,因为其他线程不同

[英]The calling thread cannot access this object because a different thread

Every time I try to set the any of the label text properties, it complains about not being in the same thread. 每次我尝试设置任何标签文本属性时,它都会抱怨不在同一个线程中。 That's kind of confusing since the code is inside an event handler. 这有点令人困惑,因为代码在事件处理程序中。

The same works alright with the pictureBox . 使用pictureBox同样适用。

How can this be modified to work as expected? 如何修改它以按预期工作?

public partial class Form3 : Form
{
    AttendanceControlDevice control;

    public Form3()
    {
        InitializeComponent();
    }

    private void Form3_Load(object sender, EventArgs e)
    {
        string conn = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
        control = new AttendanceControlDevice(new EmployeesRepository(new SqlConnection(conn)), new zkemkeeper.CZKEMClass());
        control.OnEmployeeChecked += new EventHandler<EmployeeCheckedEventArgs>(control_OnEmployeeChecked);
        control.Connect(ConfigurationManager.AppSettings["ip"], int.Parse(ConfigurationManager.AppSettings["port"])); 
    }

    void control_OnEmployeeChecked(object sender, EmployeeCheckedEventArgs e)
    {
        try
        {
            label1.Text = e.Employee.Id;
            label2.Text = e.Employee.Name;
            pictureBox1.Image = Image.FromFile(e.Employee.Picture);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
}

public class AttendanceControlDevice
{
   ...

    public bool Connect(string ip,int port)
    {
        _connected = _commChannel.Connect_Net(ip, port);
        if(!_connected)
        {
            _commChannel.GetLastError(ref _lastError);
            Error = _errorDescriptions[_lastError];
        }else{
            _commChannel.RegEvent(1, (int)deviceEvents.OnAttTransaction);
        _commChannel.OnAttTransactionEx += new zkemkeeper._IZKEMEvents_OnAttTransactionExEventHandler(commChannel_OnAttTransactionEx);
        }
        return _connected;
    }


    void commChannel_OnAttTransactionEx(string EnrollNumber, int IsInValid, int AttState, int VerifyMethod, int Year, int Month, int Day, int Hour, int Minute, int Second, int WorkCode)
    {
        if(OnEmployeeChecked != null)
        {
            Employee employee = null;

            try{
                employee = _employees.Get(EnrollNumber);
            }
            catch { 
                employee = new Employee(EnrollNumber, "Error while reading the data", ""); 
            }

            if (employee == null) employee = new Employee(EnrollNumber, "Could not match the id", "");

            OnEmployeeChecked.Invoke(this, new EmployeeCheckedEventArgs(employee));
        }
    }

} }

My guess would be that AttendanceControlDevice is raising the event on a background thread, or (more likely) the Repository is raising some event on a background thread and that is propagating through the AttendanceControlDevice and thus you are still on a background thread when the event fires. 我的猜测是AttendanceControlDevice在后台线程上引发事件,或者(更有可能)Repository在后台线程上引发一些事件并且通过AttendanceControlDevice传播,因此当事件触发时你仍然在后台线程上。

You probably know this, but you should check for InvokeRequired and Invoke/BeginInvoke appropriately. 您可能知道这一点,但您应该适当地检查InvokeRequired和Invoke / BeginInvoke。

It maybe because of thread which AttendanceControlDevice on is different with current thread. 这可能是因为AttendanceControlDevice所使用的线程与当前线程不同。

You should make the thread-safe call to Windows Forms Controls in this case as you may know (MSDN link: http://msdn.microsoft.com/en-us/library/ms171728.aspx ) 在这种情况下,您应该对Windows Forms Controls进行线程安全调用,如您所知(MSDN链接: http//msdn.microsoft.com/en-us/library/ms171728.aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用线程无法访问该对象,因为其他线程拥有它错误 - The calling thread cannot access this object because a different thread owns it error 调用线程无法访问此对象,因为另一个线程拥有它 - The calling thread cannot access this object because a different thread owns it 调用线程无法访问此对象,因为另一个线程拥有它“异常” - The calling thread cannot access this object because a different thread owns it “exception” WPF:调用线程无法访问此对象,因为其他线程拥有它 - WPF: The calling thread cannot access this object because a different thread owns it 调用线程无法访问此 object,因为不同的线程拥有它 - The calling thread cannot access this object because a different thread owns it 调用线程无法访问此 object,因为不同的线程拥有它 - The calling thread cannot access this object because a different thread owns it 调用线程无法访问此对象,因为其他线程拥有 - The calling thread cannot access this object because a different thread owns 调用线程无法访问该对象,因为其他线程拥有它 - The calling thread cannot access this object because a different thread owns it 调用线程无法访问该对象,因为其他线程拥有它 - The calling thread cannot access this object because a different thread owns it 调用线程无法访问此对象,因为另一个线程拥有它 - The calling thread cannot access this object because a different thread owns it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM