I’ve been stuck on this all morning, even though it seems like it should be very easy (wondering if I’m missing something fundamental). I have the following code in a class-
public class myClass
{
private Dispatcher m_Dispatcher;
private void myMethod() { ... }
private void invokeTheMethod(object sender, PropertyChangedEventArgs e)
{
m_Dispatcher.Invoke(myMethod); //XYZ
}
}
The dispatcher is attached to the thread that instance of myClass is running on. The invokeTheMethod method is called from another thread, and I’d like to run myMethod on the thread of m_Dispatcher. However, if I try to run this code, I get an exception at XYZ saying “Object reference not an instance of an object”. Is this because I haven’t declared myMethod in the form of a delegate? – I have tried different ways to declare myMethod as a delegate, but I can’t get any of them to compile. Any suggestions are very much appreciated.
Thanks,
Chris
The error you get
refers to the field
m_Dispatcher. It is null. That is why you cannot call theInvokemethod on it.Even if there is an instance of
Dispatcher“attached to the thread”, there is no way formyClassto get hold of that instance.What you could do is to supply the instance of
DispatchertomyClasswhen you create an instance ofmyClass. Something like this:As a side note, you should read up some on coding conventions as your casing is considered wrong by the majority of the C# development community. Here is a good start: http://msdn.microsoft.com/en-us/library/vstudio/w2a9a9s3.aspx