2

我正在尝试使用 wxPHP 编写一个简单的应用程序,该应用程序使用exec('ping')命令从文件中 ping IP。

我希望结果在后台被 ping 时一一显示在 GUI 中,所以我exec('ping')在线程中执行命令。

但是我得到了非常奇怪的错误和行为。问题是我无法重现错误。有时该程序工作得很好。有时它会在线程中崩溃。有时它在发送事件时会崩溃。等等

以下是我从命令行运行程序时遇到的错误:

C:\Users\SH\Desktop>wxphp pinger.wxphp
PHP Notice:  Undefined property: wxHtmlWindow::$parent in Unknown on line 0
PHP Fatal error:  Call to a member function setPingResults() on null in Unknown
on line 0

C:\Users\SH\Desktop>wxphp pinger.wxphp
PHP Notice:  Undefined property: wxHtmlWindow::$parent in Unknown on line 0
PHP Fatal error:  Call to a member function setPingResults() on null in Unknown
on line 0

C:\Users\SH\Desktop>wxphp pinger.wxphp
PHP Notice:  Undefined property: wxHtmlWindow::$parent in Unknown on line 553649674
PHP Fatal error:  Call to a member function setPingResults() on null in Unknown
on line 553649674

这是我的代码:

<?php

if(!extension_loaded('wxwidgets'))
{
    dl('wxwidgets.' . PHP_SHLIB_SUFFIX);
}

define('EVT_PINGDONE',wxNewEventType());


class myPing extends wxThread
{
    function __construct($parent)
    {
        parent::__construct(wxTHREAD_JOINABLE);

        $this->parent =  $parent;
    }

    function Entry()
    {
        $addresses = file($this->parent->btnBrowse->GetPath(),FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
        foreach($addresses as $k => $address){

            $results = exec("ping $address");

            $this->parent->setPingResults($results);

            $evt = new wxCommandEvent(EVT_PINGDONE);
            $this->parent->QueueEvent($evt);
        }

        $this->parent->onThreadDone();

        return;
    }
}

class mythFrame extends wxFrame {

    function __construct( $parent=null ){
        parent::__construct ( $parent, wxID_ANY, 'Pinger', wxDefaultPosition, new wxSize( 600,400 ), wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL );

        $this->SetSizeHints( wxDefaultSize, wxDefaultSize );

        $bSizer2 = new wxBoxSizer( wxHORIZONTAL );

        $this->button = new wxButton( $this, wxID_ANY, "Ping", wxDefaultPosition, new wxSize(300, 40), 0 );
        $bSizer2->Add($this->button, 0, wxALL|wxEXPAND, 5);

        $bSizer1 = new wxBoxSizer( wxVERTICAL );

        $this->btnBrowse = new wxFilePickerCtrl( $this, wxID_ANY, 'C:\Users\SH\Desktop\stuff\servers.txt', "Select a file", "*.*", wxDefaultPosition, wxDefaultSize, wxFLP_DEFAULT_STYLE );
        $bSizer1->Add( $this->btnBrowse, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL, 5 );

        $this->html = new wxHtmlWindow( $this, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0 );
        $bSizer1->Add( $this->html, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxEXPAND, 5 );

        $bSizer1->Add( $bSizer2, 0, wxALL|wxALIGN_CENTER_HORIZONTAL, 5 );

        $this->SetSizer( $bSizer1 );
        $this->Layout();

        $this->Centre( wxBOTH );

        // Connect Events
        $this->button->Connect( wxEVT_COMMAND_BUTTON_CLICKED, array($this, "hndlrButton") );
        $this->Connect(wxEVT_TIMER, array($this, "onTimer"));
        $this->Connect(EVT_PINGDONE, array($this, 'updateText'));

        $this->pingThread = new myPing($this);
        $this->m_timer = new wxTimer($this);
        $this->pingResults = '';

    }

    function updateText($event){

        $this->html->AppendToPage($this->pingResults.'<br>');
    }

    function onTimer(){
        if($this->threadDone == true){
            $this->m_timer->Stop();
            $this->button->Enable();
            $this->btnBrowse->Enable();
            $this->html->AppendToPage('<br>Finished.<hr>');

            while($this->pingThread->IsRunning()){}
            $this->pingThread->Delete();

            $this->pingThread = new myPing($this);
        }
    }

    function onThreadDone(){
        $this->threadDone = true;
    }

    function setPingResults($results){
        $this->pingResults = $results;
    }


    function hndlrButton( $event ){
        $this->threadDone = false;
        $this->m_timer->start(3000);
        $this->button->Disable();
        $this->btnBrowse->Disable();

        $this->pingThread->Create();
        $this->pingThread->Run();
    }

}

$myFrame = new mythFrame();
$myFrame->show();
wxEntry();

?>

这是我在 Windows 错误日志中得到的错误:

Faulting application name: wxphp.exe, version: 5.6.9.0, time stamp: 0x55765568
Faulting module name: ntdll.dll, version: 6.1.7601.18247, time stamp: 0x521eaf24
Exception code: 0xc0000005
Fault offset: 0x0000000000052f86
Faulting process id: 0x3c4
Faulting application start time: 0x01d0b6675e88094f
Faulting application path: C:\Program Files\wxPHP\php\wxphp.exe
Faulting module path: C:\Windows\SYSTEM32\ntdll.dll
Report Id: 9cd6fe5e-225a-11e5-b316-f46d04f70903

错误模块的变化取决于我是调用 php 函数还是调用exec()线程Entry()。但异常代码始终是0xc0000005.

错误中一致的一件奇怪的事情是错误,例如

Undefined property wxHtmlWindow::$parent 

如果您阅读代码,您会意识到根本没有这样的调用。实际上$parent只在线程中访问。我在线程中的其他函数中遇到了类似的错误。似乎应用程序没有意识到它在线程中并且$this在线程中指向主框架对象。

我想我的代码或者 wxPHP 可能有什么问题。这个程序昨晚不工作,然后今天早上开始工作。然后我添加了一个图标和一些背景颜色,它突然又给了我错误。

其他信息

  • 不适用于:

    Windows 7-64bit - wxphp-3.0.2.0-php5.6-x64.exe

    Windows 7-32bit - wxphp-3.0.2.0-php5.4-x86

  • 与 wxPHP 包捆绑在一起的示例thread.wxphp应用程序也不起作用。
  • 但是,我的代码和示例应用程序都可以在 Linux ( Linux Mint-32bit - php5-wxwidgets_3.0.2.0_i386) 上正常运行。
  • 当我从线程 中删除两者时exec(),我没有收到任何错误。QueueEvent()Entry()

有谁知道这里发生了什么?

4

1 回答 1

1

我是 wxPHP 的维护者,我建议使用 PHP pthreads扩展进行多线程处理,因为这个扩展可以正确处理 PHP zendengine 的线程。wxThread 类由 wxPHP 源代码生成器自动包装,它可能需要进一步开发才能正常工作。

于 2015-07-07T06:53:29.640 回答