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. Kill a process in RDC session

Kill a process in RDC session

Scheduled Pinned Locked Moved C#
questionsysadminhelp
4 Posts 3 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.
  • P Offline
    P Offline
    pranu_13
    wrote on last edited by
    #1

    I launch application B from application A. When i close application A i need to close application B. I RDC to a remote server and launch application A and launch B from A. When i close A it looks for B and kills it. However it goes and kills application B in another RDC session. How do i differentiate the process launched in the session i logged into? Any help would be greatly appreciated.

    I 1 Reply Last reply
    0
    • P pranu_13

      I launch application B from application A. When i close application A i need to close application B. I RDC to a remote server and launch application A and launch B from A. When i close A it looks for B and kills it. However it goes and kills application B in another RDC session. How do i differentiate the process launched in the session i logged into? Any help would be greatly appreciated.

      I Offline
      I Offline
      Ian Shlasko
      wrote on last edited by
      #2

      Well, there are quite a few ways to launch a process... In this case, why not start it using the System.Diagnostics.Process class, and just keep a reference to that object?

      private Process appB;
      public void LaunchAppB()
      {
      appB = new Process();
      appB.StartInfo.FileName = "...";
      appB.Start();
      }

      public void KillAppB()
      {
      if (!appB.CloseMainWindow()) appB.Kill();
      }

      (Note: You generally want to try CloseMainWindow first, which will try to close the app in the usual manner, like clicking the X. If that doesn't work, you kill the process. Depending on exactly what you're doing here, you might need to make it more sophisticated, perhaps giving it a few seconds to close properly and killing after that, or something of the sort)

      P 1 Reply Last reply
      0
      • I Ian Shlasko

        Well, there are quite a few ways to launch a process... In this case, why not start it using the System.Diagnostics.Process class, and just keep a reference to that object?

        private Process appB;
        public void LaunchAppB()
        {
        appB = new Process();
        appB.StartInfo.FileName = "...";
        appB.Start();
        }

        public void KillAppB()
        {
        if (!appB.CloseMainWindow()) appB.Kill();
        }

        (Note: You generally want to try CloseMainWindow first, which will try to close the app in the usual manner, like clicking the X. If that doesn't work, you kill the process. Depending on exactly what you're doing here, you might need to make it more sophisticated, perhaps giving it a few seconds to close properly and killing after that, or something of the sort)

        P Offline
        P Offline
        pranu_13
        wrote on last edited by
        #3

        The context in which i close the application i cannot maintain the instance. Thanks for the response. I found a solution. Check below: [DllImport("user32.dll", CharSet=CharSet.Auto)] public static extern IntPtr FindWindow(string sClassName, string sWindowTitle); [DllImport("user32.dll")] static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); uint processId; IntPtr hProgram = WebApp.FindWindow(null, "....."); GetWindowThreadProcessId(hProgram , out processId); System.Diagnostics.Process.GetProcessById((int)processId).Kill();

        D 1 Reply Last reply
        0
        • P pranu_13

          The context in which i close the application i cannot maintain the instance. Thanks for the response. I found a solution. Check below: [DllImport("user32.dll", CharSet=CharSet.Auto)] public static extern IntPtr FindWindow(string sClassName, string sWindowTitle); [DllImport("user32.dll")] static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); uint processId; IntPtr hProgram = WebApp.FindWindow(null, "....."); GetWindowThreadProcessId(hProgram , out processId); System.Diagnostics.Process.GetProcessById((int)processId).Kill();

          D Offline
          D Offline
          dano2k3
          wrote on last edited by
          #4

          I might suggest there is a problem with GetWindowThreadProcessId, I do UI automation testing, i was looking a using GetWindowThreadProcessId a few weeks ago. Bottom line from what i found if you are on Win95, or NT 3.51, GetWindowThreadProcessId should work fine, on Vista I thought i remember seeing warning about it not working correctly. Alternatively, i might suggest 1.) Make sure you app is modal, that only one instance of it can be running. 2.) Get the process by GetProcessesByName() for your process, and then issue a kill on the process you find, well if it is still running. HTH, dan d;)

          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