Process.Exited

  • VB.NET
  • .NET (FX) 4.5–4.8

Es gibt 11 Antworten in diesem Thema. Der letzte Beitrag () ist von Bluespide.

    Process.Exited

    Hallo zusammen,

    ich habe mal eine Frage bezüglich Process.Exited.

    Ich habe einen Prozess, welcher auf die rundll32.exe zugreift.

    VB.NET-Quellcode

    1. ​Private WithEvents Shell As New Process


    Der Aufruf klappt auch so wie gedacht.

    VB.NET-Quellcode

    1. With Shell
    2. .StartInfo.FileName = "rundll32.exe"
    3. .StartInfo.Arguments = "InetCpl.cpl,ClearMyTracksByProcess 255"
    4. .Start()
    5. End With


    Allerdings wird das Process.Exited Event nicht ausgeführt, wenn sich das Fenster schließt.

    VB.NET-Quellcode

    1. ​Private Sub Shell_Exited(sender As Object, e As EventArgs) Handles Shell.Exited
    2. btnRunSownloader.Enabled = True
    3. btnRunSownloader.Visible = True
    4. End Sub


    Wie kann ich den Code erst ausführen, wenn sich rundll32.exe beendet?

    LG,
    Marvin
    warum er das Event nicht feuert kann ich dir leider nicht sagen aber du könntest (gesetzt dem Fall es geht einfach darum etwas auszuführen sobald er fertig ist) auch folgendes machen :

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Dim t As New Process
    2. With t
    3. .StartInfo.FileName = "rundll32.exe"
    4. .StartInfo.Arguments = "InetCpl.cpl,ClearMyTracksByProcess 255"
    5. .Start()
    6. .WaitForExit()
    7. If .HasExited Then
    8. btnRunSownloader.Enabled = True
    9. btnRunSownloader.Visible = True
    10. End If
    11. End With


    Greets
    If Energy = Low Then
    Drink(aHugeCoffee)
    Else
    Drink(aHugeCoffeeToo)
    End If
    So funktioniert es zwar, allerdings muss ich es in einen anderen Thread ausführen, da ansonsten die Form solange einfriert, wie rundll32.exe aktiv ist.

    Ich würde es allerdings bevorzugen dies zu vermeiden.

    LG,
    Marvin
    Maybe... :

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Private WithEvents t As New Process
    2. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    3. With t
    4. .StartInfo.FileName = "rundll32.exe"
    5. .StartInfo.Arguments = "InetCpl.cpl,ClearMyTracksByProcess 255"
    6. .EnableRaisingEvents = True
    7. .Start()
    8. .WaitForExit()
    9. End With
    10. End Sub
    11. Private Sub Shell_Exited(sender As Object, e As EventArgs) Handles t.Exited
    12. btnRunSownloader.Enabled = True
    13. btnRunSownloader.Visible = True
    14. End Sub

    If Energy = Low Then
    Drink(aHugeCoffee)
    Else
    Drink(aHugeCoffeeToo)
    End If
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Private WithEvents T2 As Thread
    2. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    3. Dim T2 As New Threading.Thread(AddressOf Fun)
    4. T2.start()
    5. While T2.IsAlive
    6. Application.DoEvents()
    7. End While
    8. btnRunSownloader.Enabled = True
    9. btnRunSownloader.Visible = True
    10. End Sub
    11. Private Sub Fun()
    12. Dim T As New Process
    13. With t
    14. .StartInfo.FileName = "rundll32.exe"
    15. .StartInfo.Arguments = "InetCpl.cpl,ClearMyTracksByProcess 255"
    16. .Start()
    17. .WaitForExit()
    18. End With
    19. End Sub


    is allerdings sicherlich nicht die logischte lösung ^^ mir fällt nur keine bessere ein
    If Energy = Low Then
    Drink(aHugeCoffee)
    Else
    Drink(aHugeCoffeeToo)
    End If
    Man könnte es auch so lösen:

    VB.NET-Quellcode

    1. Private Async Sub button1_Click(sender As Object, e As EventArgs)
    2. Await Task.Run(Function()
    3. Dim p As New Process()
    4. p.StartInfo.FileName = "rundll32.exe"
    5. p.StartInfo.Arguments = "InetCpl.cpl,ClearMyTracksByProcess 255"
    6. p.EnableRaisingEvents = True
    7. p.Start()
    8. p.WaitForExit()
    9. End Function)
    10. MessageBox.Show("Feertig")
    11. End Sub

    Vorteil: Man muss kein böses DoEvents nutzen ! Warum nicht Application.DoEvents()?
    Warum so kompliziert?
    Einfach das Event abonnieren und darauf achten, dass EnableRaisingEvents auf True gesetzt ist.
    "Luckily luh... luckily it wasn't poi-"
    -- Brady in Wonderland, 23. Februar 2015, 1:56
    Desktop Pinner | ApplicationSettings | OnUtils

    Niko Ortner schrieb:

    Warum so kompliziert?
    Einfach das Event abonnieren und darauf achten, dass EnableRaisingEvents auf True gesetzt ist.


    das ist so leicht nicht Möglich, da die Controls sonst von anderen Threads aufgerufen werden.
    Dafür gibt es Invoke:

    VB.NET-Quellcode

    1. Private Sub Shell_Exited(sender As Object, e As EventArgs) Handles Shell.Exited
    2. If InvokeRequired Then
    3. Me.Invoke(Sub() Shell_Exited(sender, e))
    4. Return
    5. End If
    6. btnRunSownloader.Enabled = True
    7. btnRunSownloader.Visible = True
    8. End Sub