What is the problem about creating a thread in the DLL and releasing the thread? Delphi/Windows SDK/API
Http://www.delphi2007.net/DelphiAPI/html/delphi_20061128091220158.html
Can I create a thread in the DLL internal function, release the thread after the thread is executed, and then the function returns the corresponding value? In this case, how should we establish, execute, and release threads?
My parts Code As follows:
Problem 1: The main window cannot be operated during execution (this can be avoided by the rational thread), cpu100 %,
2: An error occurred while releasing;
Main Window:
Procedure tform1.button1click (Sender: tobject );
VaR
Return: integer;
Begin
Return: = readdata (); // call the dll read function
... // Process according to the returned value
End;
Dynamic library dll:
VaR
Readdatathread: treaddatathread; // thread Declaration
Readdata (): integer; stdcall;
Begin
Readthread: = treaddatathread. Create (false); // create a thread and execute it immediately.
Readthread. waitfor; // wait until execution is complete;
Readthread. Free; // release
End;
Thread unit:
Type
Treaddatathread = Class (thread)
Procedure read;
Private
Protected
Procedure execute; override;
End;
VaR
Time: integer;
Implementation
{Treaddatathread}
Procedure treaddatathread. read;
Begin
Time: = 0;
While ture do
Begin
If 9999 <time then exit; // exit when it reaches 9999
INC (time); // increments by 1
End;
End;
Procedure treaddatathread. Execute;
Begin
{Place thread code here}
Synchronize ();
End;
End.
Procedure treaddatathread. Execute;
Begin
{Place thread code here}
Synchronize ();
End;
--------> What did you execute? The thread's execution body is to throw the event to the main thread for processing ?!
You re-compile the thread-class code, which is a bit messy here.
When the tthread class thread is released, the terminate process is called to end the thread. The terminate process is simply set
Set the terminated flag of the thread class to finish executing the execute method, and then destroy.
Synchronize ();
The a process in the DLL will not be executed, because synchronize is used to process the main thread, that is, the owner of the DLL.Program.
Thank you, xjjrocker (with no trace) and dlwxn (dlwxn)
Can I create a thread in the DLL function and release it by the DLL function? What should I do? Thank you.
If you do not consider synchronizing synchronize with the master process.
The threads in the DLL are the same as those in the main program.
Freeonterminate: = true; when the thread is executed or created, the thread instance is automatically destroyed when the thread is terminated.
If the manual intervention thread terminates, freeonterminate: = true.
These conditions are static DLL calls, or dynamic DLL calls are not immediately performed on the DLL handle. Otherwise, the DLL will be released after the thread is not executed.
These prerequisites are static DLL calls or dynamic DLL calls that do not immediately release the DLL handle. Otherwise, the DLL will be released after the thread is not executed.
My idea should be like this:
The main program calls the DLL's read function, enters the READ function, and establishes a thread in the READ function. This thread is used to read data and exit the loop when conditions are set, release the thread and return to the READ function to establish the next statement of the thread for further execution. The data read in the thread is processed. Until the READ function exits. Then the main program that calls the DLL function is processed by the return value of the READ function.
In this way, when the main program calls the READ function, it will not be unable to move the main call and other window operations due to the loop in the read.
This is a process.
Hope you can help me !!! Thank you.
Return to the READ function and run the next statement that creates a thread.
--------------------- This is synchronization. What is the use of threads?
In DLL, function a creates a thread, passes in the handle of the system's main form, performs the operation, and then sends a message to the main form.
The main program calls.
The main program processes message B
Threads are asynchronous.