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
-
I am trying to download a calendar into ical and keep getting this message-Some of the events in this calendar have alerts that open files or applications. Remove alerts or cancel
What am I doing wrong or missing??I have this same problem in October of 2013. I am unable to import .ics files into Lion that I exported from my Snow Leopard MacBookPro without losing all of my future events (which all have alerts attached). All of the events from BEFORE today's date are successfully importing, presumably because they are no longer expected to trigger alerts via Apple Mail. After my first unsuccessful attempt, I learned that iCal was communicating with iCloud by default. I have since turned off iCloud, but I have not found a solution to the problem. Harvey
-
In downloading my Events from iPhoto on my Mac to my Ipad Air, the Events arrive in Photos in the correct order but not showing the Key photo. At the same time, the Events arrive in iPhoto in a totally random order but with the Key phto showing. Why? of course and is there a way to shift the order in Iphoto and get a Key photo in Photos?
HHi, thank you for the reply. I have checked my iPad and iPhone and neither has iCloud Photo Library (Beta) enabled. Turned off in both. Photostream is turned on.
i tried to sort it out by dragging all the photos to Events on the Mac and then deleting them from iCloud - (left hand side of iPhoto under the section 'Shared'). the photos now show up in Events. I did force quit but the issue remains. The message reads ' photos are bing imported to the library. Please wait for import to complete.'
i can't empty iPhoto trash either. The message read "Delete error. Please wait for import to complete.'
WHen I was moving the photos to the Events I always had a message about duplicates - to the effect that the photos already existed, did I want to import them? I clicked on Yes, import all duplicates. But when it showed the images - duplicates side by side - one showed the photo and the other was blank.
I really don't know what to do! And I don't know how to handle my iOS devices. Is it to do with the large number of photos? Any help, advice appreciated. -
I imported about 300 RAW images yesterday and worked with them for about an hour. iPhoto locked up on me and I rebooted. After I did the event icon for that event is now gray and it says there are no photos. I know the photos are there. Any idea how I can recover them?
a best practice to is to never have any computer program (including iPhoto) delete the photos from the card but to import the photos and keep them and then after at least one successful backup cycle has completed and then reformat the card -- I use three very large (32 GB) cards in rotation so I do not reformat for typically a year or more giving me one more long term backup of my photos
Back up your iPhoto library, Depress and hold the option (alt) and command keys and launch iPhoto - rebuild your iPhoto library database
LN -
i am trying to upgrade Mac OSX from 10.7.4 10 .7.5 .But, the grey screen is coming for hours. I pressed the alt key and was taken to options viz. Restart to install MAC again, use time machine. I don't have back-up and so restarted to install MAC again. Can i cancel this now. I want my data to be secure. What should i do?
If you cancel an install there's no telling what state it will leave your Mac in. Let it finish or fail.
Then get a disk, it can even be a pocket drive if it's large enough and back up all your own data.
After that, boot up holding command-r keys into your Recovery Volume.
The screen should give you these choices...
Restore from TM backup
Reinstall Mac OS X
Get help online
Disk Utility
First run Disk Utility Repair Disk, if you get errors run until no errors reported or reports "the disk cannot be fixed". Then, choose Reinstall OS X. -
I have tried to changed my Apple ID and every time I try to use the new Apple ID it won't let me because when i try to update an app the old ID comes up to sign in. I deleted that account so it will not let me use my new one.
Content and Apple IDs -
Content is forever tied to the Apple ID that bought it. Apple does not transfer content from one Apple Id to another. Apple does not merge Apple IDs. You will never be able to access your content bought with another Apple ID with a new ID. -
My old laptop broke so I got a new one. I am trying to install Photoshop elements 7 and am entering my serial number but it isn't accepting it. How do I fix this?
There is no serial number for CC Cloud versions. They are authenticated via your Adobe ID.
Sign in to your Cloud account with your Adobe ID and download the software via the Cloud app. -
I have a brand new mini mac and a new apple TV. Both have been updated and can see my movies in itunes. I can use Netflix and stream music. I have tried to use my old laptop and it streams video fine. But my new computer only see a spinning circle.
Please read this whole message before doing anything.
This procedure is a diagnostic test. It won’t solve your problem. Don’t be disappointed when you find that nothing has changed after you complete it.
The purpose of this test is to determine whether the problem is localized to your user account. Enable guest logins* and log in as Guest. Don't use the Safari-only “Guest User” login created by “Find My Mac.”
While logged in as Guest, you won’t have access to any of your personal files or settings. Applications will behave as if you were running them for the first time. Don’t be alarmed by this; it’s normal. If you need any passwords or other personal data in order to complete the test, memorize, print, or write them down before you begin.
Test while logged in as Guest. Same problem?
After testing, log out of the guest account and, in your own account, disable it if you wish. Any files you created in the guest account will be deleted automatically when you log out of it.
*Note: If you’ve activated “Find My Mac” or FileVault in OS X 10.7 or later, then you can’t enable the Guest account. The "Guest User" login created by "Find My Mac" is not the same. Create a new account in which to test, and delete it, including its home folder, after testing. -
Hi, I have just tried to update the iPhone software and it appears to be updating but the screen on my phone has had the same picture of the apple and its at the same spot of loading (about 3 quarters) for the last HOUR!! help!
oh and the device is not being recognised by itunes now either.
-
I am trying to delete a page from a pdf using Acrobat X Pro but the delete option is greyed out. Does anyone know why?
Post your question in the forum for Adobe Acrobat.
-
I have been using the apple IPad camera connection kit and my photos can be viewed in the events... But the all imported folder is no longer there and the last import folder is empty. Is there any way to resolve this and get my all imported folder back? as well as get my last import functioning normally?
Please please help me, if you know how.
-
I have atempted to gift a song to someone. I have been charged for it but it was not received. I tried to re-gift it and was charges a second time but the recipient still has not received it. How can I get the song to her or my money back?
First, have the recipient check any anti-spam filters that may be present on her email account or in any email application to make sure that messages from Apple aren't being blocked. If she is sure that is not the case, go here:
http://www.apple.com/support/itunes/contact/
and follow the instructions to report the issue to the iTunes Store.
Regards. -
My daughter locked her Ipad so I restored it and now am trying to activate it. It ask for a apple id/pw but the one she thought it was is not working. Any suggestions?
Activation Lock
http://support.apple.com/kb/PH13695 -
I just tried to update my iPhone 4 and it said it was successful, but now I have a message (USB icon - arrow- iTunes icon) that is frozen? I have turned off the phone, unplugged USB and reconnected and closed out of iTunes and back in. Any suggestions?
Hello, Spedellen.
Thank you for visiting Apple Support Communities.
When experiencing issues restoring an iPhone, here are the best articles to go through. If you received a specific error number when restoring, see the section labeled Get more help in the second article below.
If you can't update or restore your iOS device
http://support.apple.com/kb/ht1808
Resolve iOS update and restore errors in iTunes
http://support.apple.com/kb/ts1275
Cheers,
Jason H. -
I am not able to install apps from the Appstore in Iphone 5c. I tried to switch off my phone and switch it on again. But it still doesnt work
Hello stuti1200
Check out the troubleshooting article below for issues with access to the iTunes Store. Also there was a small amount of outage accessing the iTunes Store this past Wednesday.
Can't connect to the iTunes Store
http://support.apple.com/kb/TS1368
Apple Services, Stores, and iCloud
http://www.apple.com/support/systemstatus/
Thanks for using Apple Support Communities.
Regards,
-Norm G.
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
-
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
-
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