Creating Threads

Discussion about the open-source Phalanger [?] project.

Creating Threads

Postby Remo » August 17th, 2012, 3:33 pm

I'm trying to create a new thread but without much success so far. There are a few examples where System.Threading is used to let the application sleep but I couldn't find anything about creating threads.

Here are parts of the code, first the worker class:

Code: Select all
class Worker
{       
    public function ProcessRequest()


which is supposed to be used by this code:

Code: Select all
$worker = new Worker($obj);
$thread = new \System\Threading\Thread($worker->ProcessRequest);
$thread->Start();   


Using $worker->ProcessRequest as a parameter in the Thread class doesn't do anything good.

I get this error on the console before the process crashes:
Unhandled Exception: PHP.Core.PhpNetInternalException: Guarded call ---> System.ArgumentNullException: Value cannot be null.


I also tried to use a call by reference but that didn't work either.
I'm working with 3.0.0.3237 by the way.

Any ideas on how to get this running?
Remo
 
Posts: 17
Joined: August 17th, 2012, 3:25 pm

Re: Creating Threads

Postby Jakub Misek » August 17th, 2012, 3:50 pm

Hi,

Phalanger defines its own function for creating new threads, so you can create them in PHP-like way.

Try to use
Code: Select all
clr_create_thread(callback, arguments);


Otherwise in your sample, you are mixing C# syntax with PHP syntax. In PHP you can't create callbacks by passing method without arguments. You have to pass function name as a string or an array(class name or instance, method name). Something like this
Code: Select all
new \System\Threading\Thread(array($worker,"ProcessRequest"));

But I don't believe Phalanger will properly convert PHP array to C# System.Action ...
Jakub Misek │ DEVSENSE s.r.o. | @misekjakubjakub@devsense.com
User avatar
Jakub Misek
 
Posts: 2092
Joined: January 4th, 2012, 2:42 pm
Location: Prague

Re: Creating Threads

Postby Remo » August 17th, 2012, 4:30 pm

Thanks Jakub!
I think it does cast that array to a proper object in .NET. My code still has a problem but I'm pretty sure the thread is created. I'll work on the next problem and see where it goes ;-)
Remo
 
Posts: 17
Joined: August 17th, 2012, 3:25 pm


Return to Phalanger project

Who is online

Users browsing this forum: No registered users and 23 guests

cron

User Control Panel

Login

Who is online

In total there are 23 users online :: 0 registered, 0 hidden and 23 guests (based on users active over the past 5 minutes)
Most users ever online was 511 on April 27th, 2024, 10:11 pm

Users browsing this forum: No registered users and 23 guests