Solved

When does CreateThread return???

Posted on 2000-02-21
10
307 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

623 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