Help with threads

M

matt

Hi All,

I'm trying to write a windows service by modifying an example I found
at http://www.thecodeproject.com

This is my first encounter with a multithreaded app so please forgive
this newbie question but I'm having a little bit of difficulty using
global variables inside the threads

The code basically looks like this

HANDLE killServiceEvent;
HANDLE hGinaThread, hTrayIconThread;
char * szPinNumber;

void main() {
DWORD idGina, idTrayIcon;

hGinaThread=CreateThread(0,0,(LPTHREAD_START_ROUTINE)GinaThread,0,0,&idGina);

hTrayIconThread=CreateThread(0,0,(LPTHREAD_START_ROUTINE)TrayIconThread,0,0,&idTrayIcon);
WaitForSingleObject(killServiceEvent,INFINITE);
CloseHandle(killServiceEvent);
}

DWORD GinaThread(LPDWORD param) {
//Code gets pin number from a mailslot
szPinNumber = szMailslotData;
return 0;
}

DWORD TrayIconThread(LPDWORD param) {
Sleep(120000);
MessageBox(NULL, szPinNumber, NULL, MB_OK);
return 0;
}

The program just crashes when it tries to display the MessageBox.

However if I change the code slightly by changing the declaration of
szPinNumber to char * szPinNumber = "PIN Number"; and comment out the
line in GinaThread that updates szPinNumber the code executes
flawlessly.

Any ideas?
 
I

Ian Collins

Hi All,

I'm trying to write a windows service by modifying an example I found
at http://www.thecodeproject.com

This is my first encounter with a multithreaded app so please forgive
this newbie question but I'm having a little bit of difficulty using
global variables inside the threads
Rather OT here, try a windows programming group, or
comp.programming.threads.
 
F

Frank Silvermann

Hi All,

I'm trying to write a windows service by modifying an example I found
at http://www.thecodeproject.com

This is my first encounter with a multithreaded app so please forgive
this newbie question but I'm having a little bit of difficulty using
global variables inside the threads

The code basically looks like this
hTrayIconThread=CreateThread(0,0,(LPTHREAD_START_ROUTINE)TrayIconThread,0,0,&idTrayIcon);
WaitForSingleObject(killServiceEvent,INFINITE);
CloseHandle(killServiceEvent);
One of the reasons that we wrangle so much about standard C around here
is so that we don't have to unravel statements like the above. Others
would tell you to take your question to a place where things best done
with MFC are topical, but indeed, that is where you already sniped this,
and to tell you to go to an MFC forum is like telling you to go farm in
the desert. Instead, I would say that when you get tired of
WaitingForBadIdeas(&MoreBadIdeas), give C a try. frank
 
S

santosh

Hi All,

I'm trying to write a windows service by modifying an example I found
at http://www.thecodeproject.com

This is my first encounter with a multithreaded app so please forgive
this newbie question but I'm having a little bit of difficulty using
global variables inside the threads

The code basically looks like this
.... snip Windows specific code ...
Any ideas?

Firstly, this group's topic is confined to ISO C, so your Win32 code is
not topical here. The good news though is that there are a large number
of Windows programming groups and your question will get a much better
response when posted there: An example is
comp.os.ms-windows.programmer.win32.

Secondly, threaded programming is rather involved. It is best to be
very thorough with plain C programming before tackling multi-threaded
programming. Additionally learn the topic from a good book instead of
disparate examples and untested snippets scattered across the net.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,520
Latest member
KrisMacono

Latest Threads

Top