我有一个需要刷新数据网格视图的功能。当我部署程序时,它会自动关闭程序。
我身边有一台MSSQL Server。我试图修复几次功能,但只能在我的环境中正常工作。
这是我的代码。
private async void RefreshDataGrid()
{
Console.Write(_workstation);
bool uiMarshal = DGView.InvokeRequired;
var cards = (from view in _database.RandomCheckViews
where view.Workstation == _workstation
&& view.Client_Name == _clientName
orderby view.ID
select view).ToList();
if (uiMarshal)
{
DGView.BeginInvoke(new MethodInvoker(delegate
{
DGView.DataSource = cards;
DGView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
foreach (DataGridViewRow cardData in DGView.Rows)
if (cardData.Cells["Result"].Value.ToString() == "Failed")
cardData.DefaultCellStyle.BackColor = Color.Red;
DGView.Refresh();
}));
}
else
{
await Task.Delay(1000);
await Task.Run(() => DGView.DataSource = cards);
await Task.Run(() => DGView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells);
foreach (DataGridViewRow cardData in DGView.Rows)
if (cardData.Cells["Result"].Value.ToString() == "Failed")
cardData.DefaultCellStyle.BackColor = Color.Red;
await Task.Run(() => DGView.Refresh());
}
}
在部署计算机上检查了几次之后,这里是Windows Event Viewer的内容。
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Events><Event xmlns='http://schemas.microsoft.com/win/2004/08/events/event'><System><Provider Name='.NET Runtime'/><EventID Qualifiers='0'>1026</EventID><Level>2</Level><Task>0</Task><Keywords>0x80000000000000</Keywords><TimeCreated SystemTime='2019-10-29T06:24:24.000000000Z'/><EventRecordID>47775</EventRecordID><Channel>Application</Channel><Computer>EMV53.dzpemv.net</Computer><Security/></System><EventData><Data>Application: COM_Caller_CS.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Data.Entity.Core.EntityCommandExecutionException
Stack:
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__5(System.Object)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
</Data></EventData><RenderingInfo Culture='en-US'><Message>Application: COM_Caller_CS.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Data.Entity.Core.EntityCommandExecutionException
Stack:
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>b__5(System.Object)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
</Message><Level>Error</Level><Task></Task><Opcode>Info</Opcode><Channel></Channel><Provider></Provider><Keywords><Keyword>Classic</Keyword></Keywords></RenderingInfo></Event></Events>
答案 0 :(得分:0)
前段时间有一个类似的问题,如果我没记错的话,那是异步无效的问题,您也在使用。
尝试使用任务
private async Task RefreshDataGrid()