Article View: borland.public.cppbuilder.nativeapi
Article #10986Multi Threaded object -- access to subthreads...
From: Jay McVinney
Date: Tue, 02 Mar 2004 21:45
Date: Tue, 02 Mar 2004 21:45
102 lines
4256 bytes
4256 bytes
OK,
Silly question I suppose, but I'm running into it and haven't before.
Normally I just throw a thread out there to execute and die off when it's
done. Now what I need is a thread that I can go back and talk to when I
need to.
MainProcess ->Creates thread A -> which creates thread B -> which creates a
new external process
The basic code would look like
----------------------------------------------
OnAnEvent()
{
TThreadA *ThreadA = new TThreadA(true); //create a suspended thread
ThreadA->ThreadName = ThreadName; //unique name for the Named Thread
ThreadA->Resume();
};
OnAnotherEvent()
{
//this is where I need to call the method from ThreadB
};
----------------------------------------------------
ThreadA
----------------------------------------------------
void __fastcall TThreadA::SomeFunction()
{
TThreadB *ThreadB = new ThreadB(true);
ThreadB->ThreadName = SomeName; //uniquely named thread
ThreadB->Resume();
}
--------------------------------------------------
ThreadB
--------------------------------------------------
void __fastcall TThreadB::Execute()
{
bool RunIt=true; //actually a __thread variable, just put it here so the
code made some sense
RunThread();
while (RunIt)
{
Sleep(1);
Application->ProcessMessages();
pid = pi.dwProcessId;
tid = pi.dwThreadId;
GetExitCodeProcess(pi.hProcess, &dwExitCode);
if(dwExitCode==0 && RunIt){
RunThread(); //restarts the process if it dies
};
};
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
//--------------------------------------------
void TThreadB::RunThread()
{
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line).
"notepad.exe", // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
"D:\\WinNT\\System32", // Use parent's starting
directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
//nothing started
}
}
//----------------------------------------------
void TThreadB::MethodThatIwantToCall()
{
//do something with the process here
};
So, I find all kinds of documentation on threading and calling a child
thread from a main thread, but I can't seem to find out how to pass that
handle from the intermediary thread back to the main thread. i.e. say that
I have 5 threads, each with a copy of notepad running and I want to send a
command to the third instance of notepad, how can I find the correct handle
to do so. It appears that this is not covered by TThreadList, which is what
I was trying to use for managing the thread pool. Note that the above
information has been scaled to make the idea of what I am trying to do much
less complicated. There are definitive reasons for the threads to be
created in this manner.
TIA
Jay McVinney
Technical Details
Message-ID:
<4045001f$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.tpinternet.pl!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,30c2c7d078fe1ea0,start X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2004-03-02 14:10:48 PST Reply-To: "Jay McVinney" <compforce@nospam.hotmail.com> From: "Jay McVinney" <compforce@hotmail.com> Newsgroups: borland.public.cppbuilder.nativeapi Subject: Multi Threaded object -- access to subthreads... Date: Tue, 2 Mar 2004 16:45:59 -0500 Lines: 104 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 NNTP-Posting-Host: 24.196.229.31 Message-ID: <4045001f$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1078263839 24.196.229.31 (2 Mar 2004 13:43:59 -0800) Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.tpinternet.pl!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail