|Download:||( multithread.zip 9Kb )|
With PB6 it is possible to create MultiThreaded applications. This can be done by using Distributed PowerBuilder in combination with server push. A process is started on the server, the server send a message back to the client ( the server push ) and the associated event/method on the client is executed, while being in another client process.
However, you now need two applications, namely a server and a client app. This is not what you really want when creating MultiThreaded applications: you want one app with 1 or more threads. Now, best part is that we can used the Distributed PB functions on the client as well. Most important function is the SharedObjectRegister() function. When calling this function, PowerBuilder opens a separate runtime session for the shared object and creates the shared object. This is exactly what we need: another session. Normally, this function is used on the server side. We however use this function in the same application in which we then use the SharedObjectGet() function use the get the handle to the created object. The conbination of these functions act in fact the same as the 'create' statement, only in a seperate runtime session.
Now, to use this we must have a nonvisual object because that is the only type these functions work with. No problemo. So, we create a NVO which has a method called of_start(). This method must use server push to inform our application to execute a specific thread. The method can only be called asynchronously otherwise it will be executed at once and not in a new thread. So, the method is called using the POST keyword. In the of_Start method we have a loop of 5 seconds. In each iteration, we send a message to the callee to do something. Notice that we do not implement a yield() to allow other process some CPU time. If we create another NVO using the DPB functions, when calling nvoA.POST of_Start() and then nvoB.POST of_Start(), these functions will be executed at the same time. So, messages from nvoA and messages from nvoB will be received at the same time. If we use the performance monitor on NT, we actually see that 2 extra threads are created.
Check out the example PB app which has to options: singlethreaded run and multithreaded run. When Single, both NVOs are created in the normal way and are executed sychronously. When Multi, the NVOs are created using the DBP functions and are executed simultanously.
PS. This is a test application and is not stable. It might crash when exiting the application during development. The EXE is pretty stable. Check it out and let me know what you think, especially if this is MultiThreaded or not.