Question

I have an AutoIt script that uses a COM-object (created with VB6). The COM-object can fire events. I would like to use these events to display progress of a task in the AutoIt script, because the execution of the VB6 task can take up to 10 minutes. The problem is that the events are being fired, but that they are handled in AutoIt after the processing of the 'huge' task has ended. Below you can find a reduced version of the script. In the real-life version of the script a GUI is used and the progress percentage should not be written to the console, but to the inner text of a 'DIV' element.

Work()

Func Work()
    Local $g_oReport = ObjCreate("wcDashboardTools.DashBoardToolsVB6")

    If IsObj($g_oReport) Then
        $g_oReport.Server = "my-sql-server-name"

        Local $g_oReportEvents = ObjEvent($g_oReport,"ToolsEvent_") ; Start receiving Events.

        ; Test the events three times (the 'InitEvents' methods only raises the event in VB6).
        $g_oReport.InitEvents()
        Sleep(3000)

        $g_oReport.InitEvents()
        Sleep(3000)

        $g_oReport.InitEvents()

        ; In the test application this works and the event messages now have been written
        ; to the console. In the real life application, these 'test' messages will
        ; only be written to the console after the report results have been rendered.
        $g_oReport.Database             = "my-database-name"
        $g_oReport.FinancialYear        = 2013
        $g_oReport.FinancialPeriodStart = 1
        $g_oReport.FinancialPeriodEnd   = 1
        $g_oReport.ReportLayout         = "my-layout-name"

        ; Start the huge task.
        $g_oReport.CreateReport()

        ; At this point, the huge task has been completed.
        ConsoleWrite("Creation of the report finished, start rendering the report")

        If @Compiled = 0 Then $g_oReport.StoreReportAsXml("c:\temp\exploreport.xml")

        ; Start rendering.
        $sHTML = $g_oReport.GetReportAsHtmlString()
        ConsoleWrite($sHTML)
    Else
        ConsoleWrite("FOUT: Het genereren van de rapportage is mislukt.")
    EndIf
EndFunc

The 'CreateReport' method of the COM-object fires the event two times before actually starting the 'huge' task itself. However, the messages for these events only will be written to the console after the rendered HTML is written to the console.

Can anybody help me to make sure that the events are handled within the AutoIt script at the correct moment (during execution of the 'huge' task)?

Thanks in advance!

Björn

P.S. I made a reference implementation in C# which uses the same VB6 (Interop) and then the events are handled at the right moment.

Was it helpful?

Solution

It appears that this is a 'habbit' of the current live version (3.3.8.1) of AutoIt. The current beta-version (3.3.9.25) supports the 'volatile' keyword. When this keyword is used, the event handling function is called synchronous (according to the manual). Anyway, this solves the problem. Unfortunately, I cannot allow myself to use an unstable version of AutoIt, but my question has been solved (thanks to Trancexx on the AutoIt forum).

Volatile Func ToolsEvent_OnTaskProgressChange($taskProgress)

    ConsoleWrite('Progress(%): ' & $taskProgress & @CRLF)

EndFunc
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top