Solved

When does CreateThread return???

Posted on 2000-02-21
10
300 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
  • 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Mobile 6.5 device Apache Flex 3 45
Visual Studio 2015 locks debug executable 9 180
Microsoft C++ code failing in executable that worked 9 97
Windows ICD FFU Issue 4 35
If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
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…

786 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