Solved

Getting error on WPF application?

Posted on 2014-12-24
5
149 Views
Last Modified: 2014-12-29
I'm writing a test application using WPF (which I've never used before). I'm getting the following error on the MainWindow_Loaded() Event.

"MainWindow_Loaded() - Object reference not set to an instance of an object."

I'm guessing WPF forms don't initialize like a Windows Form and I've found that I can't just double-click on the WPF Form and have the "MainWindow_Loaded()" Event appear in the Code-behind. Here is my code below:

     Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
        Try
            tmr.Interval = 1
            EyeClosed.Visibility = Windows.Visibility.Hidden

        Catch ex As Exception
            MessageBox.Show("MainWindow_Loaded() - " & ex.Message)
        End Try
    End Sub

Open in new window

0
Comment
Question by:BlakeMcKenna
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40516837
Hi BlakeMcKenna;

It would help to know on what line in the code you get the exception "Object reference not set to an instance of an object." on?
0
 

Author Comment

by:BlakeMcKenna
ID: 40516989
Actually Fernando...I'm not getting that error now, rather I'm getting a different one. I've added a Timer.Timer control to the app. I've noticed that the Timer control for a WPF app is different from a Windows Timer control in that the WPF TC doesn't have the Tick() Event only the Elapsed() Event. I set the "Interval" property to 1. The new error I'm betting occurs in the Elapsed() Event.

Class MainWindow 
    Private WithEvents tmr As New Timers.Timer

    Private blnStartBlinking As Boolean = False
    Private blnBlink As Boolean = False
    '
    '
    '
    Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
        Try
            LEyeClosed.Visibility = Windows.Visibility.Collapsed
            REyeClosed.Visibility = Windows.Visibility.Collapsed

        Catch ex As Exception
            MessageBox.Show("MainWindow_Loaded() - " & ex.Message)
        End Try
    End Sub

    Private Sub tmr_Elapsed(sender As Object, e As Timers.ElapsedEventArgs) Handles tmr.Elapsed
        Try
            If blnBlink Then
                blnBlink = False
                LEyeOpened.Visibility = Windows.Visibility.Hidden
                LIris.Visibility = Windows.Visibility.Hidden
                LPupil.Visibility = Windows.Visibility.Hidden
                LEyeClosed.Visibility = Windows.Visibility.Visible

                REyeOpened.Visibility = Windows.Visibility.Hidden
                RIris.Visibility = Windows.Visibility.Hidden
                RPupil.Visibility = Windows.Visibility.Hidden
                REyeClosed.Visibility = Windows.Visibility.Visible
            Else
                blnBlink = True
                LEyeClosed.Visibility = Windows.Visibility.Hidden
                LEyeOpened.Visibility = Windows.Visibility.Visible
                LIris.Visibility = Windows.Visibility.Visible
                LPupil.Visibility = Windows.Visibility.Visible

                REyeClosed.Visibility = Windows.Visibility.Hidden
                REyeOpened.Visibility = Windows.Visibility.Visible
                RIris.Visibility = Windows.Visibility.Visible
                RPupil.Visibility = Windows.Visibility.Visible
            End If

        Catch ex As Exception
            MessageBox.Show("tmr_Elapsed() - " & ex.Message)
        End Try
    End Sub
    '
    '
    '
    Private Sub btnInitialize(sender As Object, e As RoutedEventArgs)
        Try
            tmr.Interval = 1

            LEyeClosed.Visibility = Windows.Visibility.Hidden
            LEyeOpened.Visibility = Windows.Visibility.Visible
            LIris.Visibility = Windows.Visibility.Visible
            LPupil.Visibility = Windows.Visibility.Visible

            REyeClosed.Visibility = Windows.Visibility.Hidden
            REyeOpened.Visibility = Windows.Visibility.Visible
            RIris.Visibility = Windows.Visibility.Visible
            RPupil.Visibility = Windows.Visibility.Visible

        Catch ex As Exception
            MessageBox.Show("btnInitialize() - " & ex.Message)
        End Try
    End Sub
    '
    '
    '
    Private Sub btnStartBlinking(sender As Object, e As RoutedEventArgs)
        tmr.Start()
    End Sub

    Private Sub btnStopBlinking(sender As Object, e As RoutedEventArgs)
        tmr.Stop()
    End Sub
End Class

Open in new window


The error message is:  "tmr_Elapsed() - The calling thread cannot access this object because a different thread owns it."
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40517029
I believe you should be using the DispatcherTimer Class in WPF. The timer you are using is the System Timer which is not running on the same thread. At the bottom of the page in the above link there is a sample of how to set it up.
0
 

Author Closing Comment

by:BlakeMcKenna
ID: 40521892
Thanks Fernando...that was the issue...
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40521893
Not a problem, glad to help.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question