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. The work is done,why does Process.WaitForExit() still block the thread?

The work is done,why does Process.WaitForExit() still block the thread?

Scheduled Pinned Locked Moved C#
helpquestioncsharpdatabase
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.
  • M Offline
    M Offline
    Morven Huang
    wrote on last edited by
    #1

    Hi everyone, I run into a tricky issue when I run cmd.exe using Process class in C#. 1)Here's the C# code,it run the specified .bat file in command-line: m_BasicDataProc = new Process(); m_BasicDataProc.StartInfo.FileName = "cmd.exe"; m_BasicDataProc.StartInfo.CreateNoWindow = false; m_BasicDataProc.StartInfo.UseShellExecute = false; m_BasicDataProc.StartInfo.RedirectStandardOutput = true; m_BasicDataProc.StartInfo.RedirectStandardInput = true; m_BasicDataProc.StartInfo.WorkingDirectory = Path.GetDirectoryName(batchFilePath); m_BasicDataProc.Start(); string batchFileName = Path.GetFileName(batchFilePath); string ipLine = batchFileName;StreamWriter inputStream = m_BasicDataProc.StandardInput; inputStream.WriteLine(ipLine); inputStream.Close(); m_BasicDataProc.WaitForExit(); m_BasicDataProc.EnableRaisingEvents = true; 2)In the .bat file,it calls a .vbs file with the content pretty much like this: option explicit dim fso Set fso = CreateObject("Scripting.FileSystemObject") Dim CreateFile Set CreateFile = fso.CreateTextFile("InstallScripts\CreateTriggers.sql", true) ...... CreateFile.writeLine(strStatement) CreateFile.WriteLine set CreateFile = nothing set fso = nothing // I found that the file CreateTriggers.sql was created successfully,it means that the work of this .bat file is finished,but it's really odd that the application is still blocked by the code line "m_BasicDataProc.WaitForExit();" and could not exit. Has anyone been in same situation? Anything you could help would be appreciated!

    J 1 Reply Last reply
    0
    • M Morven Huang

      Hi everyone, I run into a tricky issue when I run cmd.exe using Process class in C#. 1)Here's the C# code,it run the specified .bat file in command-line: m_BasicDataProc = new Process(); m_BasicDataProc.StartInfo.FileName = "cmd.exe"; m_BasicDataProc.StartInfo.CreateNoWindow = false; m_BasicDataProc.StartInfo.UseShellExecute = false; m_BasicDataProc.StartInfo.RedirectStandardOutput = true; m_BasicDataProc.StartInfo.RedirectStandardInput = true; m_BasicDataProc.StartInfo.WorkingDirectory = Path.GetDirectoryName(batchFilePath); m_BasicDataProc.Start(); string batchFileName = Path.GetFileName(batchFilePath); string ipLine = batchFileName;StreamWriter inputStream = m_BasicDataProc.StandardInput; inputStream.WriteLine(ipLine); inputStream.Close(); m_BasicDataProc.WaitForExit(); m_BasicDataProc.EnableRaisingEvents = true; 2)In the .bat file,it calls a .vbs file with the content pretty much like this: option explicit dim fso Set fso = CreateObject("Scripting.FileSystemObject") Dim CreateFile Set CreateFile = fso.CreateTextFile("InstallScripts\CreateTriggers.sql", true) ...... CreateFile.writeLine(strStatement) CreateFile.WriteLine set CreateFile = nothing set fso = nothing // I found that the file CreateTriggers.sql was created successfully,it means that the work of this .bat file is finished,but it's really odd that the application is still blocked by the code line "m_BasicDataProc.WaitForExit();" and could not exit. Has anyone been in same situation? Anything you could help would be appreciated!

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

      the batch file finished but cmd.exe is still running - that's what m_BasicDataProc.WaitForExit() is waiting for. Try executing the batch file directly instead of passing it as an argument to cmd.exe.

      :Badger:

      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