简体   繁体   English

使用线程插入数据时引发错误抛出异常

[英]Error throw exception when insert data with thread

I have probem when use thread in winform. 我在winform中使用线程时遇到问题。 I have error when debug program. 调试程序时出现错误。

My Application throw exception when start program. 启动程序时,我的应用程序引发异常。

启动程序时我的应用程序引发异常

I define class RunInUIThread is: 我定义的class RunInUIThread是:

private void RunInUIThread(Delegate method)
{
    this.BeginInvoke(method);
}

And in RunInUIThread method like: 并在RunInUIThread方法中,例如:

BaiXeBUS baixe = new BaiXeBUS();
RunInUIThread(new ThreadStart(delegate ()
{
          BaiXeDTO obj = new BaiXeDTO();      //Map all to define database 
          txtKhuVucBai.Text = mReader.CurrentCardIDBlock1.ToString();
          txtMaThe.Text = mReader.CurrentCardIDBlock2.ToString();

          //If I comment all below code. It's work. But I need Insert data to database.
          txtKhuVucBai.Text = obj.IDBaiXe.ToString();
          txtMaThe.Text = obj.IDRF.ToString();
          obj.BienSoXe = textBox1.Text;
          obj.HinhBienSo = color.ToString();
          obj.HinhChuXe = img.ToString();
          obj.ThoiGianVao = DateTime.Now.ToLocalTime();
          obj.ThoiGianRa = DateTime.Now.ToLocalTime();
          baixe.BaiXe_Insert(obj); //Contain data access layer to insert data with store procedure.
}));

Why my code not work. 为什么我的代码不起作用。 Someone can explain me and how to fix problem? 有人可以解释我以及如何解决问题? Thank all reader!!! 谢谢所有读者!!!

@JoelLegaspiEnriquez, your recommned me to remove [STAThread] in Program.cs? @JoelLegaspiEnriquez,您建议我删除Program.cs中的[STAThread]吗?

If I comment this line. 如果我对此行发表评论。 This have problem in control AxLiveX1 is control of camera ip. 在控制中有问题AxLiveX1是摄像机ip的控制。

调试时出现异常错误

The txtKhuVucBai.Text = mReader.CurrentCardIDBlock1.ToString(); txtKhuVucBai.Text = mReader.CurrentCardIDBlock1.ToString(); is Guid type with 16byte: 8d58d690-6b71-4ee8-85ad-006db0287bf1. 是16字节的Guid类型:8d58d690-6b71-4ee8-85ad-006db0287bf1。

But i assign txtKhuVucBai to Guid type is: 但是我将txtKhuVucBai分配给Guid类型是:

private Guid mCurrentCardIDBlock1;
public Guid CurrentCardIDBlock1
{
        get { return mCurrentCardIDBlock1; }
}

The mCurrentCardIDBlock1 is type of RFID reader with 32 character random. mCurrentCardIDBlock1是具有32个随机字符的RFID阅读器。

What I mean is trying to run this block of code without the ThreadStart 我的意思是试图在没有ThreadStart情况下运行此代码块

{
    BaiXeDTO obj = new BaiXeDTO();      //Map all to define database 
    txtKhuVucBai.Text = mReader.CurrentCardIDBlock1.ToString();
    txtMaThe.Text = mReader.CurrentCardIDBlock2.ToString();

    //If I comment all below code. It's work. But I need Insert data to database.
    txtKhuVucBai.Text = obj.IDBaiXe.ToString();
    txtMaThe.Text = obj.IDRF.ToString();
    obj.BienSoXe = textBox1.Text;
    obj.HinhBienSo = color.ToString();
    obj.HinhChuXe = img.ToString();
    obj.ThoiGianVao = DateTime.Now.ToLocalTime();
    obj.ThoiGianRa = DateTime.Now.ToLocalTime();
    baixe.BaiXe_Insert(obj); //Contain data access layer to insert data with store procedure.
}

This is to debug your code within the main thread. 这是为了在主线程中调试代码。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM