Page 1 of 1

How to find wxProcess pointer in wxProcessEvent handler? [solved]

Posted: Wed Apr 18, 2018 3:53 pm
by Tony0945
From the wxProcess Class Reference
Note
If the wxEVT_END_PROCESS event sent after termination is processed by the parent, then it is responsible for deleting the wxProcess object which sent it. However, if it is not processed, the object will delete itself and so the library users should only delete those objects whose notifications have been processed (and call wxProcess::Detach for others). This also means that unless you're going to process the wxEVT_END_PROCESS event, you must allocate the wxProcess class on the heap.
from wxWidgets-3.0.2/src/common/process.cpp

Code: Select all

void wxProcess::OnTerminate(int pid, int status)
{
    wxProcessEvent event(m_id, pid, status);

    if ( !ProcessEvent(event) )
        delete this;
    //else: the object which processed the event is responsible for deleting
    //      us!
}
How can I get the pointer in the event handler so that I can delete it? The handler is in my top level wxFrame so I don't want to "delete this".
Should I just derive from wxProcess and use an OnTerminate like this?

Code: Select all

void MyProcess::OnTerminate(int pid, int status)
{
	wxProcessEvent event(m_id, pid, status);
	ProcessEvent(event);
	delete this;
}
Or is there a way using the base class?

Sample code:

Code: Select all

BEGIN_EVENT_TABLE(DoorsFrm,wxFrame)
    ////Manual Code Start
    EVT_END_PROCESS(wxID_ANY,DoorsFrm::OnProgramTerminated)
    ////Manual Code End
    EVT_CLOSE(DoorsFrm::OnClose)

END_EVENT_TABLE()
////Event Table End
void DoorsFrm::OnProgramTerminated(wxProcessEvent& event)
{
    int pid = event.GetPid();
    int status = event.GetExitCode();
    printf("%s(%d): On Terminate called for process %d with status %d\n",__FILE__,__LINE__,pid,status);
    if (status) // ignore zero
    {
        toolbar->RemoveMonitorButton(pid);
        toolbar->Realize();
    }
    // How to delete Original wxProcess object allocated on the heap?
}

void DoorsFrm::RunProgram(wxString &cmd, wxString &icon)
{
    wxProcess * process= new wxProcess(this); //event handler will process the termination event

    printf("%s(%d) About to execute command \"%s\" ",__FILE__,__LINE__,static_cast<const char*>(cmd));

    long pid = wxExecute(cmd, wxEXEC_ASYNC, process); //successfull call will be handled by event handler

    printf("%s(%d) cmd=\"%s\" ,PID = %ld\n",__FILE__,__LINE__,static_cast<const char*>(cmd),pid);

    MonitorButton * button = new MonitorButton(toolbar);
    printf("Setting Monitor Button pid %ld\n",pid);
    button->SetPid(pid);
    wxBitmap map= *::wxGetApp().FillBitmapFromFile(icon);
    button->SetBitmap(map);
    toolbar->AddMonitorButton(button);
    toolbar->Realize();
}


Re: How to find wxProcess pointer in wxProcessEvent handler?

Posted: Wed Apr 18, 2018 4:19 pm
by doublemax
Both samples that use this derive from wxProcess.

The only other way would be to store the pointer somewhere.

Re: How to find wxProcess pointer in wxProcessEvent handler?

Posted: Sun Apr 22, 2018 11:53 pm
by Tony0945
My apologies for not responding sooner. I read your response about an hour after you posted it. I swirched to the modified Process as shown in my first post. I think that should be the default but ...

Anyway, thanks for the prompt response. BTW, I had another error in my code, I was checking status instead of pid. No wonder I had all those zero returns!