Solved

Getting error on WPF application?

Posted on 2014-12-24
5
137 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
  • 3
  • 2
5 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
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
Comment Utility
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 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
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
Comment Utility
Thanks Fernando...that was the issue...
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Not a problem, glad to help.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now