Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HI,
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
C#
}
                            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;
            }
Posted
Comments
Sergey Alexandrovich Kryukov 16-Aug-12 1:43am    
Why? What do you want to achieve by that? What's your requirement for such restarting. (Automatic, or what?) What is the application type (it does matter)?
--SA
dan!sh 16-Aug-12 1:48am    
What do you mean you want start debugging code? Put a breakpoint within the catch block and if it's hit you can debug. Are you referring to do this in the deployed application?
Sergey Alexandrovich Kryukov 16-Aug-12 2:39am    
You are right. However, in real life this technique of debugging might not be the best in case of high volume of debugging and frequent exception in some limited fragments of code which interfere with debugging of unrelated problems, for example. I also faced such inconveniences. Hope you will get the idea.

So, I've done some guess, with the solution which probably makes some sense -- please see my answer.
--SA

1 solution

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):

C#
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:
C#
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

   //...
}


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
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900