2013-12-20 41 views
1

我有一個使用COM對象(使用VB6創建)的AutoIt腳本。 COM對象可以觸發事件。我想使用這些事件來顯示AutoIt腳本中任務的進度,因爲執行VB6任務最多可能需要10分鐘。問題是這些事件正在被解僱,但是在'巨大'任務處理結束後,他們在AutoIt中處理。您可以在下面找到腳本的簡化版本。在腳本的真實版本中使用GUI,並且不應將進度百分比寫入控制檯,而應寫入「DIV」元素的內部文本。在AutoIt中處理COM(VB6)事件延遲

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腳本中處理事件嗎?

提前致謝!

Björn

P.S.我在C#中使用相同的VB6(Interop)做了一個參考實現,然後在適當的時候處理這​​些事件。

回答

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