|
分類:[VB.NET/VB2005 以降]
いつも参考にさせてもらっています。またよろしくお願いします
WindowService内OnShutDown時に一定の処理を実行させてたいのですが(例えば5分)
およそ2分でシャットダウン処理が終わってしまいます
ログにも"シャットダウン完了"が記録されません
OnStopは正常に動作します
何か考えられる原因&解決策はないでしょうか?
試したこと
・CanShutDownをTrueに設定
・レジストリ
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\WaitToKillServiceTimeoutを360000に設定
・下記修正プログラム適用(実運用環境ではインストールできず)
http://support.microsoft.com/kb/2549760/ja
環境(どちらも動作しません)
・テスト環境
Windows7SP1 FrameWork2.0
・実運用環境
WindowsServer2008Sp1 FrameWork2.0
サンプルのテストプログラム
Public Class ServiceTest
Const RunningTime As Integer = 5 * 60 * 1000 '最大で一時間くらい
Const CheckTime As Integer = 4 * 1000
Const WaitTime As Integer = 5 * 1000
Private Running As Boolean
Protected Overrides Sub OnStart(ByVal args() As String)
PutLog("サービス開始")
End Sub
Protected Overrides Sub OnStop()
PutLog("サービスストップ開始")
ExecuteWait()
While Running
Me.RequestAdditionalTime(CheckTime)
System.Threading.Thread.Sleep(WaitTime)
End While
PutLog("サービスストップ完了")
End Sub
Protected Overrides Sub OnShutdown()
PutLog("シャットダウン開始")
ExecuteWait()
While Running
System.Threading.Thread.Sleep(WaitTime)
End While
PutLog("シャットダウン完了")
MyBase.OnShutdown()
End Sub
Private Delegate Sub AsyncMethodCaller(<Runtime.InteropServices.Out()> ByRef ThreadId As Integer)
Private Sub CallbackMethod(ByVal ar As IAsyncResult)
Dim Result As Runtime.Remoting.Messaging.AsyncResult = CType(ar, Runtime.Remoting.Messaging.AsyncResult)
Dim Caller As AsyncMethodCaller = CType(Result.AsyncDelegate, AsyncMethodCaller)
Dim ThreadId As Integer = 0
Caller.EndInvoke(ThreadId, ar)
End Sub
Private Sub ExecuteWait()
Running = True
Dim Caller As New AsyncMethodCaller(AddressOf WaitAsync)
Dim ThreadId As Integer = 0
Dim Result As IAsyncResult = Caller.BeginInvoke(ThreadId, AddressOf CallbackMethod, Nothing)
End Sub
Private Sub WaitAsync()
System.Threading.Thread.Sleep(RunningTime)
Running = False
End Sub
Public Overloads Sub PutLog(ByVal ParamArray MessageList() As String)
'ログ出力
Try
If MessageList IsNot Nothing Then
Dim LogFileName As String = My.Application.Info.DirectoryPath + "\" + ServiceName + Today.ToString("yyyyMMdd") + ".log"
Using File As New IO.FileStream(LogFileName, IO.FileMode.Append, IO.FileAccess.Write, IO.FileShare.Read)
Using LogStream As New System.IO.StreamWriter(File, System.Text.Encoding.GetEncoding("Shift-Jis"))
Dim LogValue As String = Now.ToString("yyyy/MM/dd HH:mm:ss.fff")
For Each Message As String In MessageList
LogValue += ",""" + Message.Replace("""", """""") + """"
Next
LogStream.WriteLine(LogValue)
LogStream.Flush()
End Using
End Using
End If
Catch ex As Exception
End Try
End Sub
End Class
|