none
Exception- Cannot set Visibility or call Show or ShowDialog after window has closed

    Question

  • Please help me out. 
    Unable to remove this error from my application x-(
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using Microsoft.MultiPoint.CommonTypes;
    using Microsoft.MultiPoint.MousePlugIn;
    using Microsoft.MultiPoint.Controls;
    using System.Drawing;
    using System.Media;
    using Microsoft.MultiPoint.SDK;
    
    namespace InfernoMT
    {
    	/// <summary>
    	/// Interaction logic for Window3.xaml
    	/// </summary>
    	public partial class girl_intro_H : Window
    	{
    		public girl_intro_H()
    		{
    			this.InitializeComponent();			
    			// Insert code required on object creation below this point.			
    		}
    
    		private void girl_intro_ended(object sender, System.Windows.RoutedEventArgs e)
    		{
    			// TODO: Add event handler implementation here.
                Intro_living_H window = new Intro_living_H();
                window.Show();
                this.Close();			
    		}
    
            private void girl_intro_H_loaded(object sender, RoutedEventArgs e)
            {
                MultiPointSDK.Instance.Initialize(this);
                this.KeyDown += new KeyEventHandler(end_keydown);
                MultiPointSDK.Instance.DeviceArrivalEvent += new EventHandler<DeviceNotifyEventArgs>(MultiPointObject_DeviceArrivalEvent);
                for (int i = 0; i < MultiPointSDK.Instance.MouseDeviceList.Count; i++)
                {
                    // Act on each individual MouseDevice here
                    DeviceInfo mouseObject = (DeviceInfo)MultiPointSDK.Instance.MouseDeviceList[i];
                    MultiPointMouseDevice mpMouseDevice = (MultiPointMouseDevice)mouseObject.DeviceVisual;
                    mpMouseDevice.CursorImage = 
    **********************************************************************
    EXCEPTION HERE ConvertBitmapToBitmapImage(GetCursorImage(i)); GOT AN ERROR HERE

    } **********************************************************************
    MultiPointSDK.Instance.DeviceRemoveCompleteEvent += new EventHandler<DeviceNotifyEventArgs>(this.MultiPointObject_DeviceRemoveCompleteEvent); EnforceMouseLimit(4); } private void MultiPointObject_DeviceArrivalEvent(object sender, DeviceNotifyEventArgs e) { // add code to handle the arrival of the device here if (e.DeviceInfo.DeviceType == DeviceType.Mouse) { DeviceInfo mouseObject = e.DeviceInfo; MultiPointMouseDevice mpMouseDevice = (MultiPointMouseDevice)mouseObject.DeviceVisual; Bitmap cursorBitmap = GetCursorImage(MultiPointSDK.Instance.MouseDeviceList.Count); mpMouseDevice.CursorImage = ConvertBitmapToBitmapImage(cursorBitmap); } } private Bitmap GetCursorImage(int id) { switch (id) { case 0: return Properties.Resources.boy_01; case 1: return Properties.Resources.boy_06; case 2: return Properties.Resources.boy_08; case 3: return Properties.Resources.girl_02; default: return Properties.Resources.girl_02; } } private void EnforceMouseLimit(int max) { // Too many mice - disable extras if (MultiPointSDK.Instance.MouseDeviceList.Count > max) { // Disable extra mice. for (int i = max; i < MultiPointSDK.Instance.MouseDeviceList.Count; i++) { DeviceInfo mouseObject = (DeviceInfo)MultiPointSDK.Instance.MouseDeviceList[i]; MultiPointMouseDevice mpMouseDevice = (MultiPointMouseDevice)mouseObject.DeviceVisual; mpMouseDevice.Visible = false; } } } private void MultiPointObject_DeviceRemoveCompleteEvent(object sender, DeviceNotifyEventArgs e) { // Ensure that the device that was unplugged was a mouse device if (e.DeviceInfo.DeviceType == DeviceType.Mouse) { if (MultiPointSDK.Instance.MouseDeviceList.Count < 3) { MessageBox.Show("Please connect 4 mice and restart the application"); App.Current.Shutdown(); } } } public static BitmapImage ConvertBitmapToBitmapImage(System.Drawing.Bitmap b) { BitmapImage bmpimg = new BitmapImage(); System.IO.MemoryStream memStream = new System.IO.MemoryStream(); bmpimg.BeginInit(); b.MakeTransparent(System.Drawing.Color.White); b.Save(memStream, System.Drawing.Imaging.ImageFormat.Png); bmpimg.StreamSource = memStream; bmpimg.EndInit(); return bmpimg; } private void end_keydown(object sender, KeyEventArgs e) { if (e.Key == Key.Escape) { App.Current.Shutdown(); } } } }

    Saturday, April 10, 2010 10:24 AM

All replies

  • The line you refer to have the error has nothing to do with the thread tile.
    How far do you go inside ConverBitmapToBitmapImage method?
    Bigsby, Lisboa, Portugal
    O que for, quando for, é que será o que é...
    Wenn ist das Nunstruck git und Slotermeyer? Ja! ... Beiherhund das Oder die Flipperwaldt gersput!
    http://bigsby.eu
    Saturday, April 10, 2010 6:24 PM
  • But when navigating from another Window to this one, it shows an exception here and highlights the above line in yellow.
    Sunday, April 11, 2010 12:56 AM
  • Add a breakpoint to the start of GetCursorImage(int id) and to the start of  ConvertBitmapToBitmapImage(System.Drawing.Bitmap b) and F10 from then on to see how far it goes. You can also enclose these methods code in try/catch and see what comes out. Apparently, your code looks good. It might be something related to cross-thread access or somethin of that kind.
    Bigsby, Lisboa, Portugal
    O que for, quando for, é que será o que é...
    Wenn ist das Nunstruck git und Slotermeyer? Ja! ... Beiherhund das Oder die Flipperwaldt gersput!
    http://bigsby.eu
    Sunday, April 11, 2010 10:23 PM
  • Hello,

    Beside what Bigsby suggested,I can find some similar topics about this issue, it could be related to un-properly closing of a form, this one might help:

    http://social.msdn.microsoft.com/forums/en-US/wpf/thread/0f7ffa69-4b43-421d-894b-aa5d6ef7b678/

    If you could give us a snippet that could repro this issue, it would be great.

    Thanks.

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, April 12, 2010 9:32 AM