Page 1 of 1

The function "WaitThread()" is freezing the calling program!

Posted: Sat Jan 06, 2024 4:11 pm
by Maya
Hello..
In the following Dll code, I wrote the function "WaitThread()" but it freezing the calling program & keep it in the "Windows Task Manager" forever!
Is there any explanation?
Thank you.

Code: Select all

ProcedureDLL DoSomeThing(Num)
  ; Do something ...
EndProcedure

ProcedureDLL AttachProcess(Instance)
  DllThread.l = CreateThread (@DoSomeThing(), 0)
  WaitThread(DllThread)           ; <<< Causes the Program to freeze in the "Windows Task Manager" forever!
EndProcedure

Re: The function "WaitThread()" is freezing the calling program!

Posted: Sat Jan 06, 2024 4:27 pm
by skywalk

Re: The function "WaitThread()" is freezing the calling program!

Posted: Sat Jan 06, 2024 4:31 pm
by Maya
skywalk wrote: Sat Jan 06, 2024 4:27 pm timeout
I tried "Timeout", but it didn't work when it comes in a Dll:

Code: Select all

ProcedureDLL DoSomeThing(Interval.l)
  ; Do something ...
EndProcedure

ProcedureDLL AttachProcess(Instance)
  DllThread.l = CreateThread (@DoSomeThing(), 1000)
  WaitThread(DllThread)       ; <<< Causes the Program to freeze in the Tasklist 
EndProcedure

Re: The function "WaitThread()" is freezing the calling program!

Posted: Sat Jan 06, 2024 4:42 pm
by DarkDragon
Maya wrote: Sat Jan 06, 2024 4:31 pm
skywalk wrote: Sat Jan 06, 2024 4:27 pm timeout
I tried "Timeout", but it didn't work when it comes in a Dll:

Code: Select all

ProcedureDLL DoSomeThing(Interval.l)
  ; Do something ...
EndProcedure

ProcedureDLL AttachProcess(Instance)
  DllThread.l = CreateThread (@DoSomeThing(), 1000)
  WaitThread(DllThread)       ; <<< Causes the Program to freeze in the Tasklist 
EndProcedure
WaitThread blocks the process until DoSomething() returns. AttachProcess is being launched from the main thread, so all GUI and stuff will hang until the thread is done. May I ask, why would you wait for the thread to be done at this point?

Re: The function "WaitThread()" is freezing the calling program!

Posted: Sat Jan 06, 2024 4:56 pm
by skywalk
Well, to be honest, I only use waitthread() upon exiting my applications where I want to shutdown any threads.
I have not used it within a dll context.
I use semaphores and mutex to communicate and control running threads.
WaitThread() was not stable for me.

Re: The function "WaitThread()" is freezing the calling program!

Posted: Sat Jan 06, 2024 4:56 pm
by User_Russian

Re: The function "WaitThread()" is freezing the calling program!

Posted: Sat Jan 06, 2024 5:01 pm
by Maya
WaitThread blocks the process until DoSomething() returns. AttachProcess is being launched from the main thread, so all GUI and stuff will hang until the thread is done. May I ask, why would you wait for the thread to be done at this point?
Hi Daniel,
Yes, my program is get lost only when this function written inside AttachProcess().
I'm working on a new program that hide certain type of files using kernel mini-filter.
Any solution for how to wait for a thread inside a Dll?

Re: The function "WaitThread()" is freezing the calling program!

Posted: Sat Jan 06, 2024 5:05 pm
by DarkDragon
Maya wrote: Sat Jan 06, 2024 5:01 pm
WaitThread blocks the process until DoSomething() returns. AttachProcess is being launched from the main thread, so all GUI and stuff will hang until the thread is done. May I ask, why would you wait for the thread to be done at this point?
Hi Daniel,
Yes, my program is get lost only when this function written inside AttachProcess().
I'm working on a new program that hide certain type of files using kernel mini-filter.
Any solution for how to wait for a thread inside a Dll?
Why do you wait or better said: why do you use a thread if you wait for it anyway? You need to solve your problem differently, if you need so much time in your thread to make it hang. What do you do exactly in your thread?

Re: The function "WaitThread()" is freezing the calling program!

Posted: Sun Jan 07, 2024 12:45 pm
by Maya
Why do you wait or better said: why do you use a thread if you wait for it anyway? You need to solve your problem differently, if you need so much time in your thread to make it hang. What do you do exactly in your thread?
I'm using the Thread because I need to separate the Thread task when executable run.
Executable need to wait the Thread to be completed before proceeding ahead.

"AttachProcess()" is the ideal place to put the "WaitThread()", but unfortunately, when the Thread is completed, it doesn't allow executable to continue running, but only freezing it inside the Windows Task Manager.
If you don't have the solution, then please dismiss this subject.

Re: The function "WaitThread()" is freezing the calling program!

Posted: Sun Jan 07, 2024 12:54 pm
by Fred
Just as a general comment you should never use .l explicitely if not needed (most use are only in structure to reduce their size). You code probably won't work on x64 because of that.

Re: The function "WaitThread()" is freezing the calling program!

Posted: Sun Jan 07, 2024 1:03 pm
by freak
Maya wrote: Sun Jan 07, 2024 12:45 pm"AttachProcess()" is the ideal place to put the "WaitThread()", but unfortunately, when the Thread is completed, it doesn't allow executable to continue running, but only freezing it inside the Windows Task Manager.
If you don't have the solution, then please dismiss this subject.
What you are trying to do is impossible. The reason is that when a thread starts or ends it tries to call the AttachThread() and DetachThread() dll procedures (even if you do not define them in the code). However, they cannot be run as long as the AttachProcess() call you are in is not finished because only one can run at a time. So it is a classic deadlock situation.

You have to find another way to do what you want.

Re: The function "WaitThread()" is freezing the calling program!

Posted: Sun Jan 07, 2024 1:22 pm
by Maya
because only one can run at a time. So it is a classic deadlock situation.
Fully Understood.
Thank you fred & freak for this explanation.