掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
タイマーイベントから定義したイベントが呼べない。 (ID:87931)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
こんな感じで。 ●Form1 タイマーコントロール Timer1 を貼り付けておく Option Explicit Private WithEvents cls1 As Class1 Private Sub cls1_ExecFinish() Debug.Print "finish" End Sub Private Sub Command1_Click() If cls1.startRequest(5) = 0 Then Debug.Print "start" Else Debug.Print "cancel" End If End Sub Private Sub Form_Load() Set cls1 = New Class1 Call cls1.setTimer(Timer1) End Sub ●Class1 Option Explicit Private WithEvents myTimer As Timer Private methodBusy As Boolean Private execStatus As Integer Private endTime As Date Private TimerFlag As Boolean Public Event ExecFinish() 'TimerがNew で生成できないのでフォームに張り付いた物を流用させてもらう Public Sub setTimer(newTimer As Timer) Set myTimer = newTimer End Sub Public Function startRequest(ByVal waitSec As Double) As Integer If myTimer Is Nothing Then startRequest = -2 'タイマーが準備できていない ElseIf methodBusy Then startRequest = -1 '現在実行中 Else methodBusy = True execStatus = 0 endTime = DateAdd("s", waitSec, Now) '終了予定時間 TimerFlag = False myTimer.Interval = 100 myTimer.Enabled = True startRequest = 0 End If End Function Private Sub Class_Initialize() methodBusy = False TimerFlag = False Set myTimer = Nothing End Sub Private Sub myTimer_Timer() If TimerFlag Then Exit Sub myTimer.Enabled = False TimerFlag = True Call TaskMain If methodBusy Then 'まだやるべき事が残っているのでタイマーを再度有効にする。 TimerFlag = False myTimer.Enabled = True Else '処理終了 RaiseEvent ExecFinish 'タイマーは止めたままにする End If End Sub Private Sub TaskMain() Debug.Print "taskMain" If methodBusy Then '指定時間を過ぎたら終了 methodBusy = (Now < endTime) End If End Sub
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2020 Takeshi Okamoto All Rights Reserved.