none
Nach Exit-Statement .Net Framework Fehler RRS feed

  • Frage

  • Hallo liebe Community

    Ich habe ein Script erstellt, das Fenster mit Windows Forms erstellt. Mithilfe des Exit-Statements möchte ich mein Script beenden. Nach Aufruf erscheint aber folgender Fehler:

    Unhandled exception has occurred in a component in your application. If you click Continue, the application will ignore this error and attempt to continue. 
    
    System error. 

    Hier das Details-Fenster zu der Fehlermeldung:

    See the end of this message for details on invoking 
    just-in-time (JIT) debugging instead of this dialog box.
    
    ************** Exception Text **************
    System.Management.Automation.ExitException: System error.
       at System.Management.Automation.Interpreter.ThrowInstruction.Run(InterpretedFrame frame)
       at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
       at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
       at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
       at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
       at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
       at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
       at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
       at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
       at System.Management.Automation.ScriptBlock.InvokeWithPipeImpl(ScriptBlockClauseToInvoke clauseToInvoke, Boolean createLocalScope, Dictionary`2 functionsToDefine, List`1 variablesToDefine, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Object[] args)
       at System.Management.Automation.ScriptBlock.<>c__DisplayClassa.<InvokeWithPipe>b__8()
       at System.Management.Automation.Runspaces.RunspaceBase.RunActionIfNoRunningPipelinesWithThreadCheck(Action action)
       at System.Management.Automation.ScriptBlock.InvokeWithPipe(Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Boolean propagateAllExceptionsToTop, List`1 variablesToDefine, Dictionary`2 functionsToDefine, Object[] args)
       at System.Management.Automation.ScriptBlock.InvokeAsDelegateHelper(Object dollarUnder, Object dollarThis, Object[] args)
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.PerformClick()
       at System.Windows.Forms.Form.ProcessDialogKey(Keys keyData)
       at System.Windows.Forms.Control.PreProcessMessage(Message& msg)
       at System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg)
       at System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)
    
    
    ************** Loaded Assemblies **************
    mscorlib
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0 built by: NET47REL1
        CodeBase: file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/mscorlib.dll
    ----------------------------------------
    powershell_ise
        Assembly Version: 3.0.0.0
        Win32 Version: 10.0.10586.117 (th2_release.160212-2359)
        CodeBase: file:///C:/Windows/System32/WindowsPowerShell/v1.0/powershell_ise.exe
    ----------------------------------------
    System.Windows.Forms
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0 built by: NET47REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
    ----------------------------------------
    System
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0 built by: NET47REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
    ----------------------------------------
    System.Drawing
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0 built by: NET47REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
    ----------------------------------------
    System.Management.Automation
        Assembly Version: 3.0.0.0
        Win32 Version: 10.0.10586.117
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Management.Automation/v4.0_3.0.0.0__31bf3856ad364e35/System.Management.Automation.dll
    ----------------------------------------
    System.Core
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0 built by: NET47REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
    ----------------------------------------
    Microsoft.PowerShell.ISECommon
        Assembly Version: 3.0.0.0
        Win32 Version: 10.0.10586.117
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.PowerShell.ISECommon/v4.0_3.0.0.0__31bf3856ad364e35/Microsoft.PowerShell.ISECommon.dll
    ----------------------------------------
    System.Configuration
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0 built by: NET47REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
    ----------------------------------------
    System.Xml
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0 built by: NET47REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
    ----------------------------------------
    Microsoft.PowerShell.GPowerShell
        Assembly Version: 3.0.0.0
        Win32 Version: 10.0.10586.117
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.PowerShell.GPowerShell/v4.0_3.0.0.0__31bf3856ad364e35/Microsoft.PowerShell.GPowerShell.dll
    ----------------------------------------
    System.ComponentModel.Composition
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.ComponentModel.Composition/v4.0_4.0.0.0__b77a5c561934e089/System.ComponentModel.Composition.dll
    ----------------------------------------
    Microsoft.PowerShell.Editor
        Assembly Version: 3.0.0.0
        Win32 Version: 10.0.10586.117
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.PowerShell.Editor/v4.0_3.0.0.0__31bf3856ad364e35/Microsoft.PowerShell.Editor.dll
    ----------------------------------------
    PresentationFramework
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework/v4.0_4.0.0.0__31bf3856ad364e35/PresentationFramework.dll
    ----------------------------------------
    WindowsBase
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0 built by: NET47REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/WindowsBase/v4.0_4.0.0.0__31bf3856ad364e35/WindowsBase.dll
    ----------------------------------------
    PresentationCore
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0 built by: NET47REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_64/PresentationCore/v4.0_4.0.0.0__31bf3856ad364e35/PresentationCore.dll
    ----------------------------------------
    System.Xaml
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0 built by: NET47REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xaml/v4.0_4.0.0.0__b77a5c561934e089/System.Xaml.dll
    ----------------------------------------
    System.Runtime.Serialization
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0 built by: NET47REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Runtime.Serialization/v4.0_4.0.0.0__b77a5c561934e089/System.Runtime.Serialization.dll
    ----------------------------------------
    UIAutomationProvider
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0 built by: NET47REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/UIAutomationProvider/v4.0_4.0.0.0__31bf3856ad364e35/UIAutomationProvider.dll
    ----------------------------------------
    Accessibility
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0 built by: NET47REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Accessibility/v4.0_4.0.0.0__b03f5f7f11d50a3a/Accessibility.dll
    ----------------------------------------
    MetadataViewProxies_73ce144e-28d6-4475-9096-4b41cb701437
        Assembly Version: 0.0.0.0
        Win32 Version: 4.7.2053.0
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.ComponentModel.Composition/v4.0_4.0.0.0__b77a5c561934e089/System.ComponentModel.Composition.dll
    ----------------------------------------
    System.DirectoryServices
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0 built by: NET47REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.DirectoryServices/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.DirectoryServices.dll
    ----------------------------------------
    System.Management
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0 built by: NET47REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Management/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Management.dll
    ----------------------------------------
    Microsoft.Management.Infrastructure
        Assembly Version: 1.0.0.0
        Win32 Version: 6.3.9600.16384
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.Management.Infrastructure/v4.0_1.0.0.0__31bf3856ad364e35/Microsoft.Management.Infrastructure.dll
    ----------------------------------------
    PresentationFramework.AeroLite
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0 built by: NET47REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework.AeroLite/v4.0_4.0.0.0__31bf3856ad364e35/PresentationFramework.AeroLite.dll
    ----------------------------------------
    PresentationFramework-SystemXml
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework-SystemXml/v4.0_4.0.0.0__b77a5c561934e089/PresentationFramework-SystemXml.dll
    ----------------------------------------
    System.Numerics
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0 built by: NET47REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Numerics/v4.0_4.0.0.0__b77a5c561934e089/System.Numerics.dll
    ----------------------------------------
    System.Configuration.Install
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0 built by: NET47REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration.Install/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.Install.dll
    ----------------------------------------
    System.Transactions
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0 built by: NET47REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_64/System.Transactions/v4.0_4.0.0.0__b77a5c561934e089/System.Transactions.dll
    ----------------------------------------
    Anonymously Hosted DynamicMethods Assembly
        Assembly Version: 0.0.0.0
        Win32 Version: 4.7.2053.0 built by: NET47REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_64/mscorlib/v4.0_4.0.0.0__b77a5c561934e089/mscorlib.dll
    ----------------------------------------
    Microsoft.PowerShell.Security
        Assembly Version: 3.0.0.0
        Win32 Version: 10.0.10586.117
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.PowerShell.Security/v4.0_3.0.0.0__31bf3856ad364e35/Microsoft.PowerShell.Security.dll
    ----------------------------------------
    Microsoft.PowerShell.GraphicalHost
        Assembly Version: 3.0.0.0
        Win32 Version: 10.0.10586.117
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.PowerShell.GraphicalHost/v4.0_3.0.0.0__31bf3856ad364e35/Microsoft.PowerShell.GraphicalHost.dll
    ----------------------------------------
    UIAutomationTypes
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0 built by: NET47REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/UIAutomationTypes/v4.0_4.0.0.0__31bf3856ad364e35/UIAutomationTypes.dll
    ----------------------------------------
    Microsoft.GeneratedCode
        Assembly Version: 1.0.0.0
        Win32 Version: 4.7.2053.0 built by: NET47REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
    ----------------------------------------
    PresentationFramework-SystemCore
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework-SystemCore/v4.0_4.0.0.0__b77a5c561934e089/PresentationFramework-SystemCore.dll
    ----------------------------------------
    Windows.UI
        Assembly Version: 255.255.255.255
        Win32 Version: 
        CodeBase: file:///C:/Windows/system32/WinMetadata/Windows.UI.winmd
    ----------------------------------------
    Windows.Foundation
        Assembly Version: 255.255.255.255
        Win32 Version: 
        CodeBase: file:///C:/Windows/system32/WinMetadata/Windows.Foundation.winmd
    ----------------------------------------
    System.Runtime.InteropServices.WindowsRuntime
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Runtime.InteropServices.WindowsRuntime/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Runtime.InteropServices.WindowsRuntime.dll
    ----------------------------------------
    System.Runtime.WindowsRuntime
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0 built by: NET47REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Runtime.WindowsRuntime/v4.0_4.0.0.0__b77a5c561934e089/System.Runtime.WindowsRuntime.dll
    ----------------------------------------
    System.Data
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0 built by: NET47REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_64/System.Data/v4.0_4.0.0.0__b77a5c561934e089/System.Data.dll
    ----------------------------------------
    PresentationFramework-SystemData
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework-SystemData/v4.0_4.0.0.0__b77a5c561934e089/PresentationFramework-SystemData.dll
    ----------------------------------------
    SMDiagnostics
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0 built by: NET47REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/SMDiagnostics/v4.0_4.0.0.0__b77a5c561934e089/SMDiagnostics.dll
    ----------------------------------------
    System.ServiceModel.Internals
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0 built by: NET47REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.ServiceModel.Internals/v4.0_4.0.0.0__31bf3856ad364e35/System.ServiceModel.Internals.dll
    ----------------------------------------
    System.Diagnostics.Tracing
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Diagnostics.Tracing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Diagnostics.Tracing.dll
    ----------------------------------------
    Microsoft.PowerShell.Commands.Utility
        Assembly Version: 3.0.0.0
        Win32 Version: 10.0.10586.117
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.PowerShell.Commands.Utility/v4.0_3.0.0.0__31bf3856ad364e35/Microsoft.PowerShell.Commands.Utility.dll
    ----------------------------------------
    Microsoft.PowerShell.Commands.Management
        Assembly Version: 3.0.0.0
        Win32 Version: 10.0.10586.117
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.PowerShell.Commands.Management/v4.0_3.0.0.0__31bf3856ad364e35/Microsoft.PowerShell.Commands.Management.dll
    ----------------------------------------
    PresentationFramework-SystemDrawing
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework-SystemDrawing/v4.0_4.0.0.0__b77a5c561934e089/PresentationFramework-SystemDrawing.dll
    ----------------------------------------
    Microsoft.PowerShell.ScheduledJob
        Assembly Version: 3.0.0.0
        Win32 Version: 10.0.10586.117
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.PowerShell.ScheduledJob/v4.0_3.0.0.0__31bf3856ad364e35/Microsoft.PowerShell.ScheduledJob.dll
    ----------------------------------------
    PSEventHandler
        Assembly Version: 0.0.0.0
        Win32 Version: 10.0.10586.117
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Management.Automation/v4.0_3.0.0.0__31bf3856ad364e35/System.Management.Automation.dll
    ----------------------------------------
    Microsoft.CSharp
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.CSharp/v4.0_4.0.0.0__b03f5f7f11d50a3a/Microsoft.CSharp.dll
    ----------------------------------------
    System.Dynamic
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Dynamic/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Dynamic.dll
    ----------------------------------------
    Microsoft.WSMan.Management
        Assembly Version: 3.0.0.0
        Win32 Version: 10.0.10586.117
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.WSMan.Management/v4.0_3.0.0.0__31bf3856ad364e35/Microsoft.WSMan.Management.dll
    ----------------------------------------
    System.Security
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0 built by: NET47REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Security/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Security.dll
    ----------------------------------------
    System.ServiceProcess
        Assembly Version: 4.0.0.0
        Win32 Version: 4.7.2053.0 built by: NET47REL1
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.ServiceProcess/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.ServiceProcess.dll
    ----------------------------------------
    Microsoft.WSMan.Runtime
        Assembly Version: 3.0.0.0
        Win32 Version: 10.0.10586.117
        CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.WSMan.Runtime/v4.0_3.0.0.0__31bf3856ad364e35/Microsoft.WSMan.Runtime.dll
    ----------------------------------------
    
    ************** JIT Debugging **************
    To enable just-in-time (JIT) debugging, the .config file for this
    application or computer (machine.config) must have the
    jitDebugging value set in the system.windows.forms section.
    The application must also be compiled with debugging
    enabled.
    
    For example:
    
    <configuration>
        <system.windows.forms jitDebugging="true" />
    </configuration>
    
    When JIT debugging is enabled, any unhandled exception
    will be sent to the JIT debugger registered on the computer
    rather than be handled by this dialog box.

    Wie schon gesagt tritt der Fehler immer dann auf, wenn ich das Exit-Statement verwende. Hier ein Beispiel aus meinem Code:

    $ListThemesWindow.Dispose()
    $Global:SourcePath += $this.Name + "\"
    Invoke-Expression -Command  ($this.Text+"Window")
    Exit

    Nach dem Exit folgt dieser Fehler. Meine bisherigen Problemlösungsversuche:

    • .Net Framework deinstalliert und wieder installiert
    • .Net Framework auf Version 4.7 angehoben

    Mein System:

    • Windows Server 2012 R2 Standard
    • Powershell Version 5.0
    • .Net Framework 4.7
    • Aktuellster Patchstand
    • 8 GB RAM
    • Intel I5 6500

    Falls wer einen Lösungsvorschlag hat, ich bin offen für sehr viel.

    Vielen Dank

    MiTschMR


    • Bearbeitet MiTschMR Donnerstag, 4. Mai 2017 10:12
    Donnerstag, 4. Mai 2017 09:54

Antworten

  • Dispose veranlasst die Form, sich natürlich zu zerstören.
    Allerdings darfst du das erst, wenn du die Form verlassen hast, sonst kommt es zu Zugriffen auf das zerstörte Formular. D.h., der Dispose erfolgt erst nach ShowDialog().
    Wenn du dann den nächsten Ordner anzeigst kannst du das 1. Fenster nicht enfach zumachen.
    Das 2. Fenster kannst du dann ebenso per ShowDialog mit Parent des 1. Fensters anzeigen.
    Wenn du dies nicht möchtest, kannst du die Position des Formulars in einen unsichtbaren Bereich (z.B. -32000) einfach verschieben und hinterher wieder zurückstellen.
    Bedenke, dass durch das Event-System ein Callback in dein Script erfolgt.
    Du musst also warten, bis der Anwender das Kreuz oben rechts (falls sichtbar) drückt oder einen Close-Button hinzufügen.
    Der Close mit anschließendem Return (der auch fehlen kann) schließt das Formular und kehrt ins Script zurück.

    • Als Antwort markiert MiTschMR Donnerstag, 4. Mai 2017 11:49
    Donnerstag, 4. Mai 2017 11:28

Alle Antworten

  • Da du externe Ressourcen verwendest musst du diese erst wieder freigeben.
    Bei Form-Objekten reicht i.d.R. ein Close, der Dispose ist hierfür nicht nötig.
    Von der Stackfolge sehe ich nur, dass du wohl auf Grund des OnClick-Ereignisses, dass innerhalb der Form auftritt, die Anwendung beendest.
    Hier solltest du dann Close aufrufen und dann in das Script zurückkehren.

    Das wäre ja sonst vergleichbar, als wenn du während der Fahrt im Auto einfach die Zündung abstellst.

    Donnerstag, 4. Mai 2017 10:15
  • Hallo

    erstmal danke für deine Antwort. Wenn ich ein Close-Statement verwende, schliesst es das Fenster nicht. Ich poste mal meinen ganzen Code:

    #region Variables and Type Import
    
    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing
    $Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe")
    $SourcePath = "Any Path"
    $BackgroundImage = [System.Drawing.Image]::FromFile("Any Path to a Picture")
    $i = 0
    
    #Hardware-Wiki\Komponenten\Arbeitsspeicher\Allgemein for example
    $ListThemes3 = {
        $ListThemes3Window = New-Object System.Windows.Forms.Form
        $ListThemes3Window.Text = "Hardware-Wiki"
        $ListThemes3Window.StartPosition = "CenterScreen"
        $ListThemes3Window.Width = $BackgroundImage.Width
        $ListThemes3Window.Height = $BackgroundImage.Height
        $ListThemes3Window.Icon = $Icon
        $ListThemes3Window.BackgroundImage = $BackgroundImage
        
        $ListThemes3Label = New-Object System.Windows.Forms.Label
        $ListThemes3Label.Location = New-Object System.Drawing.Point(10,20)
        $ListThemes3Label.Font = New-Object System.Drawing.Font("Times New Roman",28,[System.Drawing.FontStyle]::Bold)
        $ListThemes3Label.ForeColor = "Yellow"
        $ListThemes3Label.Size = New-Object System.Drawing.Size(400,42)
        $ListThemes3Label.Text = Get-Content -Path ($SourcePath + "index.txt")
        $ListThemes3Label.BackColor = "Transparent"
        $ListThemes3Window.Controls.Add($ListThemes3Label)
    
        $ListThemes3Window.TopMost = $true
    
        #Create Buttons
        ForEach($Folder in (Get-ChildItem -Path ($SourcePath) | where{$_.PSISContainer})){
            $ListThemes3Button = New-Object System.Windows.Forms.Button
            if((Get-ChildItem -Path ($SourcePath) | where{$_.PSISContainer}).Count -eq 1){
                $ListThemes3Button.Text = Get-ChildItem -Path ($SourcePath) | where{$_.PSISContainer}
                $ListThemes3Button.Name = Get-ChildItem -Path ($SourcePath)| where{$_.PSISContainer}
            }
            else{
                $ListThemes3Button.Text = (Get-ChildItem -Path ($SourcePath)| where{$_.PSISContainer}).BaseName[$i]
                $ListThemes3Button.Name = (Get-ChildItem -Path ($SourcePath)| where{$_.PSISContainer}).BaseName[$i]
            }
            $ListThemes3Button.Text = ($ListThemes3Button.Text).Replace(" ","")
            $ListThemes3Button.Font = New-Object System.Drawing.Font("Arial",10)
            $ListThemes3Button.Location = New-Object System.Drawing.Point(20,(80+50*$i))
            $ListThemes3Button.Size = New-Object System.Drawing.Size(120,40)
            $ListThemes3Window.Controls.Add($ListThemes3Button)
    
            #Create Button Functions
            Set-Variable -Name ("ListThemes3Button" + $ListThemes3Button.Text) -Value $ListThemes3Button
            Set-Variable -Name ($ListThemes3Button.Text + "Window") -Value ("Function " + $ListThemes3Button.Text + "Window {}") -Scope Global
            $ToInvoke = Get-Variable -Name ($ListThemes3Button.Text + "Window") -ValueOnly
            Invoke-Expression $ToInvoke
            $FunctionName = ($ListThemes3Button.Text + "Window")
            $ScriptBlock = (Get-Command $FunctionName -CommandType Function).ScriptBlock
            $AllListThemes3Button += (Get-Variable -Name ("ListThemes3Button" + $ListThemes3Button.Text)).Name + "`n"
    
            #Button Click-Events
            (Get-Variable -Name ("ListThemes3Button" + $ListThemes3Button.Text) -ValueOnly).add_Click({
            $AllListThemes3Button = $AllListThemes3Button.Split("`n")
                ForEach($ButtonName in $AllListThemes3Button){
                    if($ButtonName -eq ("ListThemes3Button" + $this.Text)){
                        Write-Host (Get-Variable -Name ("ListThemes3Button" + $this.Text)).Name
                        $ListThemes3Window.Dispose()
                        $Global:SourcePath += $this.Name + "\"
                        Invoke-Expression -Command  ($this.Text + "Window")
                        Exit
                    }
                }
            })
    
            #Continue
            if($i -ne (Get-ChildItem -Path ($SourcePath) | where{$_.PSISContainer}).Count -1){
            $AllListThemes3Buttons += (Get-Variable -Name ("ListThemes3Button" + $ListThemes3Button.Text)).Name + "`n"
            }
            else{
                $AllListThemes3Buttons += (Get-Variable -Name ("ListThemes3Button" + $ListThemes3Button.Text)).Name
            }
                $i++
            }
    
        $i = 0
    
        $ListThemes3Window.ShowDialog()
    }
    
    #Hardware-Wiki\Komponenten\Arbeitsspeicher for example
    $ListThemes2 = {
        $ListThemes2Window = New-Object System.Windows.Forms.Form
        $ListThemes2Window.Text = "Hardware-Wiki"
        $ListThemes2Window.StartPosition = "CenterScreen"
        $ListThemes2Window.Width = $BackgroundImage.Width
        $ListThemes2Window.Height = $BackgroundImage.Height
        $ListThemes2Window.Icon = $Icon
        $ListThemes2Window.BackgroundImage = $BackgroundImage
        
        $ListThemes2Label = New-Object System.Windows.Forms.Label
        $ListThemes2Label.Location = New-Object System.Drawing.Point(10,20)
        $ListThemes2Label.Font = New-Object System.Drawing.Font("Times New Roman",28,[System.Drawing.FontStyle]::Bold)
        $ListThemes2Label.ForeColor = "Yellow"
        $ListThemes2Label.Size = New-Object System.Drawing.Size(300,40)
        $ListThemes2Label.Text = Get-Content -Path ($SourcePath + "index.txt")
        $ListThemes2Label.BackColor = "Transparent"
        $ListThemes2Window.Controls.Add($ListThemes2Label)
    
        $ListThemes2Window.TopMost = $true
    
        #Create Buttons
        ForEach($Folder in (Get-ChildItem -Path ($SourcePath) | where{$_.PSISContainer})){
            $ListThemes2Button = New-Object System.Windows.Forms.Button
            if((Get-ChildItem -Path ($SourcePath) | where{$_.PSISContainer}).Count -eq 1){
                $ListThemes2Button.Text = Get-ChildItem -Path ($SourcePath) | where{$_.PSISContainer}
                $ListThemes2Button.Name = Get-ChildItem -Path ($SourcePath)| where{$_.PSISContainer}
            }
            else{
                $ListThemes2Button.Text = (Get-ChildItem -Path ($SourcePath)| where{$_.PSISContainer}).BaseName[$i]
                $ListThemes2Button.Name = (Get-ChildItem -Path ($SourcePath)| where{$_.PSISContainer}).BaseName[$i]
            }
            $ListThemes2Button.Text = ($ListThemes2Button.Text).Replace(" ","")
            $ListThemes2Button.Font = New-Object System.Drawing.Font("Arial",10)
            $ListThemes2Button.Location = New-Object System.Drawing.Point(20,(80+50*$i))
            $ListThemes2Button.Size = New-Object System.Drawing.Size(120,40)
            $ListThemes2Window.Controls.Add($ListThemes2Button)
    
            #Create Button Functions
            Set-Variable -Name ("ListThemes2Button" + $ListThemes2Button.Text) -Value $ListThemes2Button
            Set-Variable -Name ($ListThemes2Button.Text + "Window") -Value ("Function " + $ListThemes2Button.Text + "Window {$ListThemes3}") -Scope Global
            $ToInvoke = Get-Variable -Name ($ListThemes2Button.Text + "Window") -ValueOnly
            Invoke-Expression $ToInvoke
            $FunctionName = ($ListThemes2Button.Text + "Window")
            $ScriptBlock = (Get-Command $FunctionName -CommandType Function).ScriptBlock
            $AllListThemes2Button += (Get-Variable -Name ("ListThemes2Button" + $ListThemes2Button.Text)).Name + "`n"
    
            #Button Click-Events
            (Get-Variable -Name ("ListThemes2Button" + $ListThemes2Button.Text) -ValueOnly).add_Click({
                $AllListThemes2Button = $AllListThemes2Button.Split("`n")
                ForEach($ButtonName in $AllListThemes2Button){
                    if($ButtonName -eq ("ListThemes2Button" + $this.Text)){
                        Write-Host (Get-Variable -Name ("ListThemes2Button" + $this.Text)).Name
                        $ListThemes2Window.Dispose()
                        $Global:SourcePath += $this.Name + "\"
                        Invoke-Expression -Command  ($this.Text + "Window")
                        Exit
                    }
                }
            })
    
            #Continue
            if($i -ne (Get-ChildItem -Path ($SourcePath) | where{$_.PSISContainer}).Count -1){
                $AllListThemes2Buttons += (Get-Variable -Name ("ListThemes2Button" + $ListThemes2Button.Text)).Name + "`n"
            }
            else{
                $AllListThemes2Buttons += (Get-Variable -Name ("ListThemes2Button" + $ListThemes2Button.Text)).Name
            }
            $i++
        }
    
        $i = 0
    
        $ListThemes2Window.ShowDialog()
    }
    
    #endregion
    
    #region Functions
    
    function MainMenu{
        $MainMenuWindow = New-Object System.Windows.Forms.Form
        $MainMenuWindow.Text = "Hardware-Wiki"
        $MainMenuWindow.StartPosition = "CenterScreen"
        $MainMenuWindow.Width = $BackgroundImage.Width
        $MainMenuWindow.Height = $BackgroundImage.Height
        $MainMenuWindow.Icon = $Icon
        $MainMenuWindow.BackgroundImage = $BackgroundImage
    
        $MainMenuLabel = New-Object System.Windows.Forms.Label
        $MainMenuLabel.Location = New-Object System.Drawing.Point(10,20)
        $MainMenuLabel.Size = New-Object System.Drawing.Size(300,40)
        $MainMenuLabel.Text = "Was möchten Sie tun?"
        $MainMenuLabel.Font = New-Object System.Drawing.Font("Times New Roman",21,[System.Drawing.FontStyle]::Bold)
        $MainMenuLabel.ForeColor = "Yellow"
        $MainMenuLabel.BackColor = "Transparent"
        $MainMenuWindow.Controls.Add($MainMenuLabel)
    
        $MainMenuButtonLesen = New-Object System.Windows.Forms.Button
        $MainMenuButtonLesen.Location = New-Object System.Drawing.Point(10,80)
        $MainMenuButtonLesen.Size = New-Object System.Drawing.Size(120,30)
        $MainMenuButtonLesen.Text = "Wiki lesen"
        $MainMenuButtonLesen.Font = New-Object System.Drawing.Font("Arial",10)
        $MainMenuWindow.Controls.Add($MainMenuButtonLesen)
    
        $MainMenuWindow.TopMost = $true
    
        #Click-Event
        $MainMenuButtonLesen.add_Click({
            $MainMenuWindow.Dispose()
            ListThemes
            Exit
        })
    
        $MainMenuWindow.ShowDialog()
    }
    
    function ListThemes{
        $ListThemesWindow = New-Object System.Windows.Forms.Form
        $ListThemesWindow.Text = "Hardware-Wiki"
        $ListThemesWindow.StartPosition = "CenterScreen"
        $ListThemesWindow.Width = $BackgroundImage.Width
        $ListThemesWindow.Height = $BackgroundImage.Height
        $ListThemesWindow.Icon = $Icon
        $ListThemesWindow.BackgroundImage = $BackgroundImage
    
        $ListThemesLabel = New-Object System.Windows.Forms.Label
        $ListThemesLabel.Location = New-Object System.Drawing.Point(10,20)
        $ListThemesLabel.Font = New-Object System.Drawing.Font("Times New Roman",28,[System.Drawing.FontStyle]::Bold)
        $ListThemesLabel.ForeColor = "Yellow"
        $ListThemesLabel.Size = New-Object System.Drawing.Size(400,40)
        $ListThemesLabel.Text = Get-Content -Path ($SourcePath + "index.txt")
        $ListThemesLabel.BackColor = "Transparent"
        $ListThemesWindow.Controls.Add($ListThemesLabel)
    
        $ListThemesWindow.TopMost = $true
    
        #Create Buttons
        ForEach($Folder in (Get-ChildItem -Path $SourcePath | where{$_.PSISContainer})){
            $ListThemesButton = New-Object System.Windows.Forms.Button
            if((Get-ChildItem -Path ($SourcePath) | where{$_.PSISContainer}).Count -eq 1){
                $ListThemesButton.Text = Get-ChildItem -Path ($SourcePath) | where{$_.PSISContainer}
                $ListThemesButton.Name = Get-ChildItem -Path ($SourcePath)| where{$_.PSISContainer}
            }
            else{
                $ListThemesButton.Text = (Get-ChildItem -Path ($SourcePath)| where{$_.PSISContainer}).BaseName[$i]
                $ListThemesButton.Name = (Get-ChildItem -Path ($SourcePath)| where{$_.PSISContainer}).BaseName[$i]
            }
            $ListThemesButton.Text = ($ListThemesButton.Text).Replace(" ","")
            $ListThemesButton.Font = New-Object System.Drawing.Font("Arial",10)
            $ListThemesButton.Location = New-Object System.Drawing.Point(20,(80+50*$i))
            $ListThemesButton.Size = New-Object System.Drawing.Size(120,30)
            $ListThemesWindow.Controls.Add($ListThemesButton)
    
            #Create Button Functions
            Set-Variable -Name ("ListThemesButton" + $ListThemesButton.Text) -Value $ListThemesButton
            Set-Variable -Name ($ListThemesButton.Text + "Window") -Value ("Function " + $ListThemesButton.Text + "Window {$ListThemes2}") -Scope Global
            $ToInvoke = Get-Variable -Name ($ListThemesButton.Text + "Window") -ValueOnly
            Invoke-Expression $ToInvoke
            $FunctionName = ($ListThemesButton.Text + "Window")
            $ScriptBlock = (Get-Command $FunctionName -CommandType Function).ScriptBlock
            $AllListThemesButton += (Get-Variable -Name ("ListThemesButton" + $ListThemesButton.Text)).Name + "`n"
    
            #Button Click-Events
            (Get-Variable -Name ("ListThemesButton" + $ListThemesButton.Text) -ValueOnly).add_Click({
                $AllListThemesButton = $AllListThemesButton.Split("`n")
                ForEach($ButtonName in $AllListThemesButton){
                    if($ButtonName -eq ("ListThemesButton" + $this.Text)){
                        Write-Host (Get-Variable -Name ("ListThemesButton" + $this.Text)).Name
                        $ListThemesWindow.Dispose()
                        $Global:SourcePath += $this.Name + "\"
                        Invoke-Expression -Command  ($this.Text+"Window")
                        Exit
                    }
                }
            })
    
            #Continue
            if($i -ne (Get-ChildItem -Path $SourcePath | where{$_.PSISContainer}).Count -1){
                $AllListThemesButton += (Get-Variable -Name ("ListThemesButton" + $ListThemesButton.Text)).Name + "`n"
            }
            else{
                $AllListThemesButton += (Get-Variable -Name ("ListThemesButton" + $ListThemesButton.Text)).Name
            }
            $i++
        }
    
        $i = 0
    
        $ListThemesWindow.ShowDialog()
    }
    
    #endregion
    
    #region Execution
    
    MainMenu
    
    #endregion

    Noch etwas Hintergrund zu meinem Code, ich bin dran, so etwas wie ein Wiki zu erstellen. Mit der SourcePath-Variable gebe ich den Anfangspfad an. Das Script liest dann die darunterstehenden Ordner aus und gibt sie als Buttons auf einer Form an. die index.txt Datei beinhaltet z.B. eine Überschrift. Wenn ich dann auf einen dieser Buttons drücke, gehe ich in einen Ordner hinein und gebe mit einer weiteren Form an, was sich dort an Ordner etc. befindet. Damit das erste Fenster verschwindet, muss ich Dispose() verwenden, mit Close() funktioniert es nicht.

    Ich hoffe so ist es etwas klarer.

    Donnerstag, 4. Mai 2017 10:59
  • Dispose veranlasst die Form, sich natürlich zu zerstören.
    Allerdings darfst du das erst, wenn du die Form verlassen hast, sonst kommt es zu Zugriffen auf das zerstörte Formular. D.h., der Dispose erfolgt erst nach ShowDialog().
    Wenn du dann den nächsten Ordner anzeigst kannst du das 1. Fenster nicht enfach zumachen.
    Das 2. Fenster kannst du dann ebenso per ShowDialog mit Parent des 1. Fensters anzeigen.
    Wenn du dies nicht möchtest, kannst du die Position des Formulars in einen unsichtbaren Bereich (z.B. -32000) einfach verschieben und hinterher wieder zurückstellen.
    Bedenke, dass durch das Event-System ein Callback in dein Script erfolgt.
    Du musst also warten, bis der Anwender das Kreuz oben rechts (falls sichtbar) drückt oder einen Close-Button hinzufügen.
    Der Close mit anschließendem Return (der auch fehlen kann) schließt das Formular und kehrt ins Script zurück.

    • Als Antwort markiert MiTschMR Donnerstag, 4. Mai 2017 11:49
    Donnerstag, 4. Mai 2017 11:28
  • Hallo,

    Vielen Dank für deine Antwort. Die hat mich auf die richtige Spur gebracht. Ich danke dir vielmals für die schnelle Hilfe.

    Freundliche Grüsse

    MiTschMR

    Donnerstag, 4. Mai 2017 11:49