Solved

When does CreateThread return???

Posted on 2000-02-21
10
303 Views
Last Modified: 2013-12-03
Hi,
 I am using CreateThread in VB5.
I would like to know when does it return so I can execute another thread.

plz add code :)
10x
Shaii

0
Comment
Question by:shaii
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 2542762
What do you mean?  It returns when the other thread has been created.  The other thread may or may not actually begin executing its code before CreateThread() returns.

>> plz add code :)
Code for what?
0
 

Author Comment

by:shaii
ID: 2542859
OK
I will make myself clear
I am recording from RS232.

I am runing a recording program in a thread
Dim lngRetVal As Long

RecThreadHwnd = CreateThread(0&, 0&, AddressOf StartRecording, lngStam, CREATE_SUSPENDED, lngThread1)

If SetThreadPriority(RecThreadHwnd, THREAD_PRIORITY_ABOVE_NORMAL) = 0 Then MsgBox "ERROR"

ResumeThread (RecThreadHwnd)

Do Until EndThreadFlag = True
     DoEvents
Loop
'now run the second recording

where EndThreadFlag is a global var that I change via a message from the program in the thread.

but this is not stable at all it works 1 out of 10 times.
I would like to know when I can run the second recording safely (... when does the thread return???)

Hope this makes it clear
Shaii
0
 
LVL 22

Accepted Solution

by:
nietod earned 20 total points
ID: 2542974
Where is the EndThreadFlag beign set to true/false?  If you are doing that improoperly it could lead to problems.

Anyways there is a better way to tell if a thread is running.  Use WaitForSingleObject() on the thread's handle.  This will return WAIT_OBJECT_0 if the thread has finished.  Otheriwse it will return WAIT_TIMEOUT.    (you can set the wait time to 0 to not wait at aill in this test.)
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:shaii
ID: 2543043
I couldn't find the CONST values for WAIT_OBJECT_O  and WAIT_TIMEOUT ...
didn't find them in the API viewer nor in the .h file.

well if u can plz add some code it would be perfect

10x
Shai
0
 

Author Comment

by:shaii
ID: 2543081
I couldn't find the CONST values for WAIT_OBJECT_O  and WAIT_TIMEOUT ...
didn't find them in the API viewer nor in the .h file.

well if u can plz add some code it would be perfect

10x
Shai
0
 

Author Comment

by:shaii
ID: 2543088
I couldn't find the CONST values for WAIT_OBJECT_O  and WAIT_TIMEOUT ...
didn't find them in the API viewer nor in the .h file.

well if u can plz add some code it would be perfect

10x
Shai
0
 
LVL 22

Expert Comment

by:nietod
ID: 2543329
I heard you the first time.  :-)

>>I couldn't find the CONST values for WAIT_OBJECT_O
they are defined in windows.h somewhere.

You are using windows.h, right?

code:

bool IsThreadRunning(HTHREAD ThdHnd)
{
    return WaitForSingleObject(ThdHnd,0) != WAIT_OBJECT_0;
};
0
 
LVL 22

Expert Comment

by:nietod
ID: 2543334
Oh, you're not using C/C++.  That's a problem.  There's no windows.h equivalent for VB is there?  I can probably tell you what those constants are.  I can't give you the code in VB.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2543350
The constants come from winbase.h and then from winnt.h

WAIT_OBJECT_0 = 0.
WAIT_TIMEOUT = 0x0102;  (that is 102 in hex).
0
 

Author Comment

by:shaii
ID: 2544970
10x man

I used it and it worked fine!

Shaii
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question