Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Timeout for an operation

Timeout for an operation

Scheduled Pinned Locked Moved C#
help
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    faheemnadeem
    wrote on last edited by
    #1

    Hi, I need to implement a timeout expression. Forexample like in services there is: TCPIPServiceController.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 10)); Where it checks for 10 sec whether the service has reached a running state, else exception. What i need is a customized similar expression (one line) implementing timeout, where i need to check the state of a variable, if true then carry on else exception. I don't wish to implement standalone timers event for that...., as there are many methods in which i need to implement this. Hassles of Async programming!!!. Any help please!!! Thank You.

    B 1 Reply Last reply
    0
    • F faheemnadeem

      Hi, I need to implement a timeout expression. Forexample like in services there is: TCPIPServiceController.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 10)); Where it checks for 10 sec whether the service has reached a running state, else exception. What i need is a customized similar expression (one line) implementing timeout, where i need to check the state of a variable, if true then carry on else exception. I don't wish to implement standalone timers event for that...., as there are many methods in which i need to implement this. Hassles of Async programming!!!. Any help please!!! Thank You.

      B Offline
      B Offline
      Ben Fair
      wrote on last edited by
      #2

      I believe the System.Threading.Timer class should be able to handle this in relatively little code. This version of the Timer class takes a TimerCallback delegate that it will invoke after the specified amount of time has transpired. You can also configure it via one of the constructor overloads to continue running at a certain interval, or you can have it run only a single time. Last, it takes a state object so you can pass in some data pertaining to the operation you wish to check and that object will be passed in to your delegate. Here's some example code just to illustrate how easy it is to use.

      using System.Threading;
      ...

      private Timer _operationTimeout = null;

      private void CheckOperation(MyOperationData data, int timeout)
      {
      // run this timer a single time...
      _operationTimeout = new Timer(new TimerCallback(CheckValue), data, timeout, Timeout.Infinite);
      }
      // this method will be called by the timeout timer, once the interval has elapsed
      private void CheckValue(object data)
      {
      MyOperationData opData = (MyOperationData)data;
      // check the result, if it is not the expected value throw the timeout exception
      if (!opData.Value.Equals(opData.ExpectedValue))
      throw opData.TimeoutException;
      }

      Hold on a second here... Don't you think you might be putting the horse ahead of the cart?

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups