Solved

Inconsistency between Debug and Release configurations when using serial port under Windows with C++

Posted on 2006-06-19
5
181 Views
Last Modified: 2013-11-20
I have a program that uses the serial port with Windows XP (it is written in Visual C++ 4 - yes, 4!). It has a separate thread for receiving data from the port.

The problem is that, while everything works fine in both Debug and Release versions, there is a problem with exiting the application. When I exit from the Release version, and try to restart it, I get a message saying that the port cannot be opened. Task Manager says the application still has a process running. This doesn't happen with the Debug version. I am using CloseHandle, which succeeds, and I am setting the port handle to NULL afterwards.

This is the first time I'm writing a multiple-thread application for Windows, so I'm a bit lost. Why don't ALL my threads exit when I exit my program under Release configuration?

Does anybody know what I can do to make the program work under Release the same way it does under Debug? I'd really appreciate the help. Thanks!
0
Comment
Question by:kai028
  • 3
  • 2
5 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 16936506
You need to stop properly all running threads, and exit main application thread. What is your application type (Console, Windows application)?
Quick and dirty solution is using CRT exit() function.
0
 

Author Comment

by:kai028
ID: 16936530
Thanks for the comment, Alex. This is a Windows application, not a console app. I'm not actually using MFC - I didn't know how to use the serial port under Windows, so I found an old Microsoft port demonstration program, written in the straight old API, to base my app on.

How do I "properly stop all running threads"? I thought setting the thread ID to 0 would do that.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16936600
First, try TerminateThread for every running thread - this must solve the problem of exiting application. However, this is not 100% correct solution.
To stop thread properly, you need to ask thread to exit, and wait while thread actually exits. This is done using event associated with thread - let's call it Stop event. THread periodically tests Stop event state and exists immediately when it is signaled. When main thread needs to stop worker thread, it sets Stop event and waits for thread handle.

0
 
LVL 48

Accepted Solution

by:
AlexFM earned 250 total points
ID: 16936609
0
 

Author Comment

by:kai028
ID: 16936726
Thanks, Alex. I'll go over your earlier answer and see if I can use the info to fix my program. BTW, I am using SendMessage to send stuff from my serial port thread back to the main thread. I suppose this isn't really the best way to alert the main thread that new data is available - I should probably use some kind of semaphore, but I have no experience doing that. Oh, well; I'm going to have to learn sometime!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl Awk Need Help 3 109
VBA "SendKeys" Syntax for Multiple Keystrokes 7 103
How to convert MFC::CString to UTF8 wchar_t* 10 147
Change to event 1 101
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now