-
Threading in VB - extra problem ??
looking at all the references I got, I understand now that program might crash
if we try to call a object from other thread without marshalling, it might
crash. however, consider the following :
I got a normal vb program, in the form load, I create a new thread, something
look like this,
handle = CreateThread(ByVal 0&, ByVal 0&, AddressOf Thread, 0&, ByVal 0&,
id)
in the thread function in a public module file, which should look like this,
Option Explicit
Declare Function InitPacketDriverNT Lib "vbLibDll.Dll" (Var As Long) As Long
Declare Function ReadPacketNT Lib "vbLibDll.Dll" (ByVal Sync As Long, ByRef
Bytes As Long) As Long
Declare Function CreateThread Lib "kernel32" (ByVal lpThreadAttributes As
Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter
As Any, ByVal dwCreationFlags As Long, lpThreadID As Long) As Long
Global ThreadControl As Long
Public Function Thread(Param As Long) As Long
Dim data As Long
InitPacketDriverNT 0
Do While ThreadControl = 0
ReadPacketNT 1, data
Loop
End Function
as we can see from this, the thread function never call any object, but a
normal win32 DLL function (compiled in VC++), but it failed !!! it shouldn't
be as the thread function never call other object from the main thread.
what's can be wrong ?? it's because the new thread can't call the function
? but the dll should be loaded within the process memory and accessible by
every thread.
"Matthew Curland" <mattcur@microsoft.com> wrote:
>Or you can look at my articles in June 99 VBPJ, or the threading material
in
>my book (http://www.PowerVB.com). This will tell you how to do it safely
>instead of explaining why VB6 can't handle it. Dan gives a good presentation
>on threading models and COM marshalling, but the conclusions (summarized
as
>'The only safe way to create background threads in a Dll in VB6 is to buy
>SpyWorks 6.2") are highly questionable, especially considering that the
6.2
>product shipped well after a robust pure VB6 solution to the problem had
>been published. -Matt
>
>"Sachin" <sachin.nigam@orbit-e.com> wrote in message
>news:3b2edc2f@news.devx.com...
>> check out Dan Appleman's article *A Thread to Visual Basic* on Desaware's
>> website
>> Sachin
>
>
>
-
Re: Threading in VB - extra problem ??
As Matt points out in another thread (in this NG) using declares causes VB
to attempt to use TLS.
>Global ThreadControl As Long
Assuming a typelib is used instead wouldn't there still be problem since
the above would be stored in the TLS also?
--
Anthony Jones
Nuesoft Ltd
-
Re: Threading in VB - extra problem ??
As Matt points out in another thread (in this NG) using declares causes VB
to attempt to use TLS.
>Global ThreadControl As Long
Assuming a typelib is used instead wouldn't there still be problem since
the above would be stored in the TLS also?
--
Anthony Jones
Nuesoft Ltd
-
Re: Threading in VB - extra problem ??
the ThreadControl variable might have different value for different thread
if it's store in TLS, but it shouldn't cause the program to crash, right
?
thanks.
"Anthony Jones" <yadayadayada@msn.com> wrote:
>As Matt points out in another thread (in this NG) using declares causes
VB
>to attempt to use TLS.
>
>>Global ThreadControl As Long
>
>Assuming a typelib is used instead wouldn't there still be problem since
>the above would be stored in the TLS also?
>
>--
>Anthony Jones
>Nuesoft Ltd
>
>
>
-
Re: Threading in VB - extra problem ??
the ThreadControl variable might have different value for different thread
if it's store in TLS, but it shouldn't cause the program to crash, right
?
thanks.
"Anthony Jones" <yadayadayada@msn.com> wrote:
>As Matt points out in another thread (in this NG) using declares causes
VB
>to attempt to use TLS.
>
>>Global ThreadControl As Long
>
>Assuming a typelib is used instead wouldn't there still be problem since
>the above would be stored in the TLS also?
>
>--
>Anthony Jones
>Nuesoft Ltd
>
>
>
-
Re: Threading in VB - extra problem ??
The TLS is not something that is always present and implied by the
existance of a thread. Rather it is means of isolating memory from other
co-operating threads. Just calling CreateThread does not setup TLS for the
thread. VB's own thread creation always configures TLS for each thread,
subsequently any VB code can depend on that fact.
--
Anthony Jones
Nuesoft Ltd
-
Re: Threading in VB - extra problem ??
The TLS is not something that is always present and implied by the
existance of a thread. Rather it is means of isolating memory from other
co-operating threads. Just calling CreateThread does not setup TLS for the
thread. VB's own thread creation always configures TLS for each thread,
subsequently any VB code can depend on that fact.
--
Anthony Jones
Nuesoft Ltd
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|