473,473 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

VC++ 2005 Threading for Complete Morons

Hi. I have what I believe to be a very simple task I'm trying to
accomplish in VC++ 2005 and I simply can't make it work. I have a good
working knowledge of C, a basic-to-middling knowledge of C++, and a
novice-level knowledge of OOP in general and Visual Studio in
particular.

I am not a full-time programmer. I am an aerospace engineer who is
trying to make a useful tool. Remember this.

Anyway, the task breaks down to this. I've made a GUI for one of my
simulator codes. It reads in data from fields and runs the simulator
from that; that all works fine. Here's the problem: I have a Kill
button on the main UI that I need use of while my sim code is running.
The obvious answer would be to put the sim execution in another thread,
leaving the first thread to handle the UI. Problem: There is a
ProgressBar on the UI that needs to update as the simulator moves
through its allotted time. I need this so that the user will know when
the sim is hanging so that s/he can hit the Kill button.

Secondary problem: I also have a message bar that I'd like to update
with a "Simulation finished" message when the case has completed
successfully. This means I need some way for the first thread to know
when the second thread has returned.

I have Googled this subject to death, in these groups, in MSDN, and on
the rest of the Web. I have an 1,100-page book specifically dealing
with VC++ 2005 that says NOTHING, about thread programming. The online
articles I have encountered have either been so simple as to be
worthless in this application, or so complicated as to lose me two
paragraphs in.

This CANNOT, simply CANNOT be a hard thing to do, even for an amateur
programmer, this should be only a step or two beyond a message box with
a "Hello World" and a button. I know there are at least four different
ways to launch threads in a VC++ program: native C++, MFC calls, CLI
Thread classes, and CLI ThreadPool classes. I've tried the last two,
and I'm currently using the last one because it enabled me to actually
pass an argument to it, albeit through a structure that I had to make
specifically for it.

Can somebody please help me?

Jan 18 '07 #1
5 1887
In article <11**********************@q2g2000cwa.googlegroups. com>,
<co*********@sbcglobal.netwrote:
>Anyway, the task breaks down to this. I've made a GUI for one of my
simulator codes. It reads in data from fields and runs the simulator
from that; that all works fine. Here's the problem: I have a Kill
button on the main UI that I need use of while my sim code is running.
The obvious answer would be to put the sim execution in another thread,
leaving the first thread to handle the UI. Problem: There is a
ProgressBar on the UI that needs to update as the simulator moves
through its allotted time. I need this so that the user will know when
the sim is hanging so that s/he can hit the Kill button.
Frankly, I think that writing a threaded application without really
understanding threads is going to lead to a LOT of bugs, problems, and
the like. Threads can make your app a lot more nondeterministic, and
while they might be able to make the first 90% of your work easier,
they make the last 10% take the other 90% of your time. It's simpler
and easier to code w/o threads initially.

If you want to crunch data and respond to user input at the same
time, why not start with the assumption that it might just be possible
to do that w/o threads? A lot of videogames do just that. Write your
main loop like this:

bool quitNow = false;
while(!quitNow)
{
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
quitNow = true;
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

// Crunch a bit of data. In a videogame, we'd do something
// like (1) Get inputs, (2) Update everything (3) draw scene

Think();
}

Just write Think() so that it takes at most a half second per call,
and you'll do fine. When your app isn't busy crunching data, then have
Think() call Sleep(0) to be nice to other apps. Until you really
understand threads, I'd advise not using them.

Nathan Mates
--
<*Nathan Mates - personal webpage http://www.visi.com/~nathan/
# Programmer at Pandemic Studios -- http://www.pandemicstudios.com/
# NOT speaking for Pandemic Studios. "Care not what the neighbors
# think. What are the facts, and to how many decimal places?" -R.A. Heinlein
Jan 18 '07 #2
Thanks. Of course the simplest solution is the best. I just needed
somebody who knows what they're doing to tell me that.

I haven't even worked with message handlers before, but I know I have
documentation on those.

Thanks again, and good luck with Mercenaries 2.

-Cory

Jan 18 '07 #3
co*********@sbcglobal.net wrote:
Hi. I have what I believe to be a very simple task I'm trying to
accomplish in VC++ 2005 and I simply can't make it work. I have a good
working knowledge of C, a basic-to-middling knowledge of C++, and a
novice-level knowledge of OOP in general and Visual Studio in
particular.

I am not a full-time programmer. I am an aerospace engineer who is
trying to make a useful tool. Remember this.

Anyway, the task breaks down to this. I've made a GUI for one of my
simulator codes. It reads in data from fields and runs the simulator
from that; that all works fine. Here's the problem: I have a Kill
button on the main UI that I need use of while my sim code is running.
The obvious answer would be to put the sim execution in another thread,
leaving the first thread to handle the UI. Problem: There is a
ProgressBar on the UI that needs to update as the simulator moves
through its allotted time. I need this so that the user will know when
the sim is hanging so that s/he can hit the Kill button.

Secondary problem: I also have a message bar that I'd like to update
with a "Simulation finished" message when the case has completed
successfully. This means I need some way for the first thread to know
when the second thread has returned.

I have Googled this subject to death, in these groups, in MSDN, and on
the rest of the Web. I have an 1,100-page book specifically dealing
with VC++ 2005 that says NOTHING, about thread programming. The online
articles I have encountered have either been so simple as to be
worthless in this application, or so complicated as to lose me two
paragraphs in.

This CANNOT, simply CANNOT be a hard thing to do, even for an amateur
programmer, this should be only a step or two beyond a message box with
a "Hello World" and a button. I know there are at least four different
ways to launch threads in a VC++ program: native C++, MFC calls, CLI
Thread classes, and CLI ThreadPool classes. I've tried the last two,
and I'm currently using the last one because it enabled me to actually
pass an argument to it, albeit through a structure that I had to make
specifically for it.
Cory:

The way I do this (not universally approved, but it works for me) is put
the work in a worker thread, and report progress back to the main GUI
thread using SendMessage(). The handler updates the progress bar and
returns 0 to stop and 1 to continue (this way you can implement a Cancel
button in the main thread).

When the thread ends I use PostMessage() back to the main thread
immediately before the worker thread function returns. The handler can
then use WaitForSingleObject() on the thread handle (which should return
almost immediately). Now the main thread knows that the worker thread
has finished. If you want, you can call GetExitCodeThread() on the
thread handle to get thereturn code of the therad function.

David Wilkinson

Jan 19 '07 #4
co*********@sbcglobal.net wrote:
Hi. I have what I believe to be a very simple task I'm trying to
accomplish in VC++ 2005 and I simply can't make it work. I have a good
working knowledge of C, a basic-to-middling knowledge of C++, and a
novice-level knowledge of OOP in general and Visual Studio in
particular.

I am not a full-time programmer. I am an aerospace engineer who is
trying to make a useful tool. Remember this.

Anyway, the task breaks down to this. I've made a GUI for one of my
simulator codes. It reads in data from fields and runs the simulator
from that; that all works fine. Here's the problem: I have a Kill
button on the main UI that I need use of while my sim code is running.
The obvious answer would be to put the sim execution in another thread,
leaving the first thread to handle the UI. Problem: There is a
ProgressBar on the UI that needs to update as the simulator moves
through its allotted time. I need this so that the user will know when
the sim is hanging so that s/he can hit the Kill button.

The thread has to check for this:

while(NotDone && !Abort)
{
// compute something
}

The main thread will be able to kill all other threads by:

Abort = TRUE;
>
Secondary problem: I also have a message bar that I'd like to update
with a "Simulation finished" message when the case has completed
successfully. This means I need some way for the first thread to know
when the second thread has returned.
Non-UI thread cannot update UI. Generally main thread is also UI thread,
which most of the time pumps messages. When this thread does some longer
task, UI stops working.

You can :

.. send particular message from finished thread, so the message will call
a handler in a context of main thread

.. periodically check completion status from main thread by variable

.. periodically call WaitForSingleObject() with zero timeout to see if
thread has finished
>
I have Googled this subject to death, in these groups, in MSDN, and on
the rest of the Web. I have an 1,100-page book specifically dealing
with VC++ 2005 that says NOTHING, about thread programming. The online
articles I have encountered have either been so simple as to be
worthless in this application, or so complicated as to lose me two
paragraphs in.

This CANNOT, simply CANNOT be a hard thing to do, even for an amateur
programmer, this should be only a step or two beyond a message box with
a "Hello World" and a button. I know there are at least four different
ways to launch threads in a VC++ program: native C++, MFC calls, CLI
Thread classes, and CLI ThreadPool classes. I've tried the last two,
and I'm currently using the last one because it enabled me to actually
pass an argument to it, albeit through a structure that I had to make
specifically for it.

Can somebody please help me?
What I said above is for native Windows API.

You need thread synchronization and those are buzzwords you should Google.

Check on following objects

Event
Mutex
Semaphore
CriticalSection (my favourite)

Usefulness of the objects above overlaps and sometimes you can use one
or another. Event/Mutex/Semaphore work across process (by unique name),
but critical section works only within single process.

Critical Section uses hidden unnamed semaphore and optimizes access by
calling the semaphore API only when necessary, so it is generally way
faster than semaphore itself.

Good luck

Roman
Jan 19 '07 #5
Thanks, guys. I think I'll probably go with Nathan's solution for the
problem at hand, but I'm going to do a little self-study using the
other two responses, so that when I need to do this again I've got a
bigger toolbox available.

I really wasn't understanding how the Windows API was working because I
was taking the easy route with CLI stuff. You all pushed me back to
looking at that, and as a result I'm getting a much better feel for it.

-Cory

Jan 19 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Tatu Portin | last post by:
Have this kind of struct: typedef struct { char **user_comments; /* ... */ } vorbis_comment; /* prototype */ char * read_vorbis_string ( FILE *sc);
4
by: ultranet | last post by:
I have cruised around http://msdn.microsoft.com/visualc/ and the rest of the site, and i am not able to find a single C++ or VC++ certification exam that will be available after June 30, 2004. I...
2
by: Slawomir Lisznianski | last post by:
Do ref classes indeed support instantiation on the stack? Similarly, can native classes be instantiated on gc heap? When you try to have a ref class instance on the stack, you'll get: R :...
5
by: Adriano Coser | last post by:
Hello. I'm moving an application from VC 2003 to VC 2005 Beta2. In mixed mode DLLs I need to set System::Threading::Thread::CurrentThread->ApartmentState =...
10
by: Adriano Coser | last post by:
Hello. I'm moving an application VC 2003 to VC 2005 Beta2. I need to set STA ApartmentState model so the drag & drop registration can work. I used to do...
2
by: Asfar | last post by:
Hi, I am just starting of VC++ .Net 2005. Can someone suggest of any good books or any good websites for beginners. Thanks, -Asfar
2
by: gbook | last post by:
I'm trying to nest classes and access the variables in each one from the other. I mainly just want to access the outer class from the inner class. Here is the code I have so far... It compiles, but...
4
by: CPettsson | last post by:
Hello all I've been trying for the last two days to install Boost to Visual C++ 2005 Express Edition. I followed the instructions on the site (http://www.boost.org/more/getting_started.html),...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.