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. Can I Hard Kill a Thread?

Can I Hard Kill a Thread?

Scheduled Pinned Locked Moved C#
questionmobiletestingbeta-testinghelp
3 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.
  • J Offline
    J Offline
    Jimmanuel
    wrote on last edited by
    #1

    My situation is this: I'm writing an app that needs to execute a separate program (we'll call it aaa.exe) and monitor its output so that my app can display the status of aaa.exe to the user. To do this I've started aaa.exe using a Process and have redirected the STDOUT stream to a StreamReader so that I can monitor the status messages that it spits out and use that information to update a status variable. Basically, what I have to monitor the stream is this:

    string line = "";
    while (line = stdout.ReadLine()) != null)
    {
    // do stuff
    }

    and then I parse line to determine the status of the spawned application. To prevent this while loop from locking up my GUI I've moved this code to another thread, so that the status variable will continually be updated by one thread and the GUI can intermittently poll that variable from it's own thread and remain responsive to the user. Under normal circumstances, this works well. The while loop continues until aaa.exe stops sending data to STDOUT and then the thread monitoring it exits happily and all is well. The problem arises when things don't go well in aaa.exe. It is possible under worst case scenario conditions (so this happens frequently while I'm testing) that the spawned application locks up. When this happens, my code freezes execution on the function call stdout.ReadLine(), and I now have a deadlocked thread. The most direct way to end the rogue thread is to hard kill the process for aaa.exe (using Process.Kill()), but my boss has just informed that this is not an acceptable solution as it causes horrendous side effects to the operating environment. Using Thread.Abort has no apparent effect, it seems to wait for the next instruction to finish before doing anything to the thread which means nothing if the thread is suspended in a function call. So my question is this: is there some other way to end the Thread without killing the instance of aaa.exe, or more generally, is there some way to kill a non-responsive Thread in my application without doing a hard kill on the application itself?

    J 1 Reply Last reply
    0
    • J Jimmanuel

      My situation is this: I'm writing an app that needs to execute a separate program (we'll call it aaa.exe) and monitor its output so that my app can display the status of aaa.exe to the user. To do this I've started aaa.exe using a Process and have redirected the STDOUT stream to a StreamReader so that I can monitor the status messages that it spits out and use that information to update a status variable. Basically, what I have to monitor the stream is this:

      string line = "";
      while (line = stdout.ReadLine()) != null)
      {
      // do stuff
      }

      and then I parse line to determine the status of the spawned application. To prevent this while loop from locking up my GUI I've moved this code to another thread, so that the status variable will continually be updated by one thread and the GUI can intermittently poll that variable from it's own thread and remain responsive to the user. Under normal circumstances, this works well. The while loop continues until aaa.exe stops sending data to STDOUT and then the thread monitoring it exits happily and all is well. The problem arises when things don't go well in aaa.exe. It is possible under worst case scenario conditions (so this happens frequently while I'm testing) that the spawned application locks up. When this happens, my code freezes execution on the function call stdout.ReadLine(), and I now have a deadlocked thread. The most direct way to end the rogue thread is to hard kill the process for aaa.exe (using Process.Kill()), but my boss has just informed that this is not an acceptable solution as it causes horrendous side effects to the operating environment. Using Thread.Abort has no apparent effect, it seems to wait for the next instruction to finish before doing anything to the thread which means nothing if the thread is suspended in a function call. So my question is this: is there some other way to end the Thread without killing the instance of aaa.exe, or more generally, is there some way to kill a non-responsive Thread in my application without doing a hard kill on the application itself?

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      Instead of having a dedicated thread that reads the standard output in a while loop, is it possible to just do something like:

      Process proc = ...;
      proc.OutputDataReceived += OutputReceivedHandler;

      ...

      void OutputReceivedHandler(object sender, DataReceivedEventArgs e)
      {
      // do stuff
      }

      That way, you're in a message-based system where you don't need to spawn/rendezvous/kill other threads.

      Tech, life, family, faith: Give me a visit. I'm currently blogging about: Virginia Tech Shootings, Guns, and Politics The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

      J 1 Reply Last reply
      0
      • J Judah Gabriel Himango

        Instead of having a dedicated thread that reads the standard output in a while loop, is it possible to just do something like:

        Process proc = ...;
        proc.OutputDataReceived += OutputReceivedHandler;

        ...

        void OutputReceivedHandler(object sender, DataReceivedEventArgs e)
        {
        // do stuff
        }

        That way, you're in a message-based system where you don't need to spawn/rendezvous/kill other threads.

        Tech, life, family, faith: Give me a visit. I'm currently blogging about: Virginia Tech Shootings, Guns, and Politics The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

        J Offline
        J Offline
        Jimmanuel
        wrote on last edited by
        #3

        Nice idea. I'll definitely give that a shot when I get in to work tomorrow. Thanks for the help.

        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