0

In my application (Windows 7 Prof, Xamarin Studio V 5.10.1 build 6) I try to open a serial port. If this port is not available, I try to send a dialog message to the user and then to exit the program with the following code, which unfortunately doesn't terminate the application:

enter code here

using System;
using Gtk;
using Pango;                    // benötigt für Font-Operationen
using System.Diagnostics;       // benötigt für "Process.Start (...)"
using System.IO.Ports;          // benötigt für serielle Schnittstelle
using System.IO;                // benötigt für IOException

public partial class MainWindow : Gtk.Window
{
   // Globale Variable der Klasse
   // ---------------------------
   bool valret = true, wahl = true;
   SerialPort sPort;
   ....

    // Initialisierung der seriellen Schnittstelle
    // -------------------------------------------
    try { sPort.Open(); } 
    catch {
            MessageDialog md = new MessageDialog (null, 
            DialogFlags.DestroyWithParent,
            MessageType.Error, 
            ButtonsType.Close, "Serial IF nicht vorhanden");

        int result = md.Run ();
        Application.Quit ();
        md.Destroy ();
    }

Neither the Main window is closed nor is the *.exe program terminated. Instead, the program continues to run with errors due to the following wrong access to sPort functions not being available.

Which code is recommended to do the job? Thanks a lot for any comments!

Olduhu
  • 11
  • 2
    Application.Quit() is fairly weak sauce, it can be trivially ignored by a window. Or does nothing when Application.Run() wasn't yet called, rather likely in the constructor of your main window. Environment.Exit() is more convincing. – Hans Passant Jul 10 '16 at 12:00
  • why don't you first try to figure out how to terminate an application when there aren't any errors, just terminating it, and that's an easy thing to search for. And your question as a lot in it that is irrelevant, you could make your example code much smaller. sPort and so on is irrelevant. When you post a question it should be useful to others too, so to the point re the subject. And you can always catch an exception and put your exit statement there. But you haven't even looked into what exit statement would work(aside from error handling) – barlop Jul 10 '16 at 12:00
  • Looks like a duplicate where the question and the answer have no advantage over what has already been posted. – barlop Jul 10 '16 at 12:07

1 Answers1

0

Try using Application.Exit() or Environment.Exit() instead of Application.Quit().

I hope this helped, good luck.

r3fresh_
  • 87
  • 1
  • 8
  • isn't that answer here http://stackoverflow.com/questions/12977924/how-to-properly-exit-a-c-sharp-application that the application exit is for winforms and the environment exit for general(other than winforms). – barlop Jul 10 '16 at 12:04
  • Yes I suppose this is a duplicate question – r3fresh_ Jul 10 '16 at 12:05
  • Replacing Application.Quit() by the proposed System.Environment.Exit(1) actually does the job and terminates both the main window and the *.exe task. On the other hand Application.Exit() is not accepted by the compiler of Xamarin Studio 5.10. Thanks for your valuable assistance! – Olduhu Jul 12 '16 at 12:05