multithreading

Heike

Regular
Joined
Jun 5, 2002
Messages
56
Simple said I have two programs. One is a EXE and the other is an addin for Word.

If the EXE is called, it starts word and uses functions etc. of the addin.

Maybe some code may help

This is the EXE
Visual Basic:
    '[...]
    Call mAddin.Object.DokumentDazu2(...)
    mAddin.Object.udtExternCalls.item(GetCurrentProcessID).objExternCall = CType(Me.objExternCall, ExternCall)

    'mAddin.Object.objThread.Join()

    blnAddinCallEnd = False
    blnAddinCallError = False
    blnAddinCallEnd = WaitForProcessCallEnd(blnAddinCallError)
    blnAddinCallEnd = True 'md070503

And this is the addin
Visual Basic:
Public Function DokumentDazu2(....) As String
    Dim strDatei As String
    Dim udtExternCall As ExternCall
    Try
        Me.blnNewExternCall(intID)
        udtExternCall = Me.udtExternCalls.Item(intID)
        If Not udtExternCall Is Nothing Then
            udtExternCall.blnInParameter = blnVorlageGegeben
            udtExternCall.strInParameter = strNewBlobKey
            udtExternCall.intInParameter = intDruckAnzahl
            System.Threading.Thread.CurrentThread.ApartmentState = System.Threading.ApartmentState.STA
            udtExternCall.objThread = New Threading.Thread(AddressOf DokumentDazuSTA)
            udtExternCall.objThread.ApartmentState = System.Threading.ApartmentState.STA
            udtExternCall.objThread.Name = CStr(intID)
            udtExternCall.objThread.Priority = ThreadPriority.AboveNormal
            udtExternCall.objThread.Start()
        End If
    Catch
        ErrorMessage(...)
    End Try

    Return strDatei
End Function

Public Sub DokumentDazuSTA()
    Dim strDatei As String
    Dim udtExternCall As ExternCall
    Dim strThreadName As String
    Try
        Call blnCheckApartmentState()
        strThreadName = System.Threading.Thread.CurrentThread.Name
        udtExternCall = Me.udtExternCalls.Item(CInt(strThreadName))
        objThread = System.Threading.Thread.CurrentThread
        If Not udtExternCall Is Nothing Then
            strDatei = mudtSchnittstelle.DokumentDazu(Me, udtExternCall.blnInParameter, udtExternCall.strInParameter, udtExternCall.intInParameter)
            udtExternCall.strOutParameter = strDatei
            udtExternCall.strOutParameter2 = udtExternCall.strInParameter
            udtExternCall.intIDEnd = CInt(strThreadName)
            udtExternCall.blnWordQuit = objIBS_INIT.udtExternCalls.blnWordQuit
            If Not udtExternCall.objExternCall Is Nothing Then
                udtExternCall.objExternCall.strOutParameter = udtExternCall.strOutParameter
                udtExternCall.objExternCall.strOutParameter2 = udtExternCall.strOutParameter2
                udtExternCall.objExternCall.blnWordQuit = udtExternCall.blnWordQuit
                udtExternCall.objExternCall.intIDEnd = udtExternCall.intIDEnd
            End If
        End If
        Me.udtExternCalls.Delete(CInt(strThreadName))
        DoEvent()
    Catch
        ErrorMessage(...)
    End Try
End Sub

It seems to work, but only on fast computers. I think waiting for the end of the thread started within the addin makes the whole program very slow. I tried a endless loop and thread.join to wait for the end; no difference.

Any idea? I read a lot of monitor and mutex etc. but nothing helps.
 
Back
Top