Solved

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

Posted on 2006-06-19
5
184 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
[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
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
repeatSeparator  java  challenge 13 58
dog bark java program 15 121
while loop over for loop 7 116
matchUp  challenge 6 78
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seveā€¦
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.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 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