<legend id='KDGsR'><style id='KDGsR'><dir id='KDGsR'><q id='KDGsR'></q></dir></style></legend>
    • <bdo id='KDGsR'></bdo><ul id='KDGsR'></ul>
  1. <small id='KDGsR'></small><noframes id='KDGsR'>

    <tfoot id='KDGsR'></tfoot>

      <i id='KDGsR'><tr id='KDGsR'><dt id='KDGsR'><q id='KDGsR'><span id='KDGsR'><b id='KDGsR'><form id='KDGsR'><ins id='KDGsR'></ins><ul id='KDGsR'></ul><sub id='KDGsR'></sub></form><legend id='KDGsR'></legend><bdo id='KDGsR'><pre id='KDGsR'><center id='KDGsR'></center></pre></bdo></b><th id='KDGsR'></th></span></q></dt></tr></i><div id='KDGsR'><tfoot id='KDGsR'></tfoot><dl id='KDGsR'><fieldset id='KDGsR'></fieldset></dl></div>

      BeginInvoke 抛出异常

      时间:2023-11-11
      <tfoot id='G9pVE'></tfoot>
            <legend id='G9pVE'><style id='G9pVE'><dir id='G9pVE'><q id='G9pVE'></q></dir></style></legend>

              <bdo id='G9pVE'></bdo><ul id='G9pVE'></ul>

              <i id='G9pVE'><tr id='G9pVE'><dt id='G9pVE'><q id='G9pVE'><span id='G9pVE'><b id='G9pVE'><form id='G9pVE'><ins id='G9pVE'></ins><ul id='G9pVE'></ul><sub id='G9pVE'></sub></form><legend id='G9pVE'></legend><bdo id='G9pVE'><pre id='G9pVE'><center id='G9pVE'></center></pre></bdo></b><th id='G9pVE'></th></span></q></dt></tr></i><div id='G9pVE'><tfoot id='G9pVE'></tfoot><dl id='G9pVE'><fieldset id='G9pVE'></fieldset></dl></div>
                <tbody id='G9pVE'></tbody>

                <small id='G9pVE'></small><noframes id='G9pVE'>

                本文介绍了BeginInvoke 抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有以下问题.FindRoot 实际上在第三方 dll 中,我无法控制它.必须通过 Begin invoke 调用.有时,FindRoot 方法会引发异常.这会导致我的整个应用程序崩溃.现在如何防止我的应用程序崩溃,即使 FindRoot 抛出异常.

                I have the following problem. FindRoot is actually in a third party dll and I do not have control over it. It has to be called via Begin invoke. Sometimes, the FindRoot method throws exception. This causes my whole application to crash. Now how do I prevent my application from crashing even if FindRoot throws exception.

                delegate void AddRoot(double number);
                public static void FindRoot(double number)
                {
                    throw new Exception();/// sometimes is thrown.
                
                }
                
                static void back_DoWork(object sender, DoWorkEventArgs e)
                {
                    AddRoot root = FindRoot;
                    root.BeginInvoke(12.0, root.EndInvoke, root);
                
                }
                

                推荐答案

                使用回调而不是直接调用EndInvoke:

                Use a callback instead of directly calling EndInvoke:

                using System.Runtime.Remoting.Messaging;
                ...
                static void back_DoWork() 
                {
                    AddRoot root = FindRoot;
                    root.BeginInvoke(12.0, new AsyncCallback(callback), root);
                }
                
                static void callback(IAsyncResult result) 
                {
                    AddRoot dlg = (AddRoot)(((AsyncResult)result).AsyncDelegate);
                
                    try 
                    {
                        dlg.EndInvoke(result);
                    }
                    catch (Exception ex) 
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
                

                顺便说一句:在我看来你已经从后台线程调用了这段代码.启动另一个线程来运行 FindRoot() 看起来很奇怪.

                Btw: it looks to me like you are already calling this code from a background thread. Starting yet another thread to run FindRoot() looks strange.

                这篇关于BeginInvoke 抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:代表的@前缀有什么特殊含义吗? 下一篇:在 C# 中使用免费的泛型类型参数模拟委托

                相关文章

                <tfoot id='JEekI'></tfoot>
                • <bdo id='JEekI'></bdo><ul id='JEekI'></ul>

                    <legend id='JEekI'><style id='JEekI'><dir id='JEekI'><q id='JEekI'></q></dir></style></legend>
                  1. <i id='JEekI'><tr id='JEekI'><dt id='JEekI'><q id='JEekI'><span id='JEekI'><b id='JEekI'><form id='JEekI'><ins id='JEekI'></ins><ul id='JEekI'></ul><sub id='JEekI'></sub></form><legend id='JEekI'></legend><bdo id='JEekI'><pre id='JEekI'><center id='JEekI'></center></pre></bdo></b><th id='JEekI'></th></span></q></dt></tr></i><div id='JEekI'><tfoot id='JEekI'></tfoot><dl id='JEekI'><fieldset id='JEekI'></fieldset></dl></div>

                    <small id='JEekI'></small><noframes id='JEekI'>