Main Site Documentation

GlideLoader.LoadWindow outside of ProgramStarted()


#1

I want to dynamically load forms at runtime, anytime during the lifecycle of the application from a HTTP result (the forms are loaded from a web server).

They can change at any time.

I’ve tried loading GlideLoader.LoadWindow() in other places besides the ProgramStarted() method, and the form does load, but any events that I hook up throw this exception:

“An unhandled exception of type ‘System.NullReferenceException’ occurred in Microsoft.SPOT.TinyCore.dll”

When ran in ProgramStarted() everything runs fine.

Here’s the code I call when I get the window xml from the Http Request:

GlideTouch.Initialize();
				// Resize any loaded Window to the LCD's size.
				Glide.FitToScreen = true;

				// Load the Window XML string.
				// Resources.GetString(Resources.StringResources.Window)
				window = GlideLoader.LoadWindow(windowXml);

				// Assign the Window to MainWindow; rendering it to the LCD.
				InitWin(); // set up the form events
				Glide.MainWindow = window;

Any help would be appreciated.
Thanks,
Brian


#2

Could you create a small complete code sample showing the failure, so users can copy paste and analyze.


#3

You’ll need to have a web site that hosts the Window.txt file.
Here is the total Sample Code:

using System;
using System.Threading;

using Microsoft.SPOT;

using GHIElectronics.NETMF.Glide;
using GHIElectronics.NETMF.Glide.Display;
using GHIElectronics.NETMF.Glide.UI;
using Gadgeteer.Networking;

namespace GadgeteerApp1
{
	public partial class Program
	{
		static GHIElectronics.NETMF.Glide.Display.Window window;

		void ProgramStarted()
		{
			// This does not work (set up network, HTTP call to get Window.txt)
			// On this way, clicking the "toggleBtn" button throws exception:
			//		"An unhandled exception of type 'System.NullReferenceException' occurred in Microsoft.SPOT.TinyCore.dll"
			SetUpNetworking();
			// this does work (comment out the above line, uncomment the next line)
			//SetUpGlide(Resources.GetString(Resources.StringResources.Window));
		}



		private void SetUpGlide(string windowXml)
		{
			// set up window xml and events
			GlideTouch.Initialize();
			Glide.FitToScreen = true;
			window = GlideLoader.LoadWindow(windowXml);
			// Events
			camera.PictureCaptured += new Gadgeteer.Modules.GHIElectronics.Camera.PictureCapturedEventHandler(camera_PictureCaptured);
			Button toggleBtn = (Button)window.GetChildByName("toggleBtn");
			toggleBtn.TapEvent += new OnTap(toggleBtn_TapEvent);
			Glide.MainWindow = window;
		}

		#region SETUP NETWORKING
		private void SetUpNetworking()
		{
			ethernet_J11D.Interface.Open();
			ethernet_J11D.Interface.NetworkInterface.EnableDhcp();

			ethernet_J11D.Interface.NetworkAddressChanged += new GHI.Premium.Net.NetworkInterfaceExtension.NetworkAddressChangedEventHandler(Interface_NetworkAddressChanged);
			if (!ethernet_J11D.Interface.IsActivated)
			{
				GHI.Premium.Net.NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet_J11D.Interface);
			}
		}

		void Interface_NetworkAddressChanged(object sender, EventArgs e)
		{
			// once we have a network address, ask the HTTP for the Window.txt file
			Debug.Print(ethernet_J11D.Interface.NetworkInterface.IPAddress);
			HttpRequest r = WebClient.GetFromWeb("http://mymachine.mdomain.local/Window.txt");
			r.ResponseReceived += new HttpRequest.ResponseHandler(r_ResponseReceived);
		}

		void r_ResponseReceived(HttpRequest sender, HttpResponse response)
		{
			// when we get the window.txt file, set up the GLIDE window
			sender.ResponseReceived -= r_ResponseReceived;
			string rtext = response.Text;
			Debug.Print(rtext);
			SetUpGlide(rtext);
		}
		#endregion


		public void toggleBtn_TapEvent(object sender)
		{
			camera.TakePicture();
		}

		void camera_PictureCaptured(Gadgeteer.Modules.GHIElectronics.Camera sender, Gadgeteer.Picture picture)
		{
			Image image = (Image)window.GetChildByName("webcam");
			image.Bitmap = picture.MakeBitmap();
			image.Invalidate();
		}

	}
}

And the Window.Txt:

<Glide Version="1.0.5">
  <Window Name="window" Width="320" Height="240" BackColor="dce3e7">
    <Image Name="webcam" X="80" Y="40" Width="160" Height="120" Alpha="255"/>
    <Button Name="toggleBtn" X="80" Y="170" Width="160" Height="32" Alpha="255" Text="Start" Font="4" FontColor="000000"/>
  </Window>
</Glide>

#4

What line throws the exception?


#5

No line number at all. Just the “An unhandled exception of type ‘System.NullReferenceException’ occurred in Microsoft.SPOT.TinyCore.dll”, in a “Microsoft Visual Studio” dialog window with the buttons “Break”, “Continue” , “Ignore”.

If I choose “Break” from the exception popup window, I get a “No Source Available” window
if I choose Continue, I get the same “Microsoft Visual Studio” exception dialog again


#6

Here is what the Output window prints:

Uncaught exception
#### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (4) ####
#### Message:
#### Microsoft.SPOT.Application::OnEvent [IP: 0090] ####
#### Microsoft.SPOT.EventSink::ProcessEvent [IP: 0023] ####
#### Microsoft.SPOT.EventSink::EventDispatchCallback [IP: 0014] ####
A first chance exception of type ‘System.NullReferenceException’ occurred in Microsoft.SPOT.TinyCore.dll
An unhandled exception of type ‘System.NullReferenceException’ occurred in Microsoft.SPOT.TinyCore.dll


#7

Hit break and look in the call stack window to find what line in your program started the sequence which resulted in the exception.


#8

Hit break and look in the call stack window to find what line in your program started the sequence which resulted in the exception.


#9

Ok try this:

        void r_ResponseReceived(HttpRequest sender, HttpResponse response)
        {
            // when we get the window.txt file, set up the GLIDE window
            sender.ResponseReceived -= r_ResponseReceived;
            string rtext = response.Text;
            Debug.Print(rtext);

            if (Dispatcher.CurrentDispatcher.CheckAccess())
                SetUpGlide(rtext);
            else
                Dispatcher.CurrentDispatcher.Invoke(new TimeSpan(100), DispatcherCallBack, rtext);
        }

        object DispatcherCallBack(object arg)
        {
            SetUpGlide((string)arg);
            return null;
        }

#10

No CallStack line number. Just says “[External Code]”.


#11

there are not multiple lines in the call stack window?


#12

#13

OT: @ Mike I am having issues with double posts too. Hitting back after the error usually prevents the dp.


#14

(Guys, thanks for the help)

Mike: Correct, there are no line numbers at all in the call stack.

Architect:
I Tried the dispatcher.
CurrentDispatcher.CheckAccess() always runs the “SetUpGlide(rtext)” method (it is saying a Dispatcher is not required).
I skipped the CheckAccess and always ran the dispatcher, and it throws the same Unhandled Exception as before.


#15

One more thing I would try is to add Glide source project and would try to step through to find out more about the exception.


#16

As I was about to give up, I decided to just try moving things around until it worked.

I moved the GlideTouch.Initialize() to the ProgramStarted method. Aparently this cannot (or must be dispatched?) be called elsewhere.

Here is what worked:

[quote]void ProgramStarted()
{
// Moving these two lines from SetUpGlide() to ProgramStarted() is what let things work correctly.
GlideTouch.Initialize();
Glide.FitToScreen = true;
SetUpNetworking();
}[/quote]

Thanks to everyone for the help given. I would have given up much earlier had it not been for you guys.
Brian


#17

Cool! I thought about it, but got distracted by Dispatcher idea and didn’t follow up.