Solved

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

Posted on 2006-06-19
5
180 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 

Author Comment

by:kai028
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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 …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 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

11 Experts available now in Live!

Get 1:1 Help Now