Building Timeout in C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pietje de kort

    #1

    Building Timeout in C#

    Hello all,

    I am trying to build a class that does something, and may timeout while
    doing so. Ofcouse I want to be a bit elegant, so I came up with the code
    found below. Problem is, I can't catch the exception thrown on timeout.
    Anybody knows how to correctly implement timeout behaviour?

    Best regards, Wouter van Vugt
  • William Stacey

    #2
    Re: Building Timeout in C#

    Hi Wouter. You forgot the "code below"...

    --
    William Stacey, MVP

    "Pietje de kort" <woutervu@hotma il.com> wrote in message
    news:e09cc77a.0 310122209.46068 7fb@posting.goo gle.com...[color=blue]
    > Hello all,
    >
    > I am trying to build a class that does something, and may timeout while
    > doing so. Ofcouse I want to be a bit elegant, so I came up with the code
    > found below. Problem is, I can't catch the exception thrown on timeout.
    > Anybody knows how to correctly implement timeout behaviour?
    >
    > Best regards, Wouter van Vugt[/color]


    Comment

    • Pietje de kort

      #3
      Re: Building Timeout in C#

      haha, oops that was a bit dumb.
      Here it is, it starts a timer class (System.Threadi ng)
      that notifies by calling a delegate. The delegate method
      that is called throws an exception indicating timeout. Problem
      is that I can't catch the exception.


      public class test
      {
      public test(){}

      public void DoTimeout()
      {

      Timer t = new Timer(new TimerCallback(T imedout),
      this,1000,Timeo ut.Infinite);
      try
      {
      while(true)
      {
      Console.WriteLi ne("Doing something");
      Thread.Sleep(10 0000);
      }
      }
      catch(Exception e)
      {
      // catch the timeout exception
      Console.WriteLi ne(e.Message);
      }

      }

      private void Timedout(object state)
      {
      throw new ApplicationExce ption("Timedout ");
      }
      }



      "William Stacey" <staceyw@mvps.o rg> wrote in message news:<uBUv1$VkD HA.2000@TK2MSFT NGP12.phx.gbl>. ..[color=blue]
      > Hi Wouter. You forgot the "code below"...
      >
      > --
      > William Stacey, MVP
      >
      > "Pietje de kort" <woutervu@hotma il.com> wrote in message
      > news:e09cc77a.0 310122209.46068 7fb@posting.goo gle.com...[color=green]
      > > Hello all,
      > >
      > > I am trying to build a class that does something, and may timeout while
      > > doing so. Ofcouse I want to be a bit elegant, so I came up with the code
      > > found below. Problem is, I can't catch the exception thrown on timeout.
      > > Anybody knows how to correctly implement timeout behaviour?
      > >
      > > Best regards, Wouter van Vugt[/color][/color]

      Comment

      • Mr.Tickle

        #4
        Re: Building Timeout in C#

        You throw exceptions for NORMAL status? You are a very inefficent
        developer. Exceptions are for "exceptiona l" circumstances, not for status or
        return codes.




        "Pietje de kort" <woutervu@hotma il.com> wrote in message
        news:e09cc77a.0 310130730.3115a 7c4@posting.goo gle.com...[color=blue]
        > haha, oops that was a bit dumb.
        > Here it is, it starts a timer class (System.Threadi ng)
        > that notifies by calling a delegate. The delegate method
        > that is called throws an exception indicating timeout. Problem
        > is that I can't catch the exception.
        >
        >
        > public class test
        > {
        > public test(){}
        >
        > public void DoTimeout()
        > {
        >
        > Timer t = new Timer(new TimerCallback(T imedout),
        > this,1000,Timeo ut.Infinite);
        > try
        > {
        > while(true)
        > {
        > Console.WriteLi ne("Doing something");
        > Thread.Sleep(10 0000);
        > }
        > }
        > catch(Exception e)
        > {
        > // catch the timeout exception
        > Console.WriteLi ne(e.Message);
        > }
        >
        > }
        >
        > private void Timedout(object state)
        > {
        > throw new ApplicationExce ption("Timedout ");
        > }
        > }
        >
        >
        >
        > "William Stacey" <staceyw@mvps.o rg> wrote in message[/color]
        news:<uBUv1$VkD HA.2000@TK2MSFT NGP12.phx.gbl>. ..[color=blue][color=green]
        > > Hi Wouter. You forgot the "code below"...
        > >
        > > --
        > > William Stacey, MVP
        > >
        > > "Pietje de kort" <woutervu@hotma il.com> wrote in message
        > > news:e09cc77a.0 310122209.46068 7fb@posting.goo gle.com...[color=darkred]
        > > > Hello all,
        > > >
        > > > I am trying to build a class that does something, and may timeout[/color][/color][/color]
        while[color=blue][color=green][color=darkred]
        > > > doing so. Ofcouse I want to be a bit elegant, so I came up with the[/color][/color][/color]
        code[color=blue][color=green][color=darkred]
        > > > found below. Problem is, I can't catch the exception thrown on[/color][/color][/color]
        timeout.[color=blue][color=green][color=darkred]
        > > > Anybody knows how to correctly implement timeout behaviour?
        > > >
        > > > Best regards, Wouter van Vugt[/color][/color][/color]


        Comment

        • Till Meyer

          #5
          Re: Building Timeout in C#

          On Mon, 13 Oct 2003 18:22:18 +0200, Mr.Tickle wrote:
          [color=blue]
          > You throw exceptions for NORMAL status? You are a very inefficent
          > developer. Exceptions are for "exceptiona l" circumstances, not for
          > status or return codes.[/color]

          Why NORMAL status?
          He wants to throw a exception if his code runs into a timeout.
          And he wants to catch this timeout-exception.

          Till Meyer

          Comment

          • Pietje de kort

            #6
            Re: Building Timeout in C#

            Exactly Till,
            I always thought that a timeout was abnormal, and
            who cares. I want a good Timeout mechanism, preferably
            the one I posted here earlier, currently I don't throw an
            exception in the Timedout() method, but set a bool 'timedout'
            to true which I check in the while() loop. I don't think it's
            particularly elegant, but it works.

            Anybody with any ideas, without flaming some EXAMPLE code.

            Laters! Wouter

            Till Meyer <nospam@aroh.de > wrote in message news:<bmeu00.1g 8.1@user.aroh.d e>...[color=blue]
            > On Mon, 13 Oct 2003 18:22:18 +0200, Mr.Tickle wrote:
            >[color=green]
            > > You throw exceptions for NORMAL status? You are a very inefficent
            > > developer. Exceptions are for "exceptiona l" circumstances, not for
            > > status or return codes.[/color]
            >
            > Why NORMAL status?
            > He wants to throw a exception if his code runs into a timeout.
            > And he wants to catch this timeout-exception.
            >
            > Till Meyer[/color]

            Comment

            • Fred Mellender

              #7
              Re: Building Timeout in C#

              Not sure I understand the problem but:

              public class PModel
              {

              private System.Timers.T imer watchDogTimer;


              public PModel()
              {

              watchDogTimer = new System.Timers.T imer(1000); //1 second timeout
              watchDogTimer.E nabled = false;
              watchDogTimer.A utoReset = true;

              watchDogTimer.E lapsed+=new ElapsedEventHan dler(OnWatchDog Bark);

              }


              public void OnWatchDogBark( object source, ElapsedEventArg s e)
              {


              watchDogTimer.E nabled = false;

              /*do something here on a time out*/

              watchDogTimer.E nabled = true;


              }
              }


              You could enable the timer in a routine other than the constructor if that
              makes more sense.


              "Pietje de kort" <woutervu@hotma il.com> wrote in message
              news:e09cc77a.0 310131325.4da58 497@posting.goo gle.com...[color=blue]
              > Exactly Till,
              > I always thought that a timeout was abnormal, and
              > who cares. I want a good Timeout mechanism, preferably
              > the one I posted here earlier, currently I don't throw an
              > exception in the Timedout() method, but set a bool 'timedout'
              > to true which I check in the while() loop. I don't think it's
              > particularly elegant, but it works.
              >
              > Anybody with any ideas, without flaming some EXAMPLE code.
              >
              > Laters! Wouter
              >
              > Till Meyer <nospam@aroh.de > wrote in message[/color]
              news:<bmeu00.1g 8.1@user.aroh.d e>...[color=blue][color=green]
              > > On Mon, 13 Oct 2003 18:22:18 +0200, Mr.Tickle wrote:
              > >[color=darkred]
              > > > You throw exceptions for NORMAL status? You are a very inefficent
              > > > developer. Exceptions are for "exceptiona l" circumstances, not for
              > > > status or return codes.[/color]
              > >
              > > Why NORMAL status?
              > > He wants to throw a exception if his code runs into a timeout.
              > > And he wants to catch this timeout-exception.
              > >
              > > Till Meyer[/color][/color]


              Comment

              • Pietje de kort

                #8
                Re: Building Timeout in C#

                Hello Fred,

                that's kind of what I built, except that I throw an
                exception in WatchDogBark(). The problem is I can't catch
                that exception. Currently I don't throw an exception but set
                a bool indicating timeout. This bool can be checked in the loop
                that does the action which can timeout (constructor PModel).
                I do not think this method is particularly elegant,

                Best regards, Wouter


                "Fred Mellender" <nospamPlease_f redm@frontierne t.net> wrote in message news:<avFib.310 3$OF2.2668@news 01.roc.ny>...[color=blue]
                > Not sure I understand the problem but:
                >
                > public class PModel
                > {
                >
                > private System.Timers.T imer watchDogTimer;
                >
                >
                > public PModel()
                > {
                >
                > watchDogTimer = new System.Timers.T imer(1000); //1 second timeout
                > watchDogTimer.E nabled = false;
                > watchDogTimer.A utoReset = true;
                >
                > watchDogTimer.E lapsed+=new ElapsedEventHan dler(OnWatchDog Bark);
                >
                > }
                >
                >
                > public void OnWatchDogBark( object source, ElapsedEventArg s e)
                > {
                >
                >
                > watchDogTimer.E nabled = false;
                >
                > /*do something here on a time out*/
                >
                > watchDogTimer.E nabled = true;
                >
                >
                > }
                > }
                >
                >
                > You could enable the timer in a routine other than the constructor if that
                > makes more sense.
                >
                >
                > "Pietje de kort" <woutervu@hotma il.com> wrote in message
                > news:e09cc77a.0 310131325.4da58 497@posting.goo gle.com...[color=green]
                > > Exactly Till,
                > > I always thought that a timeout was abnormal, and
                > > who cares. I want a good Timeout mechanism, preferably
                > > the one I posted here earlier, currently I don't throw an
                > > exception in the Timedout() method, but set a bool 'timedout'
                > > to true which I check in the while() loop. I don't think it's
                > > particularly elegant, but it works.
                > >
                > > Anybody with any ideas, without flaming some EXAMPLE code.
                > >
                > > Laters! Wouter
                > >
                > > Till Meyer <nospam@aroh.de > wrote in message[/color]
                > news:<bmeu00.1g 8.1@user.aroh.d e>...[color=green][color=darkred]
                > > > On Mon, 13 Oct 2003 18:22:18 +0200, Mr.Tickle wrote:
                > > >
                > > > > You throw exceptions for NORMAL status? You are a very inefficent
                > > > > developer. Exceptions are for "exceptiona l" circumstances, not for
                > > > > status or return codes.
                > > >
                > > > Why NORMAL status?
                > > > He wants to throw a exception if his code runs into a timeout.
                > > > And he wants to catch this timeout-exception.
                > > >
                > > > Till Meyer[/color][/color][/color]

                Comment

                Working...