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. Multithreaded application design question.

Multithreaded application design question.

Scheduled Pinned Locked Moved C#
questiondesignsysadminhelptutorial
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.
  • G Offline
    G Offline
    GriffonRL
    wrote on last edited by
    #1

    Hello, I am working in a multithreaded application and I need to find the best solution to handle this: I have several threads running in parallel doing very similar things against different objects. My problem is that some objects may sometimes be too long to wait for or may have disappear (this is a network application). I would like to be able to make a thread wait for the object no longer than a certain time (30 seconds for example) after calling it. I want to sleep my thread for this period of time and let the others threads work. My concern is that a thread.sleep(30000) is not right because time is taken also be the others threads. A thread.sleep(30000) doesn't mean this thread will wait 30 seconds of its running time, but 30 seconds of real shared time. The reality is that the thread will maybe run only for 10 seconds in this 30 seconds. My question is: what kind of solution exists to effectively make a thread wait for 30 seconds of its running time ? Thanks, R. LOPES Just programmer.

    W 1 Reply Last reply
    0
    • G GriffonRL

      Hello, I am working in a multithreaded application and I need to find the best solution to handle this: I have several threads running in parallel doing very similar things against different objects. My problem is that some objects may sometimes be too long to wait for or may have disappear (this is a network application). I would like to be able to make a thread wait for the object no longer than a certain time (30 seconds for example) after calling it. I want to sleep my thread for this period of time and let the others threads work. My concern is that a thread.sleep(30000) is not right because time is taken also be the others threads. A thread.sleep(30000) doesn't mean this thread will wait 30 seconds of its running time, but 30 seconds of real shared time. The reality is that the thread will maybe run only for 10 seconds in this 30 seconds. My question is: what kind of solution exists to effectively make a thread wait for 30 seconds of its running time ? Thanks, R. LOPES Just programmer.

      W Offline
      W Offline
      wightman_michael
      wrote on last edited by
      #2

      You could use something like this. I should allow all threads to run apox 30 seconds then abort them if they have not completed. class Program { private static AutoResetEvent e = new AutoResetEvent(false); //No delay public static void DoWork1() { Console.WriteLine("DoWork1 Completed"); e.Set(); } //Long but completes public static void DoWork2() { Thread.Sleep(new TimeSpan(0, 0, 25)); Console.WriteLine("DoWork2 Completed"); e.Set(); } //runs too long public static void DoWork3() { Thread.Sleep(new TimeSpan(0, 0, 60)); Console.WriteLine("DoWork3 Completed"); e.Set(); } static void Main(string[] args) { //Create array of worker threads Thread[] workers = new Thread[3]; (workers[0] = new Thread(new ThreadStart(DoWork1))).Start(); (workers[1] = new Thread(new ThreadStart(DoWork2))).Start(); (workers[2] = new Thread(new ThreadStart(DoWork3))).Start(); DateTime abortTime = DateTime.Now.AddSeconds(30); //Wait until timout expires or all all thread are //completed. while (DateTime.Now < abortTime) { //Waits for 10 seconds on until any thread completes e.WaitOne(new TimeSpan(0, 0, 10)); int running = 0; foreach (Thread t in workers) { if (t.ThreadState != System.Threading.ThreadState.Stopped) { running++; } } if (running == 0) { break; } else { Console.WriteLine("Running threads {0}", running); } } //Kill off the threads that have not stopped foreach (Thread t in workers) { try { t.Abort(); } catch { Console.WriteLine("Aborted"); } } } }

      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