none
Aus dcer MSDN Hotline: Scließen einer Windows Forms Anwendung RRS feed

  • Allgemeine Diskussion

  • Hallo zusammen,

    heute wurde uns bei der MSDN Hotline unter anderem folgende Frage gestellt:
    Ich versuche meine Windows Forms Anwendung mit CloseMainWindow() und Close() zu beenden. Die beiden Funktionen zeigen aber keine Wirkung. Warum ist dass so? Was mache ich falsch.

    Unsere Antwort bzw. unser Lösungsvorschlag darauf war:
    Das Problem liegt in der CloseMainWindow() – Methode.

    Diese ist die Methode, die eigentlich den Process beenden soll. Close() befreit nur die Ressourcen, die mit dem Process assoziiert sind, beendet den Process aber nicht.

    Was ist jetzt das Problem bei CloseMainWindow()? Das Problem besteht darin, dass die Methode nur das Hauptfenster einer Anwendung schließt. Das ist normalerweise ausreichend. In diesem Fall aber, da die Anwendung kein MainWindow hat oder überhaupt kein grafisches Interface, wird die Methode erfolglos ausgeführt.
    CloseMainWindow() hat auch einen Rückgabewert. Wenn dieser false ist, dann konnte den MainWindow nicht geschlossen werden. Wenn CloseMainWindow() den Processs nicht beenden kann, dann ist Process().Kill() die einzige Alternative. Das Ganze ist auch in der MSDN beschrieben.

    When a process is executing, its message loop is in a wait state. The message loop executes every time a Windows message is sent to the process by the operating system. Calling CloseMainWindow sends a request to close to the main window, which, in a well-formed application, closes child windows and revokes all running message loops for the application. The request to exit the process by calling CloseMainWindow does not force the application to quit. The application can ask for user verification before quitting, or it can refuse to quit. To force the application to quit, use the Kill method. The behavior of CloseMainWindow is identical to that of a user closing an application's main window using the system menu. Therefore, the request to exit the process by closing the main window does not force the application to quit immediately.
    Data edited by the process or resources allocated to the process can be lost if you call Kill. Kill causes an abnormal process termination, and should be used only when necessary. CloseMainWindow enables an orderly termination of the process and closes all windows, so it is preferable for applications with an interface. If CloseMainWindow fails, you can use Kill to terminate the process. Kill is the only way to terminate processes that do not have graphical interfaces.
    You can call Kill and CloseMainWindow only for processes that are running on the local computer. You cannot cause processes on remote computers to exit. You can only view information for processes running on remote computers.

    Mehr Informationen zum Thema findet man auf den folgenden Adressen in MSDN Online:

    1. MSDN: Process.CloseMainWindow Method
    1. MSDN: Process.Close Method
    1. MSDN: Process.Kill Method

    Viele Grüße,
    Atanas Dimitrov
    MSDN Hotline für MSDN Online Deutschland

    Disclaimer:
    Bitte haben Sie Verständnis dafür, dass wir hier auf Rückfragen gar nicht oder nur sehr zeitverzögert antworten können.
    Bitte nutzen Sie für Rückfragen oder neue Fragen den telefonischen Weg über die MSDN Hotline: http://www.msdn-online.de/Hotline 
    MSDN Hotline: Schnelle & kompetente Hilfe für Entwickler: kostenfrei!

    Es gelten für die MSDN Hotline und dieses Posting diese Nutzungsbedingungen, Hinweise zu Markenzeichen sowie die allgemein gültigen Informationen zur Datensicherheit sowie die gesonderten Nutzungsbedingungen für die MSDN Hotline.

    Montag, 15. November 2010 12:47