Search code examples
asynchronousprocesswxwidgetschild-processwxlua

wxExecuted process is not deleted when main window exits


I am using wxLua with wxWidgets 2.8.12 for Windows. In the following code, I try to asynchronously execute some command and redirect its output.

The problem is, if I close main window before child process terminates, the child process is not deleted. I could still see "tree.com" process inside Windows Task Manager after this wxLua app exits.

What is the problem with my code below?


require("wx")

frame = wx.wxFrame(wx.NULL, wx.wxID_ANY, "Test")

function ExecCommand(cmd)
   proc = wx.wxProcess(frame)
   proc:Redirect()
   pid = wx.wxExecute(cmd, wx.wxEXEC_ASYNC, proc)
   if pid > 0 then
       print("process id is " .. tostring(pid))
       streamIn = proc and proc:GetInputStream()
   end
end

function ReadStream()
   if streamIn and streamIn:CanRead() then
       local str = streamIn:Read(4096)
   end
end

frame:Connect(wx.wxEVT_IDLE, ReadStream)

frame:Connect(wx.wxEVT_END_PROCESS, function(event)
   proc = nil
end)

frame:Connect(wx.wxEVT_CLOSE_WINDOW, function(event)
   if proc then proc:Detach() end
   event:Skip()
end)

frame:Show(true)

cmd = "tree.com C:\\Windows"

ExecCommand(cmd)

wx.wxGetApp():MainLoop()

Solution

  • You don't kill the child process, so it continues running, why shouldn't it? If you don't want this to happen, you need to Kill() it explicitly using its PID.

    BTW, please also update to wxWidgets 3.0, especially as you're using Lua, there shouldn't be any compatibility concerns, but you would be using a supported version with a lot of new features and bug fixes compared to the 10+ year old 2.8.