Hi

Is it allowed to use multiple threads in a driver? I've a printer driver and
I try to start a second thread in a function... but it doesn't work...
everything blocks, if I try to do this... allthough I'm receiving a valid
handle to the thread ...

MR

Re: multithreading in drivers? by Gabriel

Gabriel
Tue Mar 22 06:04:19 CST 2005


"Meier Rudolf" <meiru@gmx.net> wrote in message
news:eMSDM5sLFHA.576@TK2MSFTNGP15.phx.gbl...
> Hi
>
> Is it allowed to use multiple threads in a driver? I've a printer driver
and
> I try to start a second thread in a function... but it doesn't work...
> everything blocks, if I try to do this... allthough I'm receiving a valid
> handle to the thread ...
>
> MR
>
>

As far as I know you can use theads in both kernel and user mode.
You are probably doing something wrong.



Re: multithreading in drivers? by Meier

Meier
Tue Mar 22 06:40:56 CST 2005

> As far as I know you can use theads in both kernel and user mode.

Sounds good...

> You are probably doing something wrong.

I really hope so...


here's my code... do you see the problem? -> it remains in the while loop
for ever, since dwExitCode remains STILL_ACTIVE... and when I set a
breakpoint in the "DoUpdate" function, it never stops there...


ThreadFunction, declared in class CServiceData

static DWORD WINAPI DoUpdate (LPVOID lpParameter);

defined like this

// Status: x
DWORD WINAPI CServiceData::DoUpdate(LPVOID lpParameter)
{
return 0;
}

that's the function that should create the thread... (in a printer driver ui
dll)

void doIt()
{
DWORD dwThreadID;
HANDLE hThread = ::CreateThread(NULL, 0, CServiceData::DoUpdate, NULL,
CREATE_SUSPENDED, &dwThreadID);
::SetThreadPriority(hThread, THREAD_PRIORITY_NORMAL); // nur zur
Sicherheit

if (hThread != NULL)
{
::ResumeThread(hThread);

DWORD dwExitCode;
::GetExitCodeThread(hThread, &dwExitCode);
while(dwExitCode == STILL_ACTIVE)
{
::Sleep(50);
::GetExitCodeThread(hThread, &dwExitCode);
}

CloseHandle(hThread);
}






Re: multithreading in drivers? by Meier

Meier
Tue Mar 22 06:56:51 CST 2005

ok... it's not a bug in my code... when I use the same class in a normal
console application, it works... so it has to do with the driver...

any ideas?

MR



Re: multithreading in drivers? by Gabriel

Gabriel
Tue Mar 22 07:09:46 CST 2005

Print drivers just as console application are user land code.
Just because it works in a console application it means nothing. A well
written code must work in all casses, a buggy one will work in some casses.

This is all you so here?

> DWORD WINAPI CServiceData::DoUpdate(LPVOID lpParameter)
> {
> return 0;
> }

Why do you want it to be created suspended?
Are you sure you need that?

> void doIt()
> {
> DWORD dwThreadID;
> HANDLE hThread = ::CreateThread(NULL, 0, CServiceData::DoUpdate, NULL,
> CREATE_SUSPENDED, &dwThreadID);

You don't need to do this, it's already set to normal.
> ::SetThreadPriority(hThread, THREAD_PRIORITY_NORMAL); // nur zur
> Sicherheit
>
> if (hThread != NULL)
> {
> ::ResumeThread(hThread);


If you want to wait for a thread to terminate you shoud use
WaitForSingleObject on the thread handle with a infinite timeout and non in
intterupitble mode.

> DWORD dwExitCode;
> ::GetExitCodeThread(hThread, &dwExitCode);
> while(dwExitCode == STILL_ACTIVE)
> {
> ::Sleep(50);
> ::GetExitCodeThread(hThread, &dwExitCode);
> }
>
> CloseHandle(hThread);
> }




Re: multithreading in drivers? by Meier

Meier
Tue Mar 22 07:15:22 CST 2005

> This is all you so here?

not really... at the end... but for testing it's enough


>> DWORD WINAPI CServiceData::DoUpdate(LPVOID lpParameter)
>> {
>> return 0;
>> }
>
> Why do you want it to be created suspended?
> Are you sure you need that?

no, I don't need this... but if I don't create it suspended it doesn't help
much... I thought, maybe it helps
to set another thread priority, befor starting it the first time... that's
why it's in there


>> void doIt()
>> {
>> DWORD dwThreadID;
>> HANDLE hThread = ::CreateThread(NULL, 0, CServiceData::DoUpdate, NULL,
>> CREATE_SUSPENDED, &dwThreadID);
>
> You don't need to do this, it's already set to normal.
>> ::SetThreadPriority(hThread, THREAD_PRIORITY_NORMAL); // nur zur
>> Sicherheit
>>
>> if (hThread != NULL)
>> {
>> ::ResumeThread(hThread);

yes, normally I use this... but like this, you can put in break-points, when
it's executed and hangs in this loop... if it hangs in the
"WaitForSingleObject", it's not that easy to get it out there...


> If you want to wait for a thread to terminate you shoud use
> WaitForSingleObject on the thread handle with a infinite timeout and non
> in
> intterupitble mode.
>
>> DWORD dwExitCode;
>> ::GetExitCodeThread(hThread, &dwExitCode);
>> while(dwExitCode == STILL_ACTIVE)
>> {
>> ::Sleep(50);
>> ::GetExitCodeThread(hThread, &dwExitCode);
>> }
>>
>> CloseHandle(hThread);
>> }


... and where's a bug now? :-(

MR



Re: multithreading in drivers? by Gabriel

Gabriel
Tue Mar 22 07:37:37 CST 2005

Put a breakpoint inside here and see if it enters and leaves the thread
function.

> >> DWORD WINAPI CServiceData::DoUpdate(LPVOID lpParameter)
> >> {
> >> return 0;
> >> }



Re: multithreading in drivers? by Meier

Meier
Tue Mar 22 07:45:15 CST 2005

I did this... it doesn't... I also tryed to insert a "ASSERT(FALSE);" to be
shure and a "int i = 0; int j = 1; int k = j / i;" ... and things like
that... but it never get's called...


"Gabriel Bogdan" <na@na.na> schrieb im Newsbeitrag
news:ecZnOQuLFHA.2420@TK2MSFTNGP12.phx.gbl...
> Put a breakpoint inside here and see if it enters and leaves the thread
> function.
>
>> >> DWORD WINAPI CServiceData::DoUpdate(LPVOID lpParameter)
>> >> {
>> >> return 0;
>> >> }
>
>



Re: multithreading in drivers? by Gabriel

Gabriel
Tue Mar 22 07:57:10 CST 2005

try to define the thread proc outside of a class, as a simple function like
this:
DWORD WINAPI ThreadProc(LPVOID lpParameter}{ __asm int 3; return
0;}also, don't make it suspended; use 0 for the creation flags.




"Meier Rudolf" <meiru@gmx.net> wrote in message
news:OUxwbVuLFHA.688@TK2MSFTNGP10.phx.gbl...
> I did this... it doesn't... I also tryed to insert a "ASSERT(FALSE);" to
be
> shure and a "int i = 0; int j = 1; int k = j / i;" ... and things like
> that... but it never get's called...
>
>
> "Gabriel Bogdan" <na@na.na> schrieb im Newsbeitrag
> news:ecZnOQuLFHA.2420@TK2MSFTNGP12.phx.gbl...
> > Put a breakpoint inside here and see if it enters and leaves the thread
> > function.
> >
> >> >> DWORD WINAPI CServiceData::DoUpdate(LPVOID lpParameter)
> >> >> {
> >> >> return 0;
> >> >> }
> >
> >
>
>



Re: multithreading in drivers? by Chris

Chris
Tue Mar 22 08:09:48 CST 2005

Your thread proc must be static.

ie:

static DWORD WINAPI CServiceData::DoUpdate(LPVOID lpParameter)
{
return 0;
}

Chris

"Meier Rudolf" <meiru@gmx.net> wrote in message
news:%234SsfxtLFHA.3500@TK2MSFTNGP14.phx.gbl...
>> As far as I know you can use theads in both kernel and user mode.
>
> Sounds good...
>
>> You are probably doing something wrong.
>
> I really hope so...
>
>
> here's my code... do you see the problem? -> it remains in the while loop
> for ever, since dwExitCode remains STILL_ACTIVE... and when I set a
> breakpoint in the "DoUpdate" function, it never stops there...
>
>
> ThreadFunction, declared in class CServiceData
>
> static DWORD WINAPI DoUpdate (LPVOID lpParameter);
>
> defined like this
>
> // Status: x
> DWORD WINAPI CServiceData::DoUpdate(LPVOID lpParameter)
> {
> return 0;
> }
>
> that's the function that should create the thread... (in a printer driver
> ui dll)
>
> void doIt()
> {
> DWORD dwThreadID;
> HANDLE hThread = ::CreateThread(NULL, 0, CServiceData::DoUpdate, NULL,
> CREATE_SUSPENDED, &dwThreadID);
> ::SetThreadPriority(hThread, THREAD_PRIORITY_NORMAL); // nur zur
> Sicherheit
>
> if (hThread != NULL)
> {
> ::ResumeThread(hThread);
>
> DWORD dwExitCode;
> ::GetExitCodeThread(hThread, &dwExitCode);
> while(dwExitCode == STILL_ACTIVE)
> {
> ::Sleep(50);
> ::GetExitCodeThread(hThread, &dwExitCode);
> }
>
> CloseHandle(hThread);
> }
>
>
>
>
>



Re: multithreading in drivers? by Meier

Meier
Tue Mar 22 08:18:25 CST 2005

still the same problem... I used exactely the code you gave me (but with the
function name tp instead of ThreadProc) ... but still the second thread
doesn't start....




"Gabriel Bogdan" <na@na.na> schrieb im Newsbeitrag
news:%233DvKbuLFHA.3296@TK2MSFTNGP15.phx.gbl...
> try to define the thread proc outside of a class, as a simple function
> like
> this:
> DWORD WINAPI ThreadProc(LPVOID lpParameter}{ __asm int 3; return
> 0;}also, don't make it suspended; use 0 for the creation flags.
>
>
>
>
> "Meier Rudolf" <meiru@gmx.net> wrote in message
> news:OUxwbVuLFHA.688@TK2MSFTNGP10.phx.gbl...
>> I did this... it doesn't... I also tryed to insert a "ASSERT(FALSE);" to
> be
>> shure and a "int i = 0; int j = 1; int k = j / i;" ... and things like
>> that... but it never get's called...
>>
>>
>> "Gabriel Bogdan" <na@na.na> schrieb im Newsbeitrag
>> news:ecZnOQuLFHA.2420@TK2MSFTNGP12.phx.gbl...
>> > Put a breakpoint inside here and see if it enters and leaves the thread
>> > function.
>> >
>> >> >> DWORD WINAPI CServiceData::DoUpdate(LPVOID lpParameter)
>> >> >> {
>> >> >> return 0;
>> >> >> }
>> >
>> >
>>
>>
>
>