Resolved How can i change Timers to Eventhandlers?

algebgeo

Member
Joined
May 9, 2020
Messages
8
Programming Experience
1-3
I have a variable that changes by external device which is a camera that detect faces this variable is an array of bytes which is always zero when no face is detected but when face is detected the bytes that describes the face is stored in this variable i am currently using an approach which i don't like that is uisng a timer

C#:
timer = new DispatcherTimer();

timer.Interval = TimeSpan.FromMilliseconds(0);

timer.Tick += Timer_Tick;

timer.IsEnabled = true;

timer.Start();



private void Timer_Tick(object sender, EventArgs e)

{

      FaceCamera.Authenticate(cameraAddress, faceCode);

}

i want to use other way i am open to any suggestion.

Thank you in advance.
 
Well, if that's the only API available to you, and your company won't let you start from scratch, then you have on choice but to poll.
 
One option open to you is to create your own class that does the polling internally and then raises an event when it detects a change. That class might have a property to set the polling interval, to enable you to balance resource usage and timely notification. You could then use that class anywhere you liked and handle its event as you would any other. As suggested, if the author of the library you're using offers no alternative, polling is what you must do. Encapsulating it in your own class can make the rest of your application code cleaner though, especially if you need to get this notification in multiple places. You might put that type in your own library and then use it in multiple applications.

That said, you should also be sure to check for an asynchronous API with a callback. Events are a concept in higher-level languages that doesn't exist in C/C++. The more primitive equivalent is a callback. In such cases, you would write a method that you want executed when something happens in unmanaged code, create a delegate for that method and then pass that delegate as an argument to an unmanaged method. The unmanaged signature for such a method would have a function pointer as a parameter. When your app was running, the unmanaged code would then execute your method via the delegate when the thing of interest occurred. There's no guarantee that such a method exists but be sure to look for one.

This is basically how events work in .NET. An event is a delegate list and the object raising the event invokes each of the event handlers via those delegates. For instance, if you have a Button on a form and the form handles the Click event of that Button, you write a method in the form, create a delegate for that method and pass the delegate to the Button. When the user clicks the Button, it invokes the delegate and the form method is executed.
 
Thank you for your help and replies.

I sent to the company and i told them that i have no choice but to poll the apis they replied ok we have no problem to poll the apis then thats the approach that i will use with my application and my application is for testing only i will try to solve the cpu usage problem when i create a real product for this camera.

Thanks again for your replies and help i wrote this question on other forums but no one replied me and they almost blocked me but in this forum i found a solution to my problem so thanks to this forum and its members.
 
i will try to solve the cpu usage problem when i create a real product for this camera.
That's very unlikely. Its the DLL that's the CPU hog. You said you decompiled the DLL, if so, did you not notice anything that could cause CPU intensity? Why its a hog is anyone's guess. Is there anything in the sample code that could cause CPU hikes? We are all x C++ programmers here, afaik. Sharing that sample might help. If you don't want to, that's up to you. :)
Thanks again for your replies and help i wrote this question on other forums but no one replied me and they almost blocked me but in this forum i found a solution to my problem so thanks to this forum and its members.
You're very welcome.
 
I am not allowed to share anything related to this project i don't know why its not a special code but thats what the company told me maybe because they paid for it.

I removed the Camera images stream from the application it made the application look ugly as the user will not see any images for his face but the program does its job for detecting faces and the cpu usage decreased from 15% to 4%
 

Latest posts

Back
Top Bottom