Using Multithreading

When you deploy a PowerBuilder application that contains shared objects 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:

//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

Using .NET Synchronization Functions

When using threading in PB .NET, consider using .NET synchronization functions to protect user data from corruption. Synchronization functions require passing a System.Object instance to the functions, so your script must define a System.Object variable To use .NET synchronization functions in PowerScript:

  1. Declare a global variable.
  2. Initialize the global variable.
  3. Use the global variable in your .NET synchronization functions.

For example:

//Declare a System.Object class
System.Object obj

//PowerScript function definition
//using .NET thread synchronization
//functions

global subroutine f_compute ();
  System.Threading.Monitor.Enter(obj);
  gCount = gCount + 1
  System.Threading.Monitor.Exit(obj);
end subroutine

Printing DataWindow and DataStore Objects

For printing DataWindow and DataStore objects, these limitations apply: