0

I'm writing a WPFapplication where at some point i have to deserialize a binary file into a singleton class. Everything is fine, except that if i put a MessageBox.Show() inside the catch clause i incur in the message being triggered. The problem is that i can't go in with the debug, and Console.Writeline() does not output anything, moreover i have also tried to close the application with Application.Current.Shutdown() but the application keeps running.

My guess is that there is some kind of bug going on, i would like to know what cause the problem.

Here is the code:

    public void Deserialize(string filename)
    {
        BinaryReader br = null;
        try
        {
            br = new BinaryReader(new FileStream(filename, FileMode.Open));

            //Do deserialization
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString(), "An error has occured while loading.", MessageBoxButton.OK, MessageBoxImage.Error); //message is being thrown with FileNotFound exception
            Console.WriteLine("An error occured:"  + e.ToString());//never prints
            Application.Current.Shutdown();//never closes application
        }
        finally
        {
            br?.Close();
        }
    }

The problem seems related to VisualStudio, I am using the enterprise edition 2015. However it does not always occur.

deight
  • 331
  • 1
  • 4
  • 15
  • *"i incur in the message being triggered"* -- can you explain that concept using short simple words? Maybe just describe what you see on the screen when "i incur in the message being triggered" happens. – 15ee8f99-57ff-4f92-890c-b56153 May 09 '17 at 16:00
  • 2
    [No output to console from a WPF application](http://stackoverflow.com/questions/160587/no-output-to-console-from-a-wpf-application) – Equalsk May 09 '17 at 16:01
  • Additionally, `Application.Current.Shutdown();` may only be called from the thread that created the `Application` object, is this the case? – Equalsk May 09 '17 at 16:02
  • Putting the call outside the catch block kills the application. Here is the message thrown: https://postimg.org/image/6u60xpqyb/ I didn't say that i can use the application and the data is loaded correctly, even if the message is thrown (i can see it in background). It seems that running the .exe outside the visualstudio environment do not cause this problem. – deight May 09 '17 at 16:08
  • You'll have to use Environment.Exit() to get a better guarantee that your app will terminate. Although it certainly won't hurt to use the Debug > Windows > Threads debugger window to find out what threads are still active. – Hans Passant May 09 '17 at 16:21

0 Answers0