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

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Tony0945
Earned some good credits
Earned some good credits
Posts: 105
Joined: Wed Oct 21, 2009 4:02 pm

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

Post 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();
}

Last edited by Tony0945 on Sun Apr 22, 2018 11:54 pm, edited 1 time in total.
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to find wxProcess pointer in wxProcessEvent handler?

Post by doublemax »

Both samples that use this derive from wxProcess.

The only other way would be to store the pointer somewhere.
Use the source, Luke!
Tony0945
Earned some good credits
Earned some good credits
Posts: 105
Joined: Wed Oct 21, 2009 4:02 pm

Re: How to find wxProcess pointer in wxProcessEvent handler?

Post 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!
Post Reply