PowerBuilder Thread Manager: A Reusable Multithreading Framework

Alex Riofrio
CODE AUTHOR
Posts: 2
 1 week 4 days ago #724 by Alex Riofrio
Alex Riofrio created the code: PowerBuilder Thread Manager: A Reusable Multithreading Framework

PowerBuilder Thread Manager: A Reusable Multithreading Framework
How to execute concurrent tasks, manage worker pools, and handle task notifications through a reusable framework in PowerBuilder 2025 R2.

 

1. Introduction

PowerBuilder enables the development of robust enterprise applications, yet there are scenarios where executing operations sequentially can negatively impact interface responsiveness and user experience.

Operations such as database queries, processing large volumes of data, consuming Web API services, or tasks requiring long execution times can significantly benefit from concurrent execution.

Although PowerBuilder provides mechanisms for working with multiple threads through "Shared objects," implementing this functionality requires managing aspects such as object creation and communication, task assignment, execution tracking, and result retrieval.

This can considerably increase code complexity, especially when you need to run different types of tasks concurrently and reuse the same infrastructure across multiple applications.

Thread Manager was created to simplify this process by providing a reusable structure that allows tasks to run concurrently without having to implement the entire multithreading infrastructure every time.

 

 

 

2. The Thread Manager Approach

The philosophy behind Thread Manager is simple: the developer should focus on the task that needs to be executed, not on managing the threads that run it.

Thread Manager implements a Thread Pool-based model, maintaining a predefined number of Workers that receive tasks as they become available. When all Workers are busy, new tasks are placed into a queue for later execution.

Instead of creating and managing individual threads for each operation, the framework handles:

• Thread Pool Management: Managing a predefined set of Workers.

• Task Scheduling: Assigning tasks to available Workers and maintaining a queue when all are busy.

• Task Execution: Delegating execution to specialized objects based on the task type.

• Completion Notification: Notifying the Manager when a task has finished and delivering its result.

• Reusable Architecture: Allowing new task types to be incorporated without modifying the core infrastructure of the framework.

This approach allows the developer to submit a task and continue with other operations while the framework manages its execution in the background.

 

 

3. Framework Architecture

The framework is organized into five main components. Each has a specific responsibility, which helps maintain a modular and extensible architecture.

 

Component Responsibilities

1. nvo_thread_manager — Thread Pool Manager
This is the core component of the framework. It initializes and manages the pool of Workers, receives task execution requests, assigns tasks to available Workers, and maintains a queue when all are busy.

2. nvo_thread_worker — Task Executor
Each Worker is responsible for executing an assigned task. It receives the parameters, invokes the corresponding executor, and notifies completion through the Callback object.

3. nvo_thread_callback — Notification Manager
It acts as a communication bridge between the Workers and the Manager. It receives task completion notifications and forwards the results to the Manager.

4. nvo_thread_task — Base Task Class
It defines the structure and common interface for task execution. It provides parameter handling and the execution method implemented by specialized objects.

5. Specialized Task Objects
They implement the specific operations to be executed. Currently, the framework includes three task types:

• nvo_thread_task_dw: Executes operations related to DataWindows.

• nvo_thread_task_webapi: Handles HTTP requests and communications with Web API services.

• nvo_thread_task_process:
Executes custom or general-purpose processes.

This separation allows new task types to be incorporated without modifying the core infrastructure of the Thread Manager.

 

 

Submitting Tasks

Task submission is performed through the of_submit() method of the nvo_thread_manager object.
The developer only needs to specify the task type, the required parameters, and the Callback object to manage completion notifications.

For example, to execute a set of operations of type DW, WEBAPI, and PROCESS:

 

The Manager receives each request, identifies the task type, creates the corresponding executor object, and determines whether it can assign it immediately to a Worker or must place it in the pending task queue.
Once the task is submitted, the application can continue executing other operations without waiting for processing to finish.

 

Supported Task Types 

The framework uses an extensible model based on specialized objects. Each task type implements its own execution logic, while the Thread Manager maintains control of the concurrent infrastructure.
Currently, three task types are included:

DataWindow Tasks (DW)

They allow DataWindow-related operations to be executed concurrently. For example, queries, data processing, and other operations that can be encapsulated within a specialized executor.

Web API Tasks (WEBAPI)

They allow HTTP requests to be made to Web API services without blocking the main flow of the application. They are useful for consuming external services and processing their responses.

Process Tasks (PROCESS)

They allow custom or general-purpose processes to be executed. This type offers flexibility to incorporate application-specific operations.

The architecture also allows extending the framework by creating new specialized objects without modifying the core components of the Thread Manager.

 

Task Execution and Notifications

The execution process is coordinated by the Thread Manager and its Workers, following a task assignment model using a Thread Pool.
The execution flow works as follows:
 

  1. Task Submission: The application submits a task via of_submit().
  2. Task Creation: The Manager identifies the task type and creates the corresponding specialized object.
  3. Worker Assignment: If an available Worker exists, the Manager assigns the task to it. Otherwise, the request is placed in the FIFO (First In, First Out) queue.
  4. Task Execution: The Worker executes the operation by invoking the of_execute() method of the specialized object.
  5. Completion Notification: Upon completion, the Worker notifies the Callback object and reports the execution result.
  6. Result Handling: The Callback communicates completion to the Manager, which processes the notification and allows the task result to be handled.

When a Worker finishes its operation, it becomes available to receive a new task. If pending requests exist, the Manager continues distributing them in order of arrival.
This mechanism allows Workers to be reused throughout the entire execution of the application, avoiding the need to create a new thread infrastructure for each task.

 

 

4. The Demo Application

 

To demonstrate how Thread Manager works, a PowerBuilder 2025 R2 application has been developed that allows different types of tasks to be executed concurrently from a single interface.
The application is organized into four main areas, each designed to demonstrate different capabilities of the framework.

 

Thread Manager

This section allows you to control and monitor the operation of the Thread Manager.

 

It includes the following options and indicators:

  • Activate Manager (button): Initializes the Thread Pool and prepares the Workers to receive tasks.
  • MultiProcess (button): Starts a set of concurrent operations to test the behavior of the framework under load.
  • Queue Size Max: Indicates the maximum size of the pending task queue.
  • Pool Size (Workers): Indicates the number of available Workers.
  • Wait New DB Cnn: Indicates the wait time in ms related to the availability of new database connections.
  • Workload: Displays the generated workload.
  • Workers Available / Workers Busy: Allows you to view how many Workers are available and how many are currently executing tasks.
  • Tasks Queued: Indicates the number of tasks pending execution.
  • Restart Manager (button): Restores the Manager to its initial configuration, leaving the window ready to execute the MultiProcess option again.

 

It also includes an activity log (Log) that allows you to observe task assignment, Worker startup and completion, notifications, and queue processing.

Note: The framework uses an .ini file to define parameters that control the behavior of the Thread Manager. This allows you to adjust the number of Workers, queue capacity, and wait times related to database connections without modifying the source code.

The configuration is organized into two sections:

[General]

Queue_size = 50

Pool_size  = 3

 

[Database]

Waitnewcnn = 500

 

In this example, the framework is configured to allow up to 50 pending tasks in the queue, work with three Workers, and use a 500-millisecond wait interval for managing new database connections.

 

C/S DataWindows

This section demonstrates the concurrent execution of operations using traditional DataWindows in a Client/Server architecture.

The application displays different DataWindows containing business information, allowing you to observe how data-related operations are executed while the Thread Manager handles their distribution among the Workers.

The goal is to show that the framework can be used to execute DataWindow operations without having to reimplement the entire multithreading infrastructure.

 

 

For this demo, DataWindows were used to retrieve data from a SQL Server instance, specifically the AdventureWorks database, using an MSOLEDBSQL connection.

Each instance of the nvo_thread_task_dw object is designed to manage its own database connection.

 

 

Web API .NET DataStore

This section demonstrates integration with Web API services developed using .NET DataStore.

The interface features DataWindow controls bound to business record information and allows you to work with data obtained via Web API services.

This scenario shows how the Thread Manager can delegate HTTP/REST requests to independent Workers while the main application remains responsive for other operations.

For the demo, the source code for a .NET 10 Web API is provided, which connects to SQL Anywhere 17 via the "PB Demo DB V2025R2" ODBC data source.

 

Objects

The final section demonstrates that the framework is not limited to DataWindow operations or Web API service consumption.

It also allows executing custom processes encapsulated within objects that process images or simple strings.

In this Objects section, users can continue interacting with window controls—such as "Test UI" and the days-of-the-week DropDownListBox—while the Manager concurrently distributes and executes tasks among the Workers. This demonstrates that background task execution maintains interface responsiveness without interrupting user interaction with the application.

 

Running the Demo: Tips and Recommendations

Run from an Executable: It is recommended to run the demo from the compiled EXE rather than directly from the PowerBuilder IDE. This allows you to test Worker behavior under conditions that more closely resemble a deployed application.

Database Connectivity: Make sure database connections are properly configured before starting the tests, especially when multiple tasks are executed concurrently.

Thread Pool Configuration: Adjust the Pool_size and Queue_size values in the INI file according to the system's capacity and the workload you want to test.

Monitor Task Execution: Use the activity log to monitor task assignments, Worker availability, pending tasks, and completion notifications.

  

 

5. Conclusion

Implementing multithreading in PowerBuilder does not have to be a complex task or require each application to build its own thread management infrastructure from scratch.

Thread Manager provides a reusable structure that simplifies concurrent execution through a Thread Pool, a task queue, and a centralized notification mechanism.
The presented framework also serves as a foundation for future extensions, such as new types of executors, task tracking mechanisms, and additional Thread Pool management options.
Ultimately, my goal is to offer PowerBuilder developers a practical and reusable way to incorporate concurrent execution into their applications, taking full advantage of multithreading capabilities without repeatedly solving the same infrastructure challenges.

 

 

This message has an attachment file.
Please log in or register to see it.

Please Log in or Create an account to join the conversation.