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

Just starting out? Need help? Post your questions and find answers here.
Maya

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

Post 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
User avatar
skywalk
Addict
Addict
Posts: 4215
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

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

Post by skywalk »

The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Maya

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

Post 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
DarkDragon
Addict
Addict
Posts: 2345
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

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

Post 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?
bye,
Daniel
User avatar
skywalk
Addict
Addict
Posts: 4215
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

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

Post 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.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Maya

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

Post 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?
DarkDragon
Addict
Addict
Posts: 2345
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

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

Post 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?
bye,
Daniel
Maya

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

Post 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.
Fred
Administrator
Administrator
Posts: 18178
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

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

Post 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.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

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

Post 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.
quidquid Latine dictum sit altum videtur
Maya

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

Post 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.
Post Reply