Problem with Event and Timer
-
I developed a SMS application, I use a timer to pool the modem every now and then for new responses if a response is found I launch an ResponseReceived event, then that event looks for the type of response, then when a new message is detected I launch the NewMessageReceived event from the ResponseReceived event. The main application receives this event and opens a new dialog window, but the problem is that it does not show the window. I can see the window in the taskbar but not on the screen. I tried forcing the window to show, go topmost, maximize, every thing, but no window. I suspect that this could be due to threading of the timer or something, can anyone help? Leon v Wyk
-
I developed a SMS application, I use a timer to pool the modem every now and then for new responses if a response is found I launch an ResponseReceived event, then that event looks for the type of response, then when a new message is detected I launch the NewMessageReceived event from the ResponseReceived event. The main application receives this event and opens a new dialog window, but the problem is that it does not show the window. I can see the window in the taskbar but not on the screen. I tried forcing the window to show, go topmost, maximize, every thing, but no window. I suspect that this could be due to threading of the timer or something, can anyone help? Leon v Wyk
Leon v Wyk wrote: I suspect that this could be due to threading of the timer or something, can anyone help? You're right: see the docs for the InvokeRequired property, and you'll understand more. Simply put, you can only do UI tasks on the main thread. There's an "Invoke" method and an "InvokeRequired" property on all controls and forms that ease things for you. Yes, even I am blogging now!
-
Leon v Wyk wrote: I suspect that this could be due to threading of the timer or something, can anyone help? You're right: see the docs for the InvokeRequired property, and you'll understand more. Simply put, you can only do UI tasks on the main thread. There's an "Invoke" method and an "InvokeRequired" property on all controls and forms that ease things for you. Yes, even I am blogging now!
Thank you so much! How do I send variables to the method that I invoke?
private delegate void NewSMSReceived(); protected void OnNewSMSReceived() { int Position = 1; Solutions.Gsm.Modem.SMSMessageReceive SMS = Gsm.ReadSms(Position,true); SMSReceivedForm NewSMS = new SMSReceivedForm(SMS); NewSMS.ShowDialog(); } private void Gsm_NewSMSReceived(object sender, Being.IT.Solutions.Gsm.Modem.NewSMSEventArgs e) { lbInfo.Text = "New SMS received @ "+ DateTime.Now.ToString(); Solutions.Multimedia.SoundPlayer.Play(Application.StartupPath+@"\NewSMSNotify.wav"); //e.StoragePosistion this.BeginInvoke( new NewSMSReceived( this.OnNewSMSReceived) ); }
Leon v Wyk -
Thank you so much! How do I send variables to the method that I invoke?
private delegate void NewSMSReceived(); protected void OnNewSMSReceived() { int Position = 1; Solutions.Gsm.Modem.SMSMessageReceive SMS = Gsm.ReadSms(Position,true); SMSReceivedForm NewSMS = new SMSReceivedForm(SMS); NewSMS.ShowDialog(); } private void Gsm_NewSMSReceived(object sender, Being.IT.Solutions.Gsm.Modem.NewSMSEventArgs e) { lbInfo.Text = "New SMS received @ "+ DateTime.Now.ToString(); Solutions.Multimedia.SoundPlayer.Play(Application.StartupPath+@"\NewSMSNotify.wav"); //e.StoragePosistion this.BeginInvoke( new NewSMSReceived( this.OnNewSMSReceived) ); }
Leon v WykLeon v Wyk wrote: //e.StoragePosistion this.BeginInvoke( new NewSMSReceived( this.OnNewSMSReceived) ); This becomes (I removed all the 'this.', I find it weird): Invoke(new MethodInvoker(OnNewSMSReceived), new object[] { your variables go here }); Other alternative would be using properties, since you're on different threads, but will be invoking methods on the same object ('this.'). Yes, even I am blogging now!