Valid CWinThread handles

Hello fellow newsgroupies!

Is there a way to find out whether or not a CWinThread process has
stopped or not or is still valid.

I have several CWinThread's (none of which are set to auto-delete)
which may occasionally finish their processes before I signal them to
stop. I sometimes need to reuse these CWinThread's and restart their
processes but I need to know that they are no longer running first.
They may already have their stop CEvent set but the thread may have
already acted on it and stopped.

Their m_hThread values will not be NULL but no longer handles to valid
threads.

Any suggestions gratefully received.

Many thanks in advance!

'Newsgroupie'
England
0
buzbymb (44)
10/30/2008 9:11:34 AM
vc.mfc 33608 articles. 0 followers. Follow

3 Replies
709 Views

Similar Articles

[PageSpeed] 25

Newsgroupie wrote:
> Hello fellow newsgroupies!
> 
> Is there a way to find out whether or not a CWinThread process has
> stopped or not or is still valid.
> 
> I have several CWinThread's (none of which are set to auto-delete)
> which may occasionally finish their processes before I signal them to
> stop. I sometimes need to reuse these CWinThread's and restart their
> processes but I need to know that they are no longer running first.
> They may already have their stop CEvent set but the thread may have
> already acted on it and stopped.
> 
> Their m_hThread values will not be NULL but no longer handles to valid
> threads.
> 
> Any suggestions gratefully received.

NG:

You can call ::WaitForSngleObject() on a thread that has stopped. It should 
signal immediately.

-- 
David Wilkinson
Visual C++ MVP
0
no-reply8010 (1791)
10/30/2008 10:03:47 AM
See below...
On Thu, 30 Oct 2008 02:11:34 -0700 (PDT), Newsgroupie <buzbymb@yahoo.co.uk> wrote:

>Hello fellow newsgroupies!
>
>Is there a way to find out whether or not a CWinThread process has
>stopped or not or is still valid.
****
Do you mean "process" or "thread"?  CWinThread can, at best, represent a thread, and does
not represent a process.
****
>
>I have several CWinThread's (none of which are set to auto-delete)
>which may occasionally finish their processes
****
Do not use the word "process" to talk about threads.  Threads are threads, and processes
are processes.
****
> before I signal them to
>stop. I sometimes need to reuse these CWinThread's and restart their
>processes 
****
I have no idea what this means.  You cannot "reuse" a thread after it has terminated.  If
it has terminated, it is dead, and there's nothing else you can do with it.  "Terminated"
means "terminated".
****
>but I need to know that they are no longer running first.
****
I think you do not understand threads.  
****
>They may already have their stop CEvent set but the thread may have
>already acted on it and stopped.
****
Then if it has stopped, it is dead, and you cannot "reuse" it.  So your cleaning of
m_bAutoDelete is essentially meaningless.
****
>
>Their m_hThread values will not be NULL but no longer handles to valid
>threads.
****
They are valid thread handles to terminated threads.  You cannot do much with the thread
handle to a dead thread except examine some of its state, such as its exit code (which is
rarely of interest anyway).  You cannot "restart" a terminated thread, so the basic
premise you have here is wrong.

So the question is, why did not not simply allow the default value of m_bAutoDelete to
remain TRUE so the now-completely-useless CWinThread object is automatically deleted? Does
it serve any useful purpose once the thread has terminated?

See my essay on worker threads.  The best way is to have the thread do a PostMessage to
the main GUI thread informing it that the working thread has terminated.
WaitForSingleObject on the thread handle is rarely good practice, unless this is done in a
way that does not block the GUI thread or any other thread that actually matters.

If you want a pool of worker threads (which allows threads to be "reused" because they in
fact have *not* terminated) then you can use a queueing mechanism to enqueue requests,
such as a UI thread using PostThreadMessage, a worker thread using an I/O Completion Port,
or a hand-rolled queue using a mutex/CRITICAL_SECTION and a semaphore (see my essay on
semaphores).  In this case, your "termination" will really mean "terminate this thread
from the pool" and you will need a different protocol that says "please stop the thread
computation and return to the queue processing"
****
>
>Any suggestions gratefully received.
****
You need to describe your problem a bit better, because it sounds like the solution you
are trying to implement is based on erroneous assumptions such as the idea that a thread
can be "reused".
				joe
****
>
>Many thanks in advance!
>
>'Newsgroupie'
>England
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15972)
10/30/2008 3:59:43 PM
On Thu, 30 Oct 2008 02:11:34 -0700 (PDT), Newsgroupie <buzbymb@yahoo.co.uk>
wrote:

>Hello fellow newsgroupies!
>
>Is there a way to find out whether or not a CWinThread process has
>stopped or not or is still valid.
>
>I have several CWinThread's (none of which are set to auto-delete)

Those two questions are at odds with one another. The problem is the
auto-delete, and you've described a race condition. You can't tell if the
CWinThread is alive without touching the CWinThread, which may already have
been auto-deleted or in the process of it.

>which may occasionally finish their processes before I signal them to
>stop. I sometimes need to reuse these CWinThread's and restart their
>processes but I need to know that they are no longer running first.

I'd just create new ones or use a thread pool.

>They may already have their stop CEvent set but the thread may have
>already acted on it and stopped.
>
>Their m_hThread values will not be NULL but no longer handles to valid
>threads.

Yes, because the CWinThread's have auto-deleted themselves, so nothing they
contain is valid. The solution is to turn off auto-delete. It is rarely if
ever appropriate to allow auto-delete.

>Any suggestions gratefully received.

Please read my FAQ page:

http://members.cox.net/doug_web/threads.htm

-- 
Doug Harrison
Visual C++ MVP
0
dsh (2498)
10/30/2008 5:03:43 PM
Reply:

Similar Artilces:

Msgbox entry validation
The following Macro will not loop more than twice the error entries. Will someone help me correct it to loop indefinely? Thanks Sub Entry_validation() Dim Teststring As String Dim Inputstring As String Cells(1, 1) = "ABC" 'as given filename Cells(2, 1) = "DEF" 'as given filename Cells(3, 1) = "GHI" 'as given filename Cells(4, 1) = "JKL" 'as given filename errorloop: Inputstring = InputBox(prompt:="Enter Filename" & vbLf & "Enter abort to abort en...

Data Validation -> Validation list is larger than the cell width
I have a workbook that I've just added data validation to one cell. FWIW, the data validation is based on a named range. Here's the (perceived) problem. When I've set up data validation in the past, the selection is the same width as the cell. In this case, the list starts almost a full cell width to the left. What am I missing? Thanks, Barb Reinhardt Barb, In Excel2003 I can replicate what you describe under the following conditions: 1. the named range contains entries that are wider than the data validation cell 2. The data validation cell is r...

Validating against a DTD
Hello All, I am trying to validate an xml file against a DTD. I went through a lot of examples especially: http://www.xmlforasp.net/codebank/util/srcview.aspx?path=../../codebank/System_Xml/XmlValidatingReader/Validator/validator.src&file=validator.cs&font=3 but its not working in my case. First of all, when i try to add my DTD file in XmlSchemaCollection object, it throws an error saying DocType element is expected. I am using NITF DTD for this and there is no DocType element in it. Any help will be appreciated. Thanks. VD You should not add DTDs to XmlSchemaCollection. XmlSchema...

Data Validation
Hi all I want to set data validation on a cell so that it will accept the following (and only the following) text strings: (1) "BR" (literally) or (2) "NT" (literally) or (3) "nA" where n is any positive integral numerical value including zero and A may take any of the values "L", "P", "T", "V" or "Y" or (4) "An" where n is any positive integral numerical value including zero and A may take either of the values "K" or "D". Is this possible, please, and if so how? thanks -- Return e...

Validation dropdown list is not coming visible
Hi, I'm using XP and 2003. I have a problem that I have once found resolution, but now it came again and cannot remember what needs to be done. Here is the problem: There is a column where Validation is specified in right manner with error message in case of error. When selecting the cell - dropdown list is not coming visible. In case I write something wrong it gives error message. If I recall correctly - it was something to do with some kind of list. How can I change this to work properly? I tried to paste new column from another file where the column is working as I want...

Partition Handling
I didn't get an answer in replication so I am posting this here: We've got some big SS 2000-style partitioned views using traditional horizontal partitioning that we want to migrate to the new SS 2005 partitioning. The underlying tables to the view are partitioned by date in all cases and so each view holds 3-10 years of data with each table generally being quarterly. One nice thing is that when we have a replication issue and replication breaks and needs to be reinitialized, we can specify just the most recent table. In other words, if I have to reinitialize I only r...

reinstall-validation problem
Hardware hit from lightning so new hd and video card and start over. All goes well with install using a slipstreamed XP-Pro SP3 disk but I notice it never asks for the Product Key during install yet it Activates itself during one of the boots and I think little of it until ... During the updates which followed (100+!!!) I notice Windows Defender doesn't seem to run or leave itself installed. As it is running on my laptop figured I just download it. When I run it, it wants to first do the Genuine Advantage bs which I let it, at which time it says "bogus". Ugh, g...

Validation
Julie the same can be achieve without going thru the double drop down boxes. You could do it just using the combobox alone. Thank again Please stay in the ORIGINAL thread. The archives will thank you. -- Don Guillett SalesAid Software donaldb@281.com "JLong" <anonymous@discussions.microsoft.com> wrote in message news:034101c49c3b$889b5020$a501280a@phx.gbl... > Julie the same can be achieve without going thru the > double drop down boxes. You could do it just using the > combobox alone. Thank again ...

validating macro
Hi I am sending a spread sheet out to branchs I need them to complete a sheet (common info) before they do anything else on the workbook. I am looking for a macro or code for VBA which who check (common info) see if the cells are complete if not bring up a error screen informing the user to complete the form and then take them straight to the (common info) sheet. and if possible let them go to the tab which they want to go to. Use an extended version of something along these lines: (if there are lots of cells use a 'for row numbers x to y' or 'do until' statment, o...

Data Validation
I have set up a data form and have data validation rules on the various input cells. I see that there is an option called "Ignore blanks" in the data validation settings, which is supposed to restrict someone from entering past an input cell without entering valid data, if the "Ignore blanks" box is unchecked. I have tried to do this, but it does not seem to work. Please could someone tell me what I am doing wrong? Provide information, or better yet, a screen capture of the Data Validation dialog box with the Setting tab selected (User ALT+PrintScreen to copy the imag...

Reboot Validator
I'm running XP Pro, SP3. Attempting to install a program from Nero. Software keeps failing at startup, despite numerous re-installs. During installs, it keeps attempting to update the 'reboot validator' which it does not seem to be able to do. What is the reboot validator, and how do I update it. Couldn't find anything useful on MS site and Nero Tech support just says its an MS problem. -- Thank you, B. Parker Never heard of it but Nero Support should know about it: Reboot Validator appears to be a Nero component; cf. http://www.brighthub.com/comput...

Gettting divorced
After 15 yrs of marriage I'm getting divorced. My spouse is no longer willing to share her transaction data with me. I could start a new file, but I would prefer to keep my current file, which has 5 years of data in it. Since my spouse is not willing to give me the data to properly handle the divorce in MS Money, I have the following question What category should I use for the adjustments required to bring my spouse's 401K and stock options accounts to $0? I checked the FAQ, but there's no section on divorce and how to handle things in Money. Dick, any suggestions? As I go...

Creating Generic Class To Handle Shared Memory
Hi, I wan to create Generic class to handle shared memory in win32. Like Create Shared memory, map/unmap objects to it, Mutex to synchronize among different clients (processes), implement Lock and Unlock functions to prevent simultaneous access by different processes. Can anyone please give me guidelines to create such class or set of all API functions required for it. if anyone have source code and want to share it i will be very thankful. Thanx in advance Akshay. This is actually very difficult, mostly because you have to write your own allocator for the actual shared memory. In general, ...

Conditional validation?
Hello All I have a sheet that is essentially just columns of data (except for the column headings in the first row), and two of the columns are what I would call 'mutually exclusive': i.e. if a value is entered in a row in either column, the user must be prevented from entering any value in the same row of the other column. The two columns already have some basic validation applied (ranges of permissable numerical values), and this validation would need to be applied for any values that are entered. I am sure this can be done, but cannot see how! Hope someone can help. Many thanks L...

Excel 2000 vs 2002 Sheet protection & Validation cells
Hello, I have an Excel spreadsheet built on Excel 2000. I have set sheet protection with password, and I have some unprotected cells with drop down menu (validation with 50 choices) Everything works very well. I sent this file to a friend who has Excel 2002. The same "unprotected" validation cells cannot be modified in his version as the message says that these cells are protected What can be done in order to make it work on his version?? I do have some "very hidden" sheet with password protected VBA properties, if that is an issue. Similarly there are some macros o...

REPOST: dynamic validation
I think I wasn't clear that I need the Validation option under the Data menu, List, source values. That is what I am trying to fill with this equation. In other words, range1 and range2 are sets cells which contain values. I want those values to be the dropdown options in the cell based on what the previous cell to the left is selected as. So if Range1 is three cells with Tall, Dark, Handsome and Range2 is Sweet, Pretty, Nice, then based on the selection of F or M in A1, I want the choices for B1 to be one of those lists. I know that quotes are required for the IF. My probl...

How to use data validation
[NB: I re-structure my question since people got misled from my previous one] What I really wish to ask is: I wish to use data validation for a particular cell. But it doesn't work if I choose "custom" in the "allow" box. 1) In Data | Validation | Settings Tab | Validation Criteria, there is an "Allow" Box. 2) I chose "custom", there is a formula. 3) I type in some formula, eg: =J3 (Just Reference) =J3-J1 (Reference-type formula) =round(J1) (Function-type formula) None of them can activate the validation fu...

Email Validation
I was testing this email validation rule. Is Null Or ((Like "*?@?*.com") Or (Like "*?@?*.org") Or (Like "*?@?*.net") Or (Like "*?@?*.mil") Or (Like "*?@?*.US") And (Not Like "*[ ,;]*" And Not Like "*.@*")) ...but noticed I was able to input .@any.com ... e.g., I was trying to prevent user typing a .@ [that is a dot@] using the ... And Not Like "*.@*")) .. but it must be getting cancelled out by something previous in the validation string. Any suggestions? You could use a routine such as: P...

Refer to seperate workbook for validation list for drop downs?
Id like to keep my validation data for drop downs in a seperate workbook so that many users/worksheets can reference it. However- data validation doesnt seem to let me reference named ranges in a seperate workbook- any advise out there? -- Dee Visit Debra Dalgleish's site: http://www.contextures.com/xlDataVal05.html But take note that the other workbook has to be open. Dee wrote: > > Id like to keep my validation data for drop downs in a seperate workbook so > that many users/worksheets can reference it. However- data validation doesnt > seem to let me reference named r...

Handling foreach ($item in $null)
Hello list, Let's assume that I have an XML file that looks like this: $toys =3D [xml] @" <toys> <toy name=3D"Jigsaw"> <minage>6</minage> <price>19.50</price> <comments> <comment>A very nice gift for all kids!</comment> </comments> </toy> <toy name=3D"Meccano"> <minage>7</minage> <price>14.50</price> <comments> <comment>I bought it for my son, but I was the one playing the most often!</comme...

Data Validation: Validate Specific Day of the Week?
Users will enter a date in cell D6. The date they enter must be a Monday. How can I validate for that? Thanks, -Ted I entered dates in E1:E10. I selected E1 In the Data | Validation dialog, I used Custom and entered =WEEKDAY(E1)=2 I selected all the dates and opened Data | Validation; it offered to apply the validation to all the cells Done best wishes -- Bernard Liengme Microsoft Excel MVP http://people.stfx.ca/bliengme "Ted" <ted.gallagher@gmail.com> wrote in message news:#o$yRC6vKHA.404@TK2MSFTNGP02.phx.gbl... > Users will enter a date in ce...

Data validation for 2 valid values
I want a cell to allow only 2 valid values but not have a drop-down list appear. How do I do that with Data->Validation? Data>validation>allow>list, put the 2 values separated by a comma in the source box, uncheck in cell dropdown -- Regards, Peo Sjoblom "Hall" <hall@garp.org> wrote in message news:ujuB%23w52DHA.2000@TK2MSFTNGP11.phx.gbl... > I want a cell to allow only 2 valid values but not have a drop-down list > appear. > > How do I do that with Data->Validation? > > Using a list, as Peo suggested, will force users to match the ...

Detect Valid Full backup
To detect if there is a valid Full backup exist, before starting a log backup I check of the LastBackupDate is filled. Sometime this give the following error. Error.Message : BACKUP LOG cannot be performed because there is no current database backup. BACKUP LOG is terminating abnormally. Error.Source : .Net SqlClient Data Provider Error.StackTrace : at Microsoft.SqlServer.Management.Common.ConnectionManager.ExecuteTSql(ExecuteTSqlAction action, Object execObject, DataSet fillDataSet, Boolean catchException) at Microsoft.SqlServer.Management.Common.ServerConnection.Execute...

Validating values of POEMDEV
Hi all, I've written a OEMUI.DLL. The basic code comes from the WINDDK sample. The values from the gui are stored in the POEMDEV. VERBOSE(DLLTEXT("UserName :<%s>.\r\n"), ((POEMDEV)pOEMDM)->szUserName); VERBOSE(DLLTEXT("CostCenter:<%s>.\r\n"), ((POEMDEV)pOEMDM)->szCostCenter); VERBOSE(DLLTEXT("ColorMode :<%s>.\r\n"), ((POEMDEV)pOEMDM)->bColorMode ? L"on" : L"off"); I like to find a place in this DLL, where I can verify the values of POEMDEV. Greetings Kai -- ISO Software Systeme GmbH Nuremb...

validating against schema with namespaces
I have a very complex XDR schema that uses namespaces: xmlns="urn:schemas-microsoft-com:xml-data" xmlns:b="urn:schemas-microsoft-com:BizTalkServer" xmlns:d="urn:schemas-microsoft-com:datatypes" Do I need to build a custom validator in order to validate XML documents against this schema? How does the XML parser know how to validate against the extra namespaces? For example, schemas-microsoft-com:datatypes allows for attributes such as maxLength, minLength, etc. If my XML document contains a value that doesn't meet these criteria, then the document should n...