【问题标题】:How to fix .Net Runtime unhandled exception如何修复 .Net 运行时未处理的异常
【发布时间】:2019-10-29 07:15:05
【问题描述】:

我有一个需要刷新数据网格视图的功能。当我部署程序时,它会自动关闭程序。

我身边有一个 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 事件查看器上得到的结果如下。

<?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.&lt;ThrowAsync&gt;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.&lt;ThrowAsync&gt;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>

【问题讨论】:

标签: c# wpf entity-framework asynchronous entity-framework-core


【解决方案1】:

前段时间遇到过类似的问题,如果我没记错的话,那是你也在使用的 async void 的问题。

尝试使用任务

private async Task RefreshDataGrid()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 2016-04-27
    • 1970-01-01
    • 2012-12-09
    相关资源
    最近更新 更多