私は、イベントを監視し、そのイベントで関連プログラムを実行するvb.netプログラムを持っています。タスクを監視し、予期せず停止した場合は再起動します。これはすべて機能しますが、プログラムをスマートにして、制御プログラムが停止して再起動したときに、既存のイベントの1つが実行されているときに、正しいタスクを見つけて監視を開始するようにしました。 私はProcess.GetProcessを実行し、MainModule.Filenameをループしました。これは.exeで動作しますが、.batファイルを起動するときはファイル名は "cmd.exe"です。バットファイルで実行中のプロセスを見つける方法
私が実行中のタスクと一致させるために使用できる他のプロパティは何ですか?
基本的なスタートタスク:コードの検索
Dim psi As New ProcessStartInfo()
psi.FileName = tmpProgramToExcute
psi.UseShellExecute = True
psi.Arguments = tmpCommandLineOptions
psi.Verb = "runas"
psi.WindowStyle = tmpWindowStyle
tmpTask = New Process
tmpTask.StartInfo = psi
tmpTask.Start()
:2016年9月8日12:39現地時間 を更新しました
If (tmpTask IsNot Nothing) AndAlso tmpTask.HasExited Then
tmpTask.Dispose()
tmpTask = Nothing
End If
Dim tmpProcess() As Process = Process.GetProcesses
For xLoop_tmpProcess As Integer = 0 To tmpProcess.Count - 1
Try
If tmpProcess(xLoop_tmpProcess).MainModule.FileName.ToLower = tmpProgramToExcute.ToLower Then
tmpTask = tmpProcess(xLoop_tmpProcess)
NewProcessingList(tmpKey).Task = tmpTask
Exit For
End If
Catch ex As Exception
End Try
Next
は、私は、戻って私の古いVB6のプログラムに行ってきました2003年に書かれたものです。私のブラックボックス機能の1つは、プログラムを開始することでした。これは、それは次のとおりです。
Function StartPrg(PrgName As String, Optional tmpText As String = "", Optional WindowState As WindowStyle = 4, Optional CmdPram As String = "") As Long
'0 hidden and focus is passed to the hidden window.
'1 Has focus and is restored to its original size and position.
'2 Is displayed as an icon with focus (minimized).
'3 Is maximized with focus.
'4 Is restored to its most recent size and position. No Focus.
'6 Is displayed as an icon. No Focus.
Dim tmpPID As Long
Dim xLoop As Long
Dim yLoop As Long
Dim PrgStarted As Boolean
'Log.It "Try to start " + PrgName
'tmpPID = Shell(PrgName, WindowState)
Dim Ret As Integer
Dim Bfr As String
Dim ExPath As String
Dim Ext As String
'If Dir(PrgName) = "" Or PrgName = "" Then
' StartPrg = -1
' Exit Function
'End If
If Ext = ".EXE" Or Ext = ".COM" Or Ext = ".BAT" Then
tmpPID = Shell(PrgName + " " + CmdPram, WindowState)
Else
Bfr = String(300, 32)
Ext = UCase(Right$(PrgName, 4))
Ret = FindExecutable(PrgName, vbNullString, Bfr)
If Ret > 32 Then
ExPath = Left$(Bfr, InStr(Bfr, Chr$(0)) - 1)
tmpPID = Shell(ExPath + " " + CmdPram, WindowState)
Else
StartPrg = -1
Exit Function
End If
End If
'Log.It PrgName + " started"
For xLoop = 1 To 10
DoEvents
If PrgStarted Then Exit For
For xSleepLoop = 1 To 100
PrgStarted = PrgExist(tmpPID)
If PrgStarted Then Exit For
Sleep 10
DoEvents
Next xSleepLoop
Next
If xLoop < 11 Then
MyPID = tmpPID
fEnumWindows
If Len(tmpText) > 0 Then
For xLoop = 1 To OnWin
If MyWinList(1, xLoop) = tmpPID Then
SendMessageA MyWinList(2, xLoop), WM_SETTEXT, 0&, tmpText
DoEvents
Sleep 10
End If
Next
End If
StartPrg = tmpPID
Else
StartPrg = -1
Exit Function
End If
End Function
だから私はvb.netでそれをreplcateしようとした:
Public Function ShellAndNotWait(ByVal sFilePath As String,
Optional ByVal sCommandLineOptions As String = "",
Optional ByRef sStdOut As Boolean = False,
Optional ByRef sStdError As Boolean = False,
Optional pWindowStyle As eShellWindowStyle = eShellWindowStyle.RecentSizeNoFocus,
Optional pWindowName As String = "") As Process
Dim psi As New ProcessStartInfo()
psi.FileName = sFilePath
psi.UseShellExecute = (Not (sFilePath.ToUpper.EndsWith(".EXE") Or sFilePath.ToUpper.EndsWith(".COM") Or sFilePath.ToUpper.EndsWith(".BAT")))
psi.Arguments = sCommandLineOptions
psi.RedirectStandardOutput = sStdOut
psi.RedirectStandardError = sStdError
psi.CreateNoWindow = sStdOut Or sStdError
Select Case pWindowStyle
Case eShellWindowStyle.HiddenFocus
psi.WindowStyle = ProcessWindowStyle.Hidden
Case eShellWindowStyle.IconFocus Or
eShellWindowStyle.IconNoFocus
psi.WindowStyle = ProcessWindowStyle.Minimized
Case eShellWindowStyle.RecentSizeFocus Or
eShellWindowStyle.RecentSizeNoFocus
psi.WindowStyle = ProcessWindowStyle.Normal
Case eShellWindowStyle.MaximizedFocus
psi.WindowStyle = ProcessWindowStyle.Maximized
End Select
Dim proc As New Process
proc.StartInfo = psi
proc.Start()
If proc Is Nothing Then Return Nothing
Sleep(100)
If pWindowName.Length > 0 Then SetWindowText(proc.MainWindowHandle, pWindowName)
If pWindowStyle = eShellWindowStyle.HiddenFocus OrElse
pWindowStyle = eShellWindowStyle.IconFocus OrElse
pWindowStyle = eShellWindowStyle.MaximizedFocus OrElse
pWindowStyle = eShellWindowStyle.RecentSizeFocus Then
AppActivate(proc.Id)
End If
Return proc
End Function
私はすべてのことは、作品のタイトルを設定することができます。その後、ユニークな限り、私は再びタスクを見つけることができ、私は最初からそれを開始したかのようにタスクを引き受けます。 私の問題は、VB6シェルには6つのモードがあり、.Netは起動しないということです。私はそれをエミュレートしようとしていましたが、最小化するためにstartinfoウィンドウスタイルを設定しても機能しません。あなたがcmd.exeを起動している場合のようにいくつかのGoogleの後、それは "ヒント"を無視します。多くの投稿がウィンドウを隠していたので、修正はCreateWindowをfalseに設定しました。まあ、私はウィンドウがほしい、ちょうどタスクバーに最小化する。誰かが何かシンプルなことを言うなら、私はこのスレッドに残します。また、私はいくつかのより多くのGoogleを行いますが、私はこれで何かを見つけることができない場合、私は新しいスレッドを開始します。 今のところ私はそれを開いたままにしているので、すべてが1か所にありますが、修正のためにウィンドウタイトルを表示したようです。
バッチファイルのウィンドウタイトルを設定していますか? – Squashman
バッチファイルをどのように起動しますか? 'start cmd/c%YourBatchFile%'のようなことをすることができます。これは、コマンドラインとして 'cmd/c%YourBatchFile%'を持つ新しい_cmd_プロセス(とウィンドウ)をバッチファイルで開始します。 – CristiFati
@Squashman、いいえ、それは考えです。私は開始プロパティとしてそれが表示されませんが、私は勝利のAPIを持っています。私はタスクのタグプロパティがあることを望んだ。私は他の提案のいくつかを見てみましょう、私は戻ってくるでしょう。 – penright