I am trying to raise event at UserControl, and catch it at Main program, But the event always return null

I am trying to raise a event in one of classes of userControl, and Fire it in the Main class. I tried two different ways to fire this event, one of them works, But I still want to know why other way cannot work, and how to fix it.
My userContol class:
public partial class UserControl1 : UserControl
public UserControl1()
InitializeComponent();
if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
return;
Class1 c = new Class1();
Thread accept = new Thread(
() =>
c.connection();
accept.Start();
And the Class1:
public class Class1
public delegate void myhandler(object sender, EventArgs e);
public event myhandler test;
public Class1()
public void connection()
test(this, new EventArgs());
In the Main, I just simply add into referent, and add
xmlns:my="clr-namespace:WpfControlLibrary1;assembly=WpfControlLibrary1"
then I try to subscribe this event in the main
public partial class SurfaceWindow1 : SurfaceWindow
/// <summary>
/// Default constructor.
/// </summary>
public SurfaceWindow1()
InitializeComponent();
Class1 c = new Class1();
c.test+=new Class1.myhandler(c_test);
// Add handlers for window availability events
AddWindowAvailabilityHandlers();
public void c_test(object sender, EventArgs e)
MessageBox.Show("fire");
If I only raise this event not into thread, it works fine, but If I try to let it raise in this thread, this test event only return null, and shows:
Object reference not set to an instance of an object.
looks like I did not subscribe it ever. So How to fix it if I must use it in thread.

Subscribing to events window to class is not a great approach.
You have to then go un subscribe those handlers in order to allow your instance to be disposed.
Forget that and you'll eventually notice you have memory leaks.
The way I do this sort of thing is using mvvm light messenger.
You can keep everything decoupled then.
http://social.technet.microsoft.com/wiki/contents/articles/26070.aspx
I just did a bit of code for someone else which shows how to do cross thread stuff with this approach.
public partial class MainWindow : Window
public MainWindow()
InitializeComponent();
Messenger.Default.Register<String>(this, (action) => ReceiveString(action));
private void ReceiveString(string msg)
MessageBox.Show(msg);
Dispatcher.BeginInvoke((Action)delegate()
tb.Text = msg;
private void Button_Click(object sender, RoutedEventArgs e)
Task.Factory.StartNew(() => {
Messenger.Default.Send<String>("Hello World");
Note that the message arrives on the thread it was sent from. That's not the ui thread because it was sent from that task.factory.startnew to deliberately put it on a different thread.
In order to change UI controls, it uses dispatcher.begininvoke to run code on the UI thread.
Although this is in one piece of code behind publisher and subscriber can be in two totally different classes which have no reference of knowledge of each other.
Meaning you can send a message<t> from any class1 or whatever you like and your mainwindow can subscribe and act of receipt of a message<t>.
It is the type which defines which message one is. You put data you want to send in t and use it in the subscriber.
Hope that helps.
Technet articles: Uneventful MVVM;
All my Technet Articles

Similar Messages

Maybe you are looking for

  • Why we go for SAP ?what are the benifits to implement SAP inanyorganizat?

    Hi, Why we go for SAP ?what are the benifits to implement SAP in  any organization? Early reply is highely appruciable. Regards, Chow.

  • Macbook pro 13" CPU A Diode temperature

    Hi all, I have recentlypurchased MBP13 inches (Intel Core i5 2.4 ghz, 500 G HDD, Internationalkb). I observed a difference in CPU heating behaviour compared to mypreviously used MBP 15" (2008 version). So I did the following test: 1. CPU Temperature

  • IBook Video Adapter

    I just bought one of these things off eBay, but it doesn't seem to be working with my TV. When I plug it into the iBook, the iBook screen with fade into a blue color (which would make me think it's detecting it), and then comes back with what I was d

  • Java.lang.IllegalArgumentException: setAttribute: Atributo no serializable

    i am developer whit java 1.5 and using tomcat 5.5.9. the aplication run perfect in tomcat 1.4 but in 5.5.9 not. plis. some one can help me

  • Layouts in java

    Ive just created a simple program, using AWT GUI. I created two versions, each one using the "Border Layout" but done one extending a Panel and the other extending a Frame. I placed a TextArea in the south part of a panel, and then added the panel to