Here's a good one for everyone. I have been working on a Screensaver program for some time now (some of you may even remember questions related to this). Anyway, one of the biggest challanenges with developing it was finding a good way to transition images, smoothly, and not killing all my resources. I ended up using a WPF for this piece that is used to fade in and out images.
Once I had that little component developed, I created a new class, an object that inherited a Windows Form Panel, and then proceeded to add on a series of functions. This item includes housing the WPF I have build. The code looks like this (these are snippets)
//Fields setup for the Panel
private ElementHost WPFHost;
private ScreenSaverImageDisplay ImageDisplayer;
//Inside the custom Panel's initializer
this.ImageDisplayer = new ScreenSaverImageDisplay(new ImageFadeComplete(ImageFadeCompleteCheckIn));
this.ImageDisplayer.Size = new System.Windows.Size(Width, Height);
this.WPFHost = new ElementHost();
this.WPFHost.Location = new Point(0, 0);
this.WPFHost.Size = new Size(Width, Height);
this.WPFHost.Child = ImageDisplayer;
this.Controls.Add(WPFHost);
That's how I setup and initialize the thing, pretty simple.
However, while the code runs fine all the time when I debug through Visual Studio, there is this issue I have when I run it as an actual ScreenSaver (you know go to your properties choose it, and having it setup to start after so much time idle). The problem I am facing is that sometimes (I can't seem to find a pattern), is a null exception will be thrown on the start of the ScreenSaver. Again it doesn't happy everytime, and I can't even be sure if it only happens the first time it tries to run (that being like if I shut down the PC and start it back up, that would count as a first time when it runs the first time).
I get the usual exception window, and this
************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Windows.Forms.Integration.ElementHost.Select(Boolean directed, Boolean forward)
at System.Windows.Forms.Control.SelectNextControl(Control ctl, Boolean forward, Boolean tabStopOnly, Boolean nested, Boolean wrap)
at System.Windows.Forms.Form.set_Active(Boolean value)
at System.Windows.Forms.Form.WmActivate(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
I can not figure out what is causing this problem. This is all internal code, as I do not have any functions or anything like this. And like I said, I can never get this to occur when I am debugging so it makes it even more difficult to pin down.
I am hoping maybe someone has seen this before and knows what's causing it, because right now I am scratching my head. Null exceptions can be tricky, but this one is a stumper, even more because I have no clue where in my code this is occuring.
Thanks everyone for the help