#1  
Old 25th October 2019, 04:53 AM
Michaël HUNAULT
 
Posts: n/a
Default OT: MainThreadID / DLL

Hi guys,

We use a lot dlls in our application and we are facing a thing I can't
explain. May be somebody here could explain it.

In our app, we load a dll, say DllA. In this dllA the
System.MainThreadId is initialized to the ThreadID of the main app
thread (say =1) as expected (this is the thread who loaded the dll).

This dllA create a thread (say ThreadId=2) to do a specific job. This
thread, in his execute method, load another dll (say DllB, a utility
dll). So this dllB receive 2 as System.MainThreadID (the thread who
loaded the dll, expected).
This dllB launch a thread (say threadId=3) and wait for it. Thread 3
does is job with synchronization (Synchronize call) to his main thread
in dllA (threadId 2). All is fine and expected. After is job is done the
dllB is unloaded.

The thread from dllA (ThreadId=2) still continue executing and is
waiting for new job to do (waiting for an event, it does no more job
until the problem we encounter).

Now, while the thread in dllA is waiting for a newjob, in the meantime
the main thread in the app (ThreadId=1) load the same DllB do to another
specific job (remember it has been unloaded). Here, in dllB,
MainThreadId receive 2 (ie the threadId of dllA). I expect it to be 1
(ie the main thread app ID who loaded the dll this time) ???

Can someone explain me why the second load is attach to thread 2 instead
of thread 1 ?


To be complete, why it's a problem:

This dllB start a thread (say thread 4) and wait for it. Thread 4 start
his job and call Synchronize. But as the mainThreadID of the dll is not
1 but 2 (so mainthreadid 2 <> currentthreadid 1 when Waitfor is called),
instead of doing a loop waiting for the thread termination and check
synchronization (see WaitFor), the mainthread just wait for the thread 4
termination indefinitly (WaitForSingleObject) and is never waked up to
check synchronization, so the app hang up (thread 4 wait for
synchronization and mainthread wait for thread 4 to terminate).

FYI, we have thousands of customers, and only some get into this
problem. In my dev machine, the MainthreadId has always the correct
value I expect and all work fine.


(Hope it's clear enough.)


We'll rework these dlls, because in this particular case the
synchronization is not really needed and we'll probably change this to
load only one time these dlls, but I'll be happy if I can have an
explanation on the wrong value of MainThreadId.

Thanks in advance.

--
Michaël

  #2  
Old 25th October 2019, 09:20 PM
David Rose
 
Posts: n/a
Default Re: OT: MainThreadID / DLL

On 24/10/2019 18:53, Michaël HUNAULT wrote:

Is it always the same machines that have the problem ?
Could it be that on those machines something else (maybe anti-virus ??)
is stopping the dll from being unloaded ?

David

  #3  
Old 25th October 2019, 10:31 PM
Thorsten Engler [NDA]
 
Posts: n/a
Default Re: OT: MainThreadID / DLL

Something prevents your dllB from unloading. So it remains loaded and
MainThreadID remains assigned to the value it first got.

Michaël HUNAULT wrote:

> Hi guys,
>
> We use a lot dlls in our application and we are facing a thing I can't
> explain. May be somebody here could explain it.
>
> In our app, we load a dll, say DllA. In this dllA the
> System.MainThreadId is initialized to the ThreadID of the main app
> thread (say =1) as expected (this is the thread who loaded the dll).
>
> This dllA create a thread (say ThreadId=2) to do a specific job. This
> thread, in his execute method, load another dll (say DllB, a utility
> dll). So this dllB receive 2 as System.MainThreadID (the thread who
> loaded the dll, expected).
> This dllB launch a thread (say threadId=3) and wait for it. Thread 3
> does is job with synchronization (Synchronize call) to his main thread
> in dllA (threadId 2). All is fine and expected. After is job is done the
> dllB is unloaded.
>
> The thread from dllA (ThreadId=2) still continue executing and is
> waiting for new job to do (waiting for an event, it does no more job
> until the problem we encounter).
>
> Now, while the thread in dllA is waiting for a newjob, in the meantime
> the main thread in the app (ThreadId=1) load the same DllB do to another
> specific job (remember it has been unloaded). Here, in dllB,
> MainThreadId receive 2 (ie the threadId of dllA). I expect it to be 1
> (ie the main thread app ID who loaded the dll this time) ???
>
> Can someone explain me why the second load is attach to thread 2 instead
> of thread 1 ?
>
>
> To be complete, why it's a problem:
>
> This dllB start a thread (say thread 4) and wait for it. Thread 4 start
> his job and call Synchronize. But as the mainThreadID of the dll is not
> 1 but 2 (so mainthreadid 2 <> currentthreadid 1 when Waitfor is called),
> instead of doing a loop waiting for the thread termination and check
> synchronization (see WaitFor), the mainthread just wait for the thread 4
> termination indefinitly (WaitForSingleObject) and is never waked up to
> check synchronization, so the app hang up (thread 4 wait for
> synchronization and mainthread wait for thread 4 to terminate).
>
> FYI, we have thousands of customers, and only some get into this
> problem. In my dev machine, the MainthreadId has always the correct
> value I expect and all work fine.
>
>
> (Hope it's clear enough.)
>
>
> We'll rework these dlls, because in this particular case the
> synchronization is not really needed and we'll probably change this to
> load only one time these dlls, but I'll be happy if I can have an
> explanation on the wrong value of MainThreadId.
>
> Thanks in advance.




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT +11. The time now is 10:27 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.