使用线程插入数据时出错抛出异常
Error throw exception when insert data with thread
我在winform中使用thread时有问题。
我在调试程序时出错。
我的应用程序在启动程序时抛出异常。
我定义class RunInUIThread
是:
private void RunInUIThread(Delegate method)
{
this.BeginInvoke(method);
}
在 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.
}));
为什么我的代码不起作用。有人可以向我解释以及如何解决问题吗?
谢谢大家reader!!!
@JoelLegaspiEnriquez,你建议我删除 Program.cs 中的 [STAThread]
?
如果我评论这一行。这个在控制上有问题 AxLiveX1是控制camera ip.
txtKhuVucBai.Text = mReader.CurrentCardIDBlock1.ToString();
是16字节的Guid类型:8d58d690-6b71-4ee8-85ad-006db0287bf1.
但我将 txtKhuVucBai 分配给 Guid 类型是:
private Guid mCurrentCardIDBlock1;
public Guid CurrentCardIDBlock1
{
get { return mCurrentCardIDBlock1; }
}
mCurrentCardIDBlock1 是 RFID reader 类型,随机 32 个字符。
我的意思是尝试 运行 没有 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.
}
这是为了在主线程中调试您的代码。
我在winform中使用thread时有问题。 我在调试程序时出错。
我的应用程序在启动程序时抛出异常。
我定义class RunInUIThread
是:
private void RunInUIThread(Delegate method)
{
this.BeginInvoke(method);
}
在 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.
}));
为什么我的代码不起作用。有人可以向我解释以及如何解决问题吗? 谢谢大家reader!!!
@JoelLegaspiEnriquez,你建议我删除 Program.cs 中的 [STAThread]
?
如果我评论这一行。这个在控制上有问题 AxLiveX1是控制camera ip.
txtKhuVucBai.Text = mReader.CurrentCardIDBlock1.ToString();
是16字节的Guid类型:8d58d690-6b71-4ee8-85ad-006db0287bf1.
但我将 txtKhuVucBai 分配给 Guid 类型是:
private Guid mCurrentCardIDBlock1;
public Guid CurrentCardIDBlock1
{
get { return mCurrentCardIDBlock1; }
}
mCurrentCardIDBlock1 是 RFID reader 类型,随机 32 个字符。
我的意思是尝试 运行 没有 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.
}
这是为了在主线程中调试您的代码。