Main Site Documentation

A first chance exception of type 'System.InvalidOperationException' occurred in Microsoft.SPOT.TinyCore.dll Exception was thrown: System.InvalidOperationException


#1

Hi

i added to Image One for Plugged and one for Unplugged to check the network status

first off all the ethernet_NetworkUp is never gonna show up and i don’t know why but ethernet_NetworkDown is working fine

and i got an error : A first chance exception of type ‘System.InvalidOperationException’ occurred in Microsoft.SPOT.TinyCore.dll Exception was thrown: System.InvalidOperationException

for this line of code :

       void Interface_CableConnectivityChanged(object sender, EthernetBuiltIn.CableConnectivityEventArgs e)
        {
            try
            {
                if (e.IsConnected)
                {
                    Plugged.Visibility = Visibility.Visible;
                    UnPlugged.Visibility = Visibility.Hidden;
                }
                else
                {
                    Plugged.Visibility = Visibility.Hidden;
                    UnPlugged.Visibility = Visibility.Visible;
                }
            }
            catch (Exception ex) { Debug.Print(ex.Message); }

        }

my whole code :

using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using GHI.Premium.Net;

namespace NetWatch
{
    public partial class Program
    {
        #region Local Varaibles
        static int top1 = 0;
        static int top2 = 55;
        static int top3 = 100;

        Canvas canvas1;
        Canvas canvas2;
        Canvas canvas3;

        Font myFontB;   //NinaB
        Font myFontS;   //small

        Window lcdWindow1;
        Window lcdWindow2;
        Window lcdWindow3;
        Text IP;
        Text Mask;
        Text Gateway;
        Text NetStatus;
        Text Cable;

        Image Plugged = new Image(new Bitmap(Resources.GetBytes(Resources.BinaryResources.Plugged), Bitmap.BitmapImageType.Bmp));
        Image UnPlugged =  new Image(new Bitmap(Resources.GetBytes(Resources.BinaryResources.UnPlugged), Bitmap.BitmapImageType.Bmp));
        #endregion
        void ProgramStarted()
        {
            myFontS = Resources.GetFont(Resources.FontResources.small);
            myFontB = Resources.GetFont(Resources.FontResources.NinaB);
            /*******************************************************************************************
                GT.Timer timer = new GT.Timer(1000); // every second (1000ms)
                timer.Tick +=<tab><tab>
                timer.Start();
            *******************************************************************************************/
            LCD_BootUp();
           // Plugged = new Image(new Bitmap(Resources.GetBytes(Resources.BinaryResources.Plugged), Bitmap.BitmapImageType.Bmp));
            //UnPlugged = new Image(new Bitmap(Resources.GetBytes(Resources.BinaryResources.UnPlugged), Bitmap.BitmapImageType.Bmp));
            //UnPlugged = new Image ( Resources.GetBytes(Resources.BinaryResources.UnPlugged));
            canvas1.Children.Add(Plugged);
            canvas1.Children.Add(UnPlugged);
            PositionElemen(Plugged, 280, 0);
            PositionElemen(UnPlugged, 280, 0);
            Plugged.Visibility = Visibility.Hidden;
            NetStatus = new Text(myFontB, "Welcome! Welcome! Welcome!");
            IP = new Text(myFontB, "Welcome! Welcome! Welcome!");
            Mask = new Text(myFontB, "Welcome! Welcome! Welcome!");
            Gateway = new Text(myFontB, "Welcome! Welcome! Welcome!");

            //Hello.ForeColor = GT.Color.Black;


            //canvas1.Children.Add(Hello);
            //PositionElemen(canvas1, 10, 10);

            multicolorLed.TurnRed();
            ethernet.Interface.CableConnectivityChanged += Interface_CableConnectivityChanged;
            ethernet.Interface.NetworkAddressChanged += Interface_NetworkAddressChanged;

            ethernet.Interface.Open();

            NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet.Interface);
            if (!ethernet.Interface.NetworkInterface.IsDhcpEnabled)
            { ethernet.Interface.NetworkInterface.EnableDhcp(); }

            //
            //ethernet.Interface.NetworkInterface.EnableStaticIP("172.16.43.176", "255.255.255.0", "172.16.43.254");
            ethernet.NetworkUp += ethernet_NetworkUp;
            ethernet.NetworkDown += ethernet_NetworkDown;
        }



        #region Network Events
        void ethernet_NetworkDown(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        {
            Debug.Print("Network is Down!");
        }

        void ethernet_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        {
            Debug.Print("Network is UP!");
        }

        void Interface_NetworkAddressChanged(object sender, EventArgs e)
        {

        }

        void Interface_CableConnectivityChanged(object sender, EthernetBuiltIn.CableConnectivityEventArgs e)
        {
            try
            {
                if (e.IsConnected)
                {
                    Plugged.Visibility = Visibility.Visible;
                    UnPlugged.Visibility = Visibility.Hidden;
                }
                else
                {
                    Plugged.Visibility = Visibility.Hidden;
                    UnPlugged.Visibility = Visibility.Visible;
                }
            }
            catch (Exception ex) { Debug.Print(ex.Message); }

        }
        #endregion
        #region LCD
        private void PositionElemen(UIElement element, int x, int y)
        {
            Canvas.SetTop(element, y);
            Canvas.SetLeft(element, x);
        }
        public void LCD_BootUp()
        {

            lcdWindow1 = display.WPFWindow;
            lcdWindow1 = new Window();
            lcdWindow1.Width = display.WPFWindow.Width;
            lcdWindow1.Height = display.WPFWindow.Height;
            canvas1 = new Canvas();
            canvas1.Width = (int)lcdWindow1.Width;
            canvas1.Height = (int)lcdWindow1.Height;
            lcdWindow1.Child = canvas1;
            lcdWindow1.Top = top1;
            lcdWindow1.Background = new SolidColorBrush(Colors.White);
            lcdWindow1.Visibility = Visibility.Visible;
            //

            lcdWindow2 = display.WPFWindow;
            lcdWindow2 = new Window();
            lcdWindow2.Width = display.WPFWindow.Width;
            lcdWindow2.Height = display.WPFWindow.Height;
            canvas2 = new Canvas();
            canvas2.Width = (int)lcdWindow2.Width;
            canvas2.Height = (int)lcdWindow2.Height;
            lcdWindow2.Child = canvas2;
            lcdWindow2.Top = top2;
            lcdWindow2.Background = new SolidColorBrush(Colors.Yellow);
            lcdWindow2.Visibility = Visibility.Hidden;
            //

            lcdWindow3 = display.WPFWindow;
            lcdWindow3 = new Window();
            lcdWindow3.Width = display.WPFWindow.Width;
            lcdWindow3.Height = display.WPFWindow.Height;
            canvas3 = new Canvas();
            canvas3.Width = (int)lcdWindow3.Width;
            canvas3.Height = (int)lcdWindow3.Height;
            lcdWindow3.Child = canvas3;
            lcdWindow3.Top = top3;
            lcdWindow3.Background = new SolidColorBrush(Colors.LightGray);
            lcdWindow3.Visibility = Visibility.Hidden;
        }
        #endregion
    }
}


#2

Can you please wrap code fragments with [ code ] [/ code] tags (no spaces). You can edit your previous post with the pencil button.


#3

yeah sure


#4

@ Alir3za - What line throws the exception?


#5

when i want to change the property of Plugged and UnPlugged
i got this error message :

if (e.IsConnected)
                {
                    Plugged.Visibility = Visibility.Visible;
                    UnPlugged.Visibility = Visibility.Hidden;
                }
                else
                {
                    Plugged.Visibility = Visibility.Hidden;
                    UnPlugged.Visibility = Visibility.Visible;
                }