Newsgroup: it.comp.lang.visual-basic
Titolo: Creazione di Thread

Ciao,
Vorrei creare tramite vb6 un nuovo thread all'interno del quale far eseguire
un ciclo di controllo , ma non so come
creare un nuovo thread e come settarlo in modo che funzioni in modo
indipendente della finestra principale
e che si chiuda al momento della chiusura della finestra stessa.
Grazie
P.S. Preferibilmwnti no OCX
Mello



Per creare un nuovo thread si utilizza l'API CreateThread(), ma penso
che in VB i thread NON si possono creare (visto che l'API richiede
l'indirizzo di una funzione che non può essere ottenuta mediante la
clausola AddressOf di VB)...
in ogni caso, se vuoi provare, mandami una mail in privato che ti
spiego come si usa l'API.
Ciao
Marco Frazzoli

La CreateThread si puó usare benissimo con VB, il problema sta nel fatto che
M$ ha modificato la struttura interna di VB6 quindi tutti i programmi VB5
che usavano CreateThread con Vb6 non funzionano piú.
per ulteriori info... Q198607
L'unico modo per creare thread con Vb6 é quello di utilizzare l'Apartment
thread disponibile quando crei componenti ActiveX.
Ciao,
Corrado

La CreateThread si puó usare benissimo con VB, il problema sta nel fatto che
M$ ha modificato la struttura interna di VB6 quindi tutti i programmi VB5
che usavano CreateThread con Vb6 non funzionano piú.
per ulteriori info... Q198607
L'unico modo per creare thread con Vb6 é quello di utilizzare l'Apartment
threading disponibile quando crei componenti ActiveX.
Ciao,
Corrado

...


Ecco la KB incriminata Q198607
PRB: Access Violation in VB Run-Time Using AddressOf
The information in this article applies to:
Microsoft Visual Basic Learning, Professional, and Enterprise Editions for
Windows, version 6.0
SYMPTOMS
When running your Visual Basic application, you may encounter an error
similar to the following:
   The instruction at 0x660bd3b1 referenced memory at 0x0000009c. The
    memory could not be written.
This error may occur immediately following a declared API function call
within a Visual Basic callback function.
CAUSE
When the error occurs, the callback function is called by a thread that was
not created by Visual Basic.
For instance, certain API calls such as CreateThread() and
RegisterServiceCtrlHandler() will create an additional thread before
invoking the callback function that was previously setup by using the
AddressOf operator. Although the same code may have worked in previous
versions of Visual Basic, calling API's of this nature are currently
unsupported in Visual Basic.
RESOLUTION
If the crash is occurring at the specified instruction and address described
in the Symptoms section, then two possible work arounds may help:
Try running the application after compiling the project to P-Code.
-or-
Call the API functions through the use of a type library.
MORE INFORMATION
Steps to Reproduce Behavior
Create a New Standard EXE project in Visual Basic. Form1 is created by
default.
Add a CommandButton (Command1) to Form1.
Add a TextBox (Text1) to Form1.
Add the following code to Form1:
      Option Explicit
      Private Sub Command1_Click()
         Dim threadid As Long
         Dim hThread As Long
         hThread = CreateThread(0&, 2000, AddressOf ThreadProc, _
                                0&, 0&, threadid)
         If hThread = 0 Then
           Exit Sub
         End If
         CloseHandle hThread
      End Sub
Add a Module (Module1) to Project1 by selecting Add Module from the Project
menu.
Add the following code to Module1:
      Option Explicit
      Declare Function CreateThread Lib "kernel32"( _
         ByVal lpSecurityAttributes  As Long, _
         ByVal dwStackSize As Long, _
         ByVal lpStartAddress As Long, _
         ByVal lpParameter As Long, _
         ByVal dwCreationFlags As Long, _
         lpThreadId As Long) As Long
      Declare Function GetCurrentThreadId Lib "kernel32" () As Long
      Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) _
      As Long
      Public Function ThreadProc() As Long
         Dim hThread As Long
         hThread = GetCurrentThreadId()
         Form1.Text1 = hThread
         ThreadProc = hThread
      End Function
Compile Project1 and create Project1.exe.
Run the resulting Project1.exe file.
An error similar to the error described in the Symptoms section will be
encountered immediately following the API GetCurrentThreadId function call.
Furthermore, additional care must be taken with regards to what code can be
executed within the callback function. Any use of the following within the
callback function may cause undesirable results:
File I/O.
Error handling.
Fixed size arrays.
Set statements.
COM method calls that return HRESULTs (such as any Visual Basic ActiveX
object).
Declare calls.
Global objects such as the Application object.
Most of the Visual Basic run-time files.
REFERENCES
For additional information, please see the following articles in the
Microsoft Knowledge Base:
   ARTICLE-ID: Q189133
   TITLE     : Make C DLL More Accessible to VB with a Type Library
   ARTICLE-ID: Q143258
   TITLE     : How to Create Constants and DLL Declarations in a
               Type Library
Ciao,
Corrado

...


Non ho mai provato con VB6 la CreateThread (con VB5, ovviamente compilato
era tutto ok).
Se vuoi inviamelo pure che così lo provo
Ciao
Corrado

Ho provato il tuo codice e funziona con VB6 e Win98. Mi riservo di provarlo
con NT4.0 al più presto.
...non so che dire...
Ciao,
Corrado