使用C#代码重新启动调试 [英] restart debuging with c# code

查看:119
本文介绍了使用C#代码重新启动调试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,
我正在用C#开发一个Windows应用程序,因为我每隔一小时发送一次电子邮件.现在,如果它无法像这样发送邮件,我想重新启动调试.


代码


I am developing one windows application in C# in that i am sending emails for every one hour. Now i want to restart debugging if its fail to send mail like this.


code

}
                            smtpClient.Send(mail);
                            Log("------------------------------------------------------(Lifting Entry Report)Send Successfully at " + dt);
                        }
                    }
                }
                tmr_SendEmail.Enabled = true;
            }
            catch (Exception ex)
            {
                if (con.State == ConnectionState.Open) con.Close();
                Log("----------------------------------------------------------(Lifting Entry Report)ERROR IN SEND EMAIL: " + ex.Message + DateTime.Now);
*****<big>Now here i want to restart debugging code if it  is fail to send</big> email. 
                tmr_SendEmail.Enabled = true;
            }

推荐答案

请参阅我对问题的评论.不太清楚.

如果我猜对了主意,则在某些操作(例如发送电子邮件)失败时需要保持调试过程,然后自动执行(因为否则,您始终可以手动执行).不幸的是,您没有共享应用程序类型.

因此,现在,我将尝试基于一个简单的控制台应用程序为您提供一个想法.如果您能够掌握它,则可以将其应用于不同的情况.如果没有,请回复-我们可以进一步讨论.

我认为您无法完全像使用调试器手动启动应用程序那样完全重启应用程序,而根本没有任何手动操作.您可以做不同的事情:make在逻辑上与重新启动是相同的.严格来说,这仅在不使用任何静态字段/属性的情况下才有效,否则重复调试可能与第一次运行并不完全相同.实际上,您可以忽略该问题,然后在真实"设置中重新测试该功能.

因此,考虑您有一些可以完成所有操作的控制台应用程序.假设它看起来像这样(简化):

Please see my comments to the question. Not quite clear.

If I guess the idea correctly, you need to keep debugging process when you have a failure of a certain operation like sending a e-mail and do it automatically (because otherwise you can always do it manually). Unfortunately, you did not share the application type.

So, I''ll try to give you an idea based on a simple console application for now. If you can grasp it, you will be able to adopt it to different situations. If not, please respond — we can further discuss it.

I don''t think you can restart the application exactly as you would do it manually using the debugger, without any manual operations at all. You can do different thing: make is the way is logically the same as restarting. Strictly speaking, this is valid only if you are not using any static fields/properties, otherwise repeated debugging may be not strictly the same as in the first run. Practically, you can ignore the problem and later re-test the functionality in "real" settings.

So, consider you have some console application which does it all. Let''s assume it looks like this (simplified):

class Program {

   void SendMail(/* ... */) {
      //...
      smtpClient.Send(mail);
      //...
   }

   static void Main() {
      //...
      SendMail(/* ... */); // in some loops, recursions, etc., called many times
      //...
   }

   //...
}



您可以通过以下方式对其进行修改:



You can modify it in this way:

class ResetException : System.ApplicationException {
   internal ResetException(System caughtException) : base(null) {
       InnerException = caughtException;
   }
}

class Program {

   void SendMail(/* ... */) {
      //...
      try {
          smtpClient.Send(mail);
      } catch (System.Exception e) {
          throw new ResetException(e); // this is the key
      } //exception
      //...
   } //SendMail

   static void ImplementMain() {
      //...
      SendMain(/* ... */); // in some loops, recursions, etc., called many times
      //...
   } //ImplementMain

   static void Main() {
      while (true) {
          try {
              ImplementMain();
          } catch(ResetException e) {
              System.Exception exceptionToLog = e.InnerException;
              Log(exceptionToLog); //somehow
              // exception propagation is blocked here, after logging, but don't do it in other cases
              // (as a rule of thumb)
          } //
      } //loop, ImplementMain restarts in case of ResetException
   } //Main

   //...
}



你有主意吗?您可以放置​​任何断点,并且在发生故障的情况下,只需按F5键即可继续调试而无需手动重启,同时能够分析故障.请注意,仅当抛出异常并且仅针对特定选择的代码片段捕获该异常时,才会重新启动应用程序" ImplementMain,否则整个应用程序将被终止,但是您也可以捕获其他异常.至关重要的是,永远不要阻止异常的传播,也许除了每个线程的堆栈的最顶层堆栈帧.

如果这不能为您提供所需的技术,请回复;我会很乐意讨论它.使用UI应用程序,解决方案将更加复杂,但是非常笼统的想法几乎是相同的.

—SA



Are you getting the idea? You can put any breakpoints, and, in case of failure, simply hit F5 to continue debugging without manual restarts, while being able to analyze the failures. Pay attention that the "application" ImplementMain is restarted only if the exception was thrown and caught only for specially chosen fragment(s) of code, otherwise the whole application will be terminated, but you can catch other exceptions, too. It''s critically important that you never block the propagation of the exception, perhaps except the very top stack frame of the stack of each thread.

If this does not provide you with a technique you want, please respond; I''ll gladly discuss it. With UI application the solution will be more complex, but the very general idea is pretty much the same.

—SA


这篇关于使用C#代码重新启动调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆