Visualizzazione dei risultati da 1 a 7 su 7
  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2001
    Messaggi
    7

    [VB6] Thread su function con parametri?

    Ho questo tipo di problema/quesito:

    Riporto il codice:

    Modulo standard:

    Public Const CTF_COINIT = &H8
    Public Const CTF_INSIST = &H1
    Public Const CTF_PROCESS_REF = &H4
    Public Const CTF_THREAD_REF = &H2
    'Public stato As Integer
    Public Declare Function SHCreateThread Lib "shlwapi.dll" (ByVal pfnThreadProc As Long, pData As Any, ByVal dwFlags As Long, ByVal pfnCallback As Long) As Long

    Public Sub myThread(Index)


    Do While Not stopThread(Index)
    Form1.Text1(Index).Text = CLng(Form1.Text1(Index).Text) + 1
    Loop

    Form1.Text1(Index).Text = 0

    End Sub

    Nel Form:


    'problema: non mi accetta il passaggio del parametro Index in myThread, quindi
    'non riesco a passarlo
    SHCreateThread AddressOf myThread(Index), ByVal 0&, CTF_INSIST, ByVal 0&

    Avete una soluzione?

    Grazie.
    Rienzi

  2. #2
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    Il passaggio di argomenti avviene tramite il secondo parametro (pData).

    Attenzione pero' perche' la gestione di thread non e' supportata da VB6 e il risultato sara' sempre un crash.
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  3. #3
    Utente di HTML.it
    Registrato dal
    Jan 2001
    Messaggi
    7
    Ma, io l'ho provato e senza parametri e funziona bene. Purtroppo mi serve passargli almeno un paramentro...

    Come faccio a catturare il parametro che, come hai detto tu, passo in PData nella funzione myThread?
    E se devo passare più parametri?

    Grazie.
    Rienzi

  4. #4
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    Il parametro lo passi per indirizzo. Se ti serve passare piu' parametri, allora devi creare una struttura e passare la struttura per indirizzo ...

    P.S. Al primo crash all'interno del codice del thread, ti diro' ... "te l'avevo detto ..." ...
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  5. #5
    Utente di HTML.it
    Registrato dal
    Jan 2001
    Messaggi
    7
    Scusa se sono pesante...
    Mi puoi fare un esempio pratico del passaggio del parametro?

    P.S. Tu come risolveresti il problema dei thread?

    Ti spiego...
    Ho un ciclo Do Loop Until per copiare un file a blocchi di 1024.
    Ma.... devo dare all'utente la possibilità di interromperlo.
    Il problema è che se non creo un Thread separato per la sub che copia il file, la mia applicazione rimane praticamente bloccata fino a che la copia non è terminata!

    Come posso risolvere se non creando il thread e quindi non bloccando l'applicazione durante la copia del file??

    Grazie e grazie.
    Rienzi

  6. #6
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    Originariamente inviato da rienzi
    Scusa se sono pesante...
    Mi puoi fare un esempio pratico del passaggio del parametro?
    Guarda ... la ritengo una perdita di tempo ...

    VB6 usa il modello "apartment threading". Quella API usa (alla fine) la CreateThread e quindi un modello "free threading" che causa, inevitabilmente, un "crash" dovuto alla gestione TLS di VB6 ... (il discorso ci porterebbe molto fuori argomento ... per capirci, con VB6 non puoi usare i thread).

    P.S. Tu come risolveresti il problema dei thread?
    Mah ... essenzialmente lo farei con .NET (VB o C#) che non presenta il problema.

    Ti spiego...
    Molto probabilmente potresti eludere il problema inserendo nel ciclo di copia una

    DoEvents
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  7. #7
    Utente di HTML.it
    Registrato dal
    Jan 2001
    Messaggi
    7
    Credo di aver risolto aggirando il problema!

    Questo è il codice che credo sia del vostro sito:

    Attiva la copia classica del S.O. e quindi posso fare un annulla dalla popup, e lascia attivo quindi il processo padre sottostante, nel processo padre verifico solo con un timer il file e quindi gestisco la cosa verificando se il file è stato copiato per intero o no. Comunque grazie.
    Portare tutto su NET sarebbe praticamente impossibile, visto che l'applicativo in questione è complesso e pesa più di 400Kb compilato!

    'NOTE - These constants and some of the code is taken from
    'KB article Q151799...

    Option Explicit

    Private Const FO_COPY = &H2& 'Copies the files specified
    'in the pFrom member to the
    'location specified in the
    'pTo member.

    Private Const FO_DELETE = &H3& 'Deletes the files specified
    'in pFrom (pTo is ignored.)

    Private Const FO_MOVE = &H1& 'Moves the files specified
    'in pFrom to the location
    'specified in pTo.

    Private Const FO_RENAME = &H4& 'Renames the files
    'specified in pFrom.

    Private Const FOF_ALLOWUNDO = &H40& 'Preserve Undo information.
    Private Const FOF_CONFIRMMOUSE = &H2& 'Not currently implemented.
    Private Const FOF_CREATEPROGRESSDLG = &H0& 'handle to the parent
    'window for the
    'progress dialog box.

    Private Const FOF_FILESONLY = &H80& 'Perform the operation
    'on files only if a
    'wildcard file name
    '(*.*) is specified.

    Private Const FOF_MULTIDESTFILES = &H1& 'The pTo member
    'specifies multiple
    'destination files (one
    'for each source file)
    'rather than one
    'directory where all
    'source files are
    'to be deposited.

    Private Const FOF_NOCONFIRMATION = &H10& 'Respond with Yes to
    'All for any dialog box
    'that is displayed.

    Private Const FOF_NOCONFIRMMKDIR = &H200& 'Does not confirm the
    'creation of a new
    'directory if the
    'operation requires one
    'to be created.

    Private Const FOF_RENAMEONCOLLISION = &H8& 'Give the file being
    'operated on a new name
    'in a move, copy, or
    'rename operation if a
    'file with the target
    'name already exists.

    Private Const FOF_SILENT = &H4& 'Does not display a
    'progress dialog box.

    Private Const FOF_SIMPLEPROGRESS = &H100& 'Displays a progress
    'dialog box but does
    'not show the
    'file names.

    Private Const FOF_WANTMAPPINGHANDLE = &H20&
    'If FOF_RENAMEONCOLLISION is specified,
    'the hNameMappings member will be filled
    'in if any files were renamed.
    ' The SHFILOPSTRUCT is not double-word aligned. If no steps are
    ' taken, the last 3 variables will not be passed correctly. This
    ' has no impact unless the progress title needs to be changed.

    Private Type SHFILEOPSTRUCT
    hwnd As Long
    wFunc As Long
    pFrom As String
    pTo As String
    fFlags As Integer
    fAnyOperationsAborted As Long
    hNameMappings As Long
    lpszProgressTitle As String
    End Type

    Private Declare Sub CopyMemory Lib "KERNEL32" _
    Alias "RtlMoveMemory" (hpvDest As Any, _
    hpvSource As Any, ByVal cbCopy As Long)

    Private Declare Function SHFileOperation Lib "Shell32.dll" _
    Alias "SHFileOperationA" (lpFileOp As Any) As Long




    Private Sub cmdCopy_Click()
    Dim result As Long
    Dim lenFileop As Long
    Dim foBuf() As Byte
    Dim fileop As SHFILEOPSTRUCT

    lenFileop = LenB(fileop) ' double word alignment increase
    ReDim foBuf(1 To lenFileop) ' the size of the structure.

    With fileop

    .hwnd = Me.hwnd
    .wFunc = FO_COPY 'This means that the Function will be Copy
    'Look in the General Declerations for more
    'constants to use with the Copy Box...

    'BELOW: The files to be copied, you can use wild cards, or
    'just specify each file. Each file to be copied must be seperated
    'by vbNullChar and finally 2 of them at the end...
    '.pFrom = App.Path & "\readme.html" _
    ' & vbNullChar _
    ' & App.Path & "\readme.doc" _
    ' & vbNullChar _
    ' & App.Path & "\readme.txt" _
    ' & vbNullChar _
    ' & vbNullChar

    'F:\Streaming
    .pFrom = "F:\Streaming" & "\dabo suini.mov"

    .pTo = "F:\Streaming\output" & "\dabo suini.mov" 'Where the files will end up - you can specify a
    'directory and it will be created(use a flag{see below}
    'if you wish the directory to be created without asking...

    'BELOW: Which 'flags' you wish to use - these are defined by the
    'Constants in the General Declerations - you can hide the progress bar,
    'or do a few more things...
    .fFlags = FOF_CREATEPROGRESSDLG

    'Give the dialog box a title...
    .lpszProgressTitle = "VB HowTo Copy Example " & _
    vbNullChar _
    & vbNullChar
    End With

    'Call the CopyMemory procedure and copy the
    'structure into a byte array(??)
    Call CopyMemory(foBuf(1), fileop, lenFileop)
    Call CopyMemory(foBuf(19), foBuf(21), 12)

    'This is used for Error Handling - 0 means everything was
    'fine, any other number means their was an error...
    result = SHFileOperation(foBuf(1))

    'ERROR HANDLER....
    If result <> 0 Then ' Operation failed
    MsgBox Err.LastDllError 'Show the error returned from
    'the API.
    Else
    If fileop.fAnyOperationsAborted <> 0 Then
    MsgBox "Operation Failed"
    End If
    End If

    End Sub
    Rienzi

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.