【发布时间】:2017-06-29 01:00:43
【问题描述】:
我使用 Devexpress GridControl。我想根据 tcp 消息更改单元格值。
private void Form1_Load(object sender, EventArgs e)
{
grid_mygrid.DataSource = Get_Devices_Info.Get_Device_From_DB().Tables[0];
this.tcpListener = new TcpListener(IPAddress.Any, 2000);
this.listenThread = new Thread(new ThreadStart(ListenForClients));
this.listenThread.Priority = ThreadPriority.Highest;
this.listenThread.Start();
}
private void ListenForClients()
{
this.tcpListener.Start();
while (true)
{
TcpClient client = this.tcpListener.AcceptTcpClient();
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
clientThread.Start(client);
}
}
private void HandleClientComm(object client)
{
...
try
{
gridView1.SetRowCellValue(row_index, "boot_progress", 50);
}
catch(Exception e)
{
if (e.InnerException != null)
{
Log2LogText(e.InnerException.Message);
}
}
...
}
如果我想在线程下试试这个
gridView1.SetRowCellValue(row_index, "boot_progress", 50);
如果 row_index=0 -> 没关系
if row_index>0 -> 在 mscorlib.dll 中发生了“System.Reflection.TargetInvocationException”类型的未处理异常
关于 GridControl 如果我点击按钮尝试此操作
gridView1.SetRowCellValue(1, "boot_progress", 50); -> It is OK
gridView1.SetRowCellValue(2, "boot_progress", 50); -> It is OK
gridView1.SetRowCellValue(4, "boot_progress", 50); -> There is no row with index 4. But there is no error. It is OK.
gridView1.SetRowCellValue(5, "random_column", 50); -> There is no column with "random_column". But there is no error. It is OK.
我使用 try-catch 但此代码仍然给出错误并停止程序。当我评论这一行时
gridView1.SetRowCellValue(row_index, "boot_progress", 50);
没问题
我正在等待您的建议。
【问题讨论】:
-
读取 InnerException。
-
UI 不是线程安全的。您需要在 UI 线程上运行。
-
您应该将您的 UI 代码与您的 TCP / 逻辑分开。
-
@SLaks 谢谢。我解决了这个问题。
标签: c# multithreading devexpress tcplistener gridcontrol