473,842 Members | 1,419 Online
Bytes | Software Development & Data Engineering Community
+ 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 1909
In article <11************ **********@q2g2 000cwa.googlegr oups.com>,
<co*********@sb cglobal.netwrot e:
>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 nondeterministi c, 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(&m sg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
quitNow = true;
else
{
TranslateMessag e(&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*********@sbc global.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 WaitForSingleOb ject() 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 GetExitCodeThre ad() on the
thread handle to get thereturn code of the therad function.

David Wilkinson

Jan 19 '07 #4
co*********@sbc global.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 WaitForSingleOb ject() 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
1926
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
2306
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 emailed support, and the reply was: "We understand your concerns in this matter. We would like to inform you that at this moment we do not have any plan or information if there will be replacements for the discontinued exams." Does anybody...
2
1271
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 : cannot use this type here without a top-level '^'. When you try to instantiate a native class using gcnew using Visual Studio 2005 Beta 1, you'll get:
5
1644
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 = System::Threading::ApartmentState::STA; so the drag and drop registration can work. I used to do this in DllMain and it worked fine. But now I can't call managed code from this point. I understand that I must provide a module
10
2599
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 System::Threading::Thread::CurrentThread->ApartmentState = System::Threading::ApartmentState::STA; as the first statment of _tWinMain and also in the DllMain of my mixed mode assemblies.
2
1279
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
1871
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 I can't declare an instance of the inner class inside the outer class. class ImageData { .... ... .... class LoadFile;
4
4957
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), other helps I found by google:ing and reading around in newsgroups. I think I'm doing it the right way, but it still won't work completely. All the headers seem to install properly, but only some libs. For instance, the date_time works, I can compile...
0
9876
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9717
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10950
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7862
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5699
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5886
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4506
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 we have to send another system
2
4096
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3148
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.