Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-06-19
5
Medium Priority
?
192 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 750 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

670 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