Skip to content

Instantly share code, notes, and snippets.

@Cyken-Zeraux
Created June 12, 2015 16:15
Show Gist options
  • Save Cyken-Zeraux/aedec00f393dedb9d8cf to your computer and use it in GitHub Desktop.
Save Cyken-Zeraux/aedec00f393dedb9d8cf to your computer and use it in GitHub Desktop.
<?php
//Downloads a file with file_get_contents or cURL without locking up GUI.
//Has error where program will randomly crash on processing thread, no error outputted to console.
//This can be reproduced by clicking the button multiple times, sometimes it happens more than others.
//This is an odd error, and this is the best I've gotten to work.
//Sources from: https://github.com/krakjoe/pthreads/issues/155
//https://github.com/wxphp/wxphp/blob/master/examples/thread.php
error_reporting(-1);
ini_set('error_reporting', E_ALL);
/*
* PHP code generated with wxFormBuilder (version Jun 5 2014)
* http://www.wxformbuilder.org/
*
* PLEASE DO "NOT" EDIT THIS FILE!
*/
/*
* Class MyFrame1
*/
class MyFrame1 extends wxFrame {
function __construct( $parent=null ){
parent::__construct ( $parent, wxID_ANY, wxEmptyString, wxDefaultPosition, new wxSize( 300,250 ), wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL );
$this->SetSizeHints( new wxSize( 300,250 ), new wxSize( 300,250 ) );
$bSizer1 = new wxBoxSizer( wxVERTICAL );
$this->m_staticText1 = new wxStaticText( $this, wxID_ANY, "Not Threaded", wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE );
$this->m_staticText1->Wrap( -1 );
$bSizer1->Add( $this->m_staticText1, 0, wxALL|wxEXPAND, 5 );
$this->m_button1 = new wxButton( $this, wxID_ANY, "Test Download file_get_contents", wxDefaultPosition, wxDefaultSize, 0 );
$bSizer1->Add( $this->m_button1, 0, wxALL|wxEXPAND, 5 );
$this->m_button2 = new wxButton( $this, wxID_ANY, "Test Download cURL", wxDefaultPosition, wxDefaultSize, 0 );
$bSizer1->Add( $this->m_button2, 0, wxALL|wxEXPAND, 5 );
$this->m_staticText11 = new wxStaticText( $this, wxID_ANY, "Threaded", wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE );
$this->m_staticText11->Wrap( -1 );
$bSizer1->Add( $this->m_staticText11, 0, wxALL|wxEXPAND, 5 );
$this->m_button11 = new wxButton( $this, wxID_ANY, "Test threaded File_Get_Contents on URL", wxDefaultPosition, wxDefaultSize, 0 );
$bSizer1->Add( $this->m_button11, 0, wxALL|wxEXPAND, 5 );
$this->m_button21 = new wxButton( $this, wxID_ANY, "Test Download cURL", wxDefaultPosition, wxDefaultSize, 0 );
$bSizer1->Add( $this->m_button21, 0, wxALL|wxEXPAND, 5 );
$this->SetSizer( $bSizer1 );
$this->Layout();
$this->Centre( wxBOTH );
// Connect Events
$this->m_button1->Connect( wxEVT_COMMAND_BUTTON_CLICKED, array($this, "fgtbuttonClick") );
$this->m_button2->Connect( wxEVT_COMMAND_BUTTON_CLICKED, array($this, "cURLbuttonClick") );
$this->m_button11->Connect( wxEVT_COMMAND_BUTTON_CLICKED, array($this, "threadfgtbuttonClick") );
$this->m_button21->Connect( wxEVT_COMMAND_BUTTON_CLICKED, array($this, "threadcURLbuttonClick") );
}
function __destruct( ){
}
// Virtual event handlers, overide them in your derived class
function fgtbuttonClick( $event ){
}
function cURLbuttonClick( $event ){
}
function threadfgtbuttonClick( $event ){
}
function threadcURLbuttonClick( $event ){
}
}
/*Subclass of MyFrame1, which is generated by wxFormBuilder.*/
class DocumentConverter extends wxThread
{
function __construct($parent)
{
parent::__construct(wxTHREAD_JOINABLE);
$this->parent = $parent;
}
function Entry()
{
$console = new ConsoleLoop();
$console->start();
while (!$console->stop) {
$console->synchronized(function($console){
if (empty($console->line) || $console->line === false) {
printf( "%s\n", 'Bad Data');
$console->stop();
} else printf("%s\n", 'Worked');
$console->notify();
$console->stop();
}, $console);
}
$console->join();
$this->parent->onDocumentsConverted();
return;
}
}
class ConsoleLoop extends Thread{
public $line;
public $stop;
public $base;
public $ev;
public $fp;
public function __construct(){
$this->line = false;
$this->stop = false;
}
public function stop(){
$this->stop = true;
}
public function run(){
while($this->stop === false){
$this->synchronized(function(){
$this->line = file_get_contents('https://raw.githubusercontent.com/wxphp/wxphp/master/examples/thread.php');
$this->wait();
});
}
}
}
// Implementing MyFrame1
class MyProject2MyFrame1 extends MyFrame1 {
function __construct($parent=null){
parent::__construct( $parent );
$this->m_button1->disable();
$this->m_button21->disable();
$this->m_button2->disable();
$this->document_converter = new DocumentConverter($this);
$this->converting = false;
$this->m_timer = new wxTimer($this);
//Timer to periodically call TestDestroy
$this->m_timer2 = new wxTimer($this);
$this->m_timer2->Start(500);
$this->Connect($this->m_timer->GetID(), wxEVT_TIMER, array($this, "onTimer"));
$this->Connect($this->m_timer2->GetID(), wxEVT_TIMER, array($this, "onTimerDestroy"));
}
function onDocumentsConverted()
{
$this->converting = false;
}
function onTimer($event)
{
if(!$this->converting)
{
if($this->m_timer->IsRunning()) {
$this->m_timer->Stop();
// Ensure thread isn't running before Delete
while($this->document_converter->IsRunning()){}
$this->document_converter->Delete();
wxMessageBox("Succeeded!");
$this->m_button11->Enable();
}
}
}
function onTimerDestroy() {
$this->document_converter->TestDestroy();
}
// Handlers for MyFrame1 events.
function fgtbuttonClick( $event ){
// TODO: Implement fgtbuttonClick
}
function cURLbuttonClick( $event ){
// TODO: Implement cURLbuttonClick
}
function threadfgtbuttonClick( $event ){
$this->document_converter = new DocumentConverter($this);
$this->converting = true;
$this->m_timer->Start(500);
$this->document_converter->Create();
$this->m_button11->Disable();
$this->document_converter->Run();
}
function threadcURLbuttonClick( $event ){
// TODO: Implement threadcURLbuttonClick
}
}
class myApp extends wxApp
{
function OnInit()
{
global $mf;
$mf = new MyProject2MyFrame1();
$mf->Show();
return 0;
}
function OnExit()
{
return 0;
}
}
wxInitAllImageHandlers();
$xt = new myApp();
wxApp::SetInstance($xt);
wxEntry();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment