在AutoIt中延迟处理COM(VB6)事件

问题描述 投票:1回答:1

我有一个使用COM对象的AutoIt脚本(使用VB6创建)。 COM对象可以触发事件。我想使用这些事件在AutoIt脚本中显示任务的进度,因为执行VB6任务最多可能需要10分钟。问题是事件被触发,但是在“巨大”任务的处理结束后,它们在AutoIt中处理。您可以在下面找到该脚本的简化版本。在脚本的现实版本中,使用GUI并且不应将进度百分比写入控制台,而应写入“DIV”元素的内部文本。

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

在实际启动“巨大”任务本身之前,COM对象的“CreateReport”方法会触发事件两次。但是,只有在将呈现的HTML写入控制台后,才会将这些事件的消息写入控制台。

任何人都可以帮助我确保在正确的时刻(在执行“巨大”任务期间)在AutoIt脚本中处理事件吗?

提前致谢!

比约恩

附:我在C#中使用相同的VB6(Interop)进行了参考实现,然后在适当的时候处理事件。

events com autoit
1个回答
0
投票

看来这是AutoIt当前实时版本(3.3.8.1)的“habbit”。目前的beta版本(3.3.9.25)支持'volatile'关键字。使用此关键字时,事件处理函数称为同步(根据手册)。无论如何,这解决了这个问题。不幸的是,我不能允许自己使用不稳定版本的AutoIt,但我的问题已经解决了(感谢AutoIt论坛上的Trancexx)。

Volatile Func ToolsEvent_OnTaskProgressChange($taskProgress)

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

EndFunc
© www.soinside.com 2019 - 2024. All rights reserved.