Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

Learn How to Stop A Thread Inside Python In A Delphi Windows App

Earlier we know how to create a Thread Inside Python for a Delphi App. However, we didn’t attempt to stop the thread while running. How to stop the thread inside python for a Delphi App with Python4Delphi? This post will help to understand better with the sample. You can also use Python4Delphi with C++Builder to build Python GUI apps on Windows.

Python4Delphi Demo33 Sample App shows how to achieve concurrency(using more interpreters) inside Python and stop in between while threads were running. You can find the Demo33 source on GitHub. This sample is a continuation of Demo11.

Prerequisites: Download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out this video Getting started with Python4Delphi.

Components used in Python4Delphi Demo33 App:

You can find the Python4Delphi Demo33 sample project from the extracted GitHub repository ..Python4DelphiDemosDemo33.dproj. Open this project in RAD Studio 10.4.1 and run the application.

Implementation Details:

In this Sample, Three thread instance were created and initialized with execution mode as shown below.

[crayon-67631ad334420054946752/]

 one interpreter Button uses an emNewState(single interpreter with new state and upon execution completion, restores the thread state) ThreadExecMode, and three interpreter buttons use an emNewInterpreter (same as a new state but with new interpreter fully initialized) ThreadExecMode to Execute.

[crayon-67631ad334427100419618/]

When all the thread is executed the OwnThreadState is restored.

[crayon-67631ad334429112427600/]

On Clicking one interpreter or three interpreter button, the three thread is initialized and executes python script by the TSortThread.ExecuteWithPython procedure. Upon execution, the 3 PaintBox’s is get updated by the TSortThread.VisualSwap procedure.

The threads can be stopped manually, by clicking the stop interpreters button Which stops the interpreters which are running using the below code.

[crayon-67631ad33442a552165031/]

In the sample output, the three interpreters button is clicked first and followed by stop interpreters.

<strong>Python4Delphi Demo33<strong>

 

Exit mobile version