I had a function which I need to Refresh the data grid view. When I deploy the program it automatically close the program.
I have a MSSQL Server in my side. I tried to fix my function several times but it only works fine in my environment.
Here is my code.
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());
}
}
After I checked several times on our deployment pc, here what I got on 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>
had a similar problem some time ago, if i remember correctly it was a problem with async void, which you are using aswell.
Try with using a Task
private async Task RefreshDataGrid()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.