When closing an executed application, my program is in background

S

sewid

Hi there!

I've got a problem with no solution, I hope you might help me.

I am writing a small tool with many buttons. Every button starts a
thread and this thread starts something else, in the example posted
below, the hardware configuration dialog. My problem is, that
sometimes when I close the executed program / application / dialog, my
program is displayed in background. When I execute the taskmanager,
everything works fine, but when I execute e.g. the hardware
configuration dialog, this behaviour occurs. Can somebody tell me why
and how to avoid this? (I don't want a topMost-application).

Using the shell()-command leads to the same behaviour.

Thanks in advance, best regards,
Sebastian

The button-click-action is:

Private Sub butSettingsHardware_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
butSettingsHardware.Click
Dim functionName As String = "butSettingsHardware_Click"
Try
Dim myThread As New Thread(AddressOf
threadOpenSettingsHardware)
myThread.Start()
Catch ex As Exception
exceptionLogging(ex, className, functionName)
End Try
End Sub

The started thread is:
''' <summary>
''' Öffnet die Hardware-Einstellungen
''' </summary>
''' <remarks></remarks>
Private Sub threadOpenSettingsHardware()
Dim functionName As String = "threadOpenSettingsHardware"
Try
Dim programName As String = "Display-Einstellungen"
Dim program As String = "rundll32.exe"
Dim arguments As String = "shell32.dll,Control_RunDLL
sysdm.cpl,,2"

executeProgram(programName, program, arguments)
Catch ex As Exception
exceptionLogging(ex, className, functionName)
End Try
End Sub

The executeProgram-funtion is:

''' <summary>
''' Führt ein Programm aus und gibt Rückmeldung, ob die Ausführung
erfolgreich war.
''' </summary>
''' <param name="programName">Programmname (für das Log)</param>
''' <param name="program">Programm mit Pfad</param>
''' <param name="arguments">Argumente / Parameter</param>
''' <param name="visible">true oder false, je nachdem ob das
Programm sichbar sein soll</param>
''' <param name="noWait">true oder false, je nachdem ob auf das
Programm gewartet werden soll</param>
''' <param name="skipAvailabilityCheck">true oder false, je
nachdem, ob die Verfügbarkeit des Programms überprüft werden soll</
param>
''' <param name="redirectFilename">Dateiname, wohin die Ausgabe
umgeleitet werden soll. Wenn leer, dann wird die Ausgabe nicht
umgeleitet.</param>
''' <returns>true oder false, je nachdem ob das Programm gestartet
werden konnte</returns>
''' <remarks></remarks>
Public Function executeProgram(ByVal programName As String, ByVal
program As String, Optional ByVal arguments As String = "", Optional
ByVal visible As Boolean = True, Optional ByVal noWait As Boolean =
False, Optional ByVal skipAvailabilityCheck As Boolean = False,
Optional ByVal redirectFilename As String = "") As Boolean
Dim functionName As String = "executeProgram"
Try
If Not skipAvailabilityCheck Then
If Not File.Exists(program) Then
log("Die Datei: '" & program & "' existiert
nicht", )
Dim myException As Exception =
Library.newException(className, functionName, "Die Datei: '" & program
& "' existiert nicht")
Throw myException
End If
End If

Dim theProcessStartInfo As New ProcessStartInfo(program,
arguments)

If visible = True Then
theProcessStartInfo.CreateNoWindow = False
theProcessStartInfo.WindowStyle =
ProcessWindowStyle.Normal
Else
theProcessStartInfo.CreateNoWindow = True
theProcessStartInfo.WindowStyle =
ProcessWindowStyle.Hidden
End If

If redirectFilename <> "" Then
theProcessStartInfo.RedirectStandardOutput = True
theProcessStartInfo.UseShellExecute = False
End If

log("Starte '" & programName & "'", )
If arguments <> "" Then
log("Aufruf: '" & program & " " & arguments & "'", )
Else
log("Aufruf: '" & program & "'", )
End If
Dim myProcess As New Process
myProcess.StartInfo = theProcessStartInfo
myProcess.Start()
log(programName & " gestartet", )

If redirectFilename = "" Then
If noWait Then
log("Auf die Beendigung von " & programName & "
wird nicht gewartet", )
Else
log(programName & " geschlossen", )
End If
End If


If redirectFilename <> "" Then
Dim myFileInfo As New FileInfo(redirectFilename)
If Not Directory.Exists(myFileInfo.DirectoryName) Then

Directory.CreateDirectory(myFileInfo.DirectoryName)
End If

Dim output As String =
myProcess.StandardOutput.ReadToEnd
output = output.Replace(vbLf, "")
output = output.Replace(vbCr, vbNewLine)

Dim myWriter As New StreamWriter(redirectFilename,
False)
myWriter.Write(output)
myWriter.Close()

log(programName & " geschlossen", )
End If

Return True

Catch ex As Exception
exceptionLogging(ex, className, functionName)
Dim myException As Exception =
Library.newException(className, functionName, ex.Message)
Throw myException
End Try
End Function
 
K

kimiraikkonen

Hi there!

I've got a problem with no solution, I hope you might help me.

I am writing a small tool with many buttons. Every button starts a
thread and this thread starts something else, in the example posted
below, the hardware configuration dialog. My problem is, that
sometimes when I close the executed program / application / dialog, my
program is displayed in background. When I execute the taskmanager,
everything works fine, but when I execute e.g. the hardware
configuration dialog, this behaviour occurs. Can somebody tell me why
and how to avoid this? (I don't want a topMost-application).

Using the shell()-command leads to the same behaviour.

Thanks in advance, best regards,
Sebastian

The button-click-action is:

Private Sub butSettingsHardware_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
butSettingsHardware.Click
Dim functionName As String = "butSettingsHardware_Click"
Try
Dim myThread As New Thread(AddressOf
threadOpenSettingsHardware)
myThread.Start()
Catch ex As Exception
exceptionLogging(ex, className, functionName)
End Try
End Sub

The started thread is:
''' <summary>
''' Öffnet die Hardware-Einstellungen
''' </summary>
''' <remarks></remarks>
Private Sub threadOpenSettingsHardware()
Dim functionName As String = "threadOpenSettingsHardware"
Try
Dim programName As String = "Display-Einstellungen"
Dim program As String = "rundll32.exe"
Dim arguments As String = "shell32.dll,Control_RunDLL
sysdm.cpl,,2"

executeProgram(programName, program, arguments)
Catch ex As Exception
exceptionLogging(ex, className, functionName)
End Try
End Sub

The executeProgram-funtion is:

''' <summary>
''' Führt ein Programm aus und gibt Rückmeldung, ob die Ausführung
erfolgreich war.
''' </summary>
''' <param name="programName">Programmname (für das Log)</param>
''' <param name="program">Programm mit Pfad</param>
''' <param name="arguments">Argumente / Parameter</param>
''' <param name="visible">true oder false, je nachdem ob das
Programm sichbar sein soll</param>
''' <param name="noWait">true oder false, je nachdem ob auf das
Programm gewartet werden soll</param>
''' <param name="skipAvailabilityCheck">true oder false, je
nachdem, ob die Verfügbarkeit des Programms überprüft werden soll</
param>
''' <param name="redirectFilename">Dateiname, wohin die Ausgabe
umgeleitet werden soll. Wenn leer, dann wird die Ausgabe nicht
umgeleitet.</param>
''' <returns>true oder false, je nachdem ob das Programm gestartet
werden konnte</returns>
''' <remarks></remarks>
Public Function executeProgram(ByVal programName As String, ByVal
program As String, Optional ByVal arguments As String = "", Optional
ByVal visible As Boolean = True, Optional ByVal noWait As Boolean =
False, Optional ByVal skipAvailabilityCheck As Boolean = False,
Optional ByVal redirectFilename As String = "") As Boolean
Dim functionName As String = "executeProgram"
Try
If Not skipAvailabilityCheck Then
If Not File.Exists(program) Then
log("Die Datei: '" & program & "' existiert
nicht", )
Dim myException As Exception =
Library.newException(className, functionName, "Die Datei: '" & program
& "' existiert nicht")
Throw myException
End If
End If

Dim theProcessStartInfo As New ProcessStartInfo(program,
arguments)

If visible = True Then
theProcessStartInfo.CreateNoWindow = False
theProcessStartInfo.WindowStyle =
ProcessWindowStyle.Normal
Else
theProcessStartInfo.CreateNoWindow = True
theProcessStartInfo.WindowStyle =
ProcessWindowStyle.Hidden
End If

If redirectFilename <> "" Then
theProcessStartInfo.RedirectStandardOutput = True
theProcessStartInfo.UseShellExecute = False
End If

log("Starte '" & programName & "'", )
If arguments <> "" Then
log("Aufruf: '" & program & " " & arguments & "'", )
Else
log("Aufruf: '" & program & "'", )
End If
Dim myProcess As New Process
myProcess.StartInfo = theProcessStartInfo
myProcess.Start()
log(programName & " gestartet", )

If redirectFilename = "" Then
If noWait Then
log("Auf die Beendigung von " & programName & "
wird nicht gewartet", )
Else
log(programName & " geschlossen", )
End If
End If

If redirectFilename <> "" Then
Dim myFileInfo As New FileInfo(redirectFilename)
If Not Directory.Exists(myFileInfo.DirectoryName) Then

Directory.CreateDirectory(myFileInfo.DirectoryName)
End If

Dim output As String =
myProcess.StandardOutput.ReadToEnd
output = output.Replace(vbLf, "")
output = output.Replace(vbCr, vbNewLine)

Dim myWriter As New StreamWriter(redirectFilename,
False)
myWriter.Write(output)
myWriter.Close()

log(programName & " geschlossen", )
End If

Return True

Catch ex As Exception
exceptionLogging(ex, className, functionName)
Dim myException As Exception =
Library.newException(className, functionName, ex.Message)
Throw myException
End Try
End Function

In your code, one thing i paid attention which may be the reason:

If visible = True Then
theProcessStartInfo.CreateNoWindow = False
theProcessStartInfo.WindowStyle =
ProcessWindowStyle.Normal
Else
theProcessStartInfo.CreateNoWindow = True
theProcessStartInfo.WindowStyle =
ProcessWindowStyle.Hidden

' Here you hide the app's window's style maybe it's still running but
in hidden window state, so i recommend to change it to "normal" if you
reproduce the same issue.

End If

Meanwhile, i was wondering similiar thing to this which forced me to
think why an app is still running on background althoýugh it seems
off, then i awared of the forms were "hidden" by "me.hide", after
closing it gone away from background ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top