none
Try Catch Wait Retry? RRS feed

  • Question

  • How do you wait after a catch phrase and then retry for example

    Try{

    /do something

    }

    catch

    {

    // wait 10 seconds

    //retry

    }

    Also can Thread.sleep be used here? I've seen a lot of people mention Thread.sleep is bad. Why is that? And what is the alternative way to wait.

    Thank you!

    Wednesday, February 11, 2015 4:59 AM

Answers

  • I have already tried Thread.Sleep but it makes other stuff on my form to not work properly. For example a label that should appear when this exception occurs does not appear if I put the Thread.Sleep. And more importantly nothing on my Form shows up if this exception with Thread.Sleep occurs on the FormLoad.

    Any other way to wait inside a catch phrase?


    Hi SnowTauren,

    For example if we used

    System.Threading.Thread.Sleep(5000)

    It should never be executed in the main UI thread as it will cause your application to hang for 5 seconds at least (if it loops only once)

    From my point of view, do not like using Sleep for thread synchronization. .NET offers a bunch of classes that are perfect for thread synchronization, WaitHandle being one of them. Good Luck!

    Best wishes!

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, February 13, 2015 9:06 AM
    Moderator

All replies

  • put Try/Catch block in loop and use thread.sleep, when your work is done exit the loop.
    Wednesday, February 11, 2015 6:53 AM
  • void doWork()
    {
     var workCompleted = false;
     
     while(!workCompleted) 
     {
      try
      {
       // do the work which can throw exception
       
       ...
       
       // great, work completed without exception
       workCompleted = true;
      } 
      catch(Exception)
      {
       // oh, there is some exception lets sleep for 10 seconds
       Thread.Sleep(10000);
      }  
     }
    }

    Wednesday, February 11, 2015 7:04 AM
  • Why Thread.Sleep()?

    Life is not so easy.......

    Wednesday, February 11, 2015 7:56 AM
  • [Content deleted because it was a literal copy from a post in another forum and no credit was given. Another reply further down this thread contains a link to the original article in case you want to see the code.]

    Wednesday, February 11, 2015 8:10 AM
  • I have already tried Thread.Sleep but it makes other stuff on my form to not work properly. For example a label that should appear when this exception occurs does not appear if I put the Thread.Sleep. And more importantly nothing on my Form shows up if this exception with Thread.Sleep occurs on the FormLoad.

    Any other way to wait inside a catch phrase?

    Thursday, February 12, 2015 3:12 AM
  • I have already tried Thread.Sleep but it makes other stuff on my form to not work properly. For example a label that should appear when this exception occurs does not appear if I put the Thread.Sleep. And more importantly nothing on my Form shows up if this exception with Thread.Sleep occurs on the FormLoad.

    Any other way to wait inside a catch phrase?


    Hi SnowTauren,

    For example if we used

    System.Threading.Thread.Sleep(5000)

    It should never be executed in the main UI thread as it will cause your application to hang for 5 seconds at least (if it loops only once)

    From my point of view, do not like using Sleep for thread synchronization. .NET offers a bunch of classes that are perfect for thread synchronization, WaitHandle being one of them. Good Luck!

    Best wishes!

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, February 13, 2015 9:06 AM
    Moderator
  • Wow. Line by line you copied this code from this stack overflow post without crediting the original author. Congratulations

    https://stackoverflow.com/a/1563234

    Sunday, June 16, 2019 12:42 AM