Using Multithreading

When you deploy a PowerBuilder application that contains shared objects or an NVO assembly to .NET, the application can be run in a multithreaded environment. The PowerBuilder .NET runtime library also supports .NET synchronization, enabling your application to avoid possible data corruption.

.NET Threading in PowerScript

This PowerScript code fragment uses .NET threading:

#if defined PBDOTNET then
//Declare a .NET Class
System.Threading.Thread ithread
	
//Declare a delegate for .NET Thread
System.Threading.ThreadStart threadproc 

//Assign a user defined PowerScript 
//function to the delegate
threadproc = f_compute	
		
FOR Count = 1 TO a_count
 ithread = create System.Threading.Thread(threadproc)
	ithread.IsBackground = true
 ithread.Start()
 ithread.sleep(500)
NEXT

#else
	/*action*/
#end if

Using .NET Synchronization Functions

To use .NET synchronization functions directly in PowerScript:

  1. Declare a global variable.
  2. Initialize the global variable.
  3. Use the global variable in your .NET synchronization functions. Define your types and functions within #IF DEFINED and #END IF preprocessor statements.

Windows Form example:

/* declare and initialize global variable */
System.Object obj
obj = create System.Object

#if defined PBWINFORM then
  System.Threading.Monitor.Enter(obj);
#else
    /*action*/
#end if

b = 1000/globala
globala = 0

a = 1000

globala = 10
b = a / globala

#if defined PBWINFORM then
  System.Threading.Monitor.Exit(obj);
#else
    /*action*/

#end if

return 1