简体   繁体   中英

Event in winform - .NET Compact Framework 3.5

I am currently working on one of the functions in a mobile application which requires to continuously monitor if there are any bluetooth devices are out of range or not. If out of range, the corresponding button for the device should be disabled. One of the options I can think of is that I check the status of each connected bluetooth device periodically using the timer by sending out an event. If the periodical check indicates that one of the devices is out of range, the button for this device will be disabled. However, I cannot think of any method how to detect if it is out of range or not. In wpf, I have used caliburn which can send event in application wise. In other word, when the event is sent out, any part of the application receive this event as long as they subscribe to this event. Does anyone have any idea of how to achieve this?

Thanks for any help in advance.

Thanks,

Charles Lau

CF 3.5 has background workers (2.0 did not). You could create a backround worker that periodically checks the status of bluetooth devices and sends a ReportProgress event when the status of one changes. You can have whatever you need subscribe to the event.

The default usage of ReportProgress is to sent an int showing the progress of the background worker, however ReportProgress allows you to send an object as the second argument: http://msdn.microsoft.com/en-us/library/a3zbdb1t.aspx

If you haven't used the BackgroundWorker class before here's a quick guide: http://www.albahari.com/threading/part3.aspx

This will work with winforms nicely. It avoids the following pattern (for marshalling the call onto the UI thread):

 if (ctrl.InvokeRequired)
 {
     ctrl.Invoke(action);
 }
 else
 {
     action();
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM