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.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,130
Location
Sydney, Australia
Programming Experience
10+
If you want to use an event handler then you need an event to handle. Does the object referred to by that FaceCamera field/property expose any events? Could you perhaps let us know what it actually is so we might investigate it for ourselves? We can't advise you specifically how to use an object when we don't know what that object is.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,749
Location
Chesapeake, VA
Programming Experience
10+
Perhaps I'm missing something... Isn't Timer_Tick() already an event handler, albeit an event handler for the timer. Is the question about how to get an event from FaceCamera? If so, then why not register for an event from that device? If the device offers no such event, then you're pretty much up a creek without a paddle. Your only alternative at that point is to write a wrapper around the device, setup a timer that occasionally checks, and then fire your own events when a face is recognized.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,130
Location
Sydney, Australia
Programming Experience
10+
Perhaps I'm missing something... Isn't Timer_Tick() already an event handler, albeit an event handler for the timer.
My impression that the OP was asking how to be notified by the object itself when something happens rather than using a Timer in order to poll the object periodically to see whether that something has happened. Rather like handling a TextChanged event rather than using a Timer to periodically check the Text property and see whether it has changed.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,130
Location
Sydney, Australia
Programming Experience
10+

algebgeo

Member
Joined
May 9, 2020
Messages
8
Programming Experience
1-3
Thank you for your replys
the DLL is not .net DLL it is a unmanged C++ DLL i don't have the documentation of it or contact with the creator of the DLL. so is using WPF dispatcher timer a bad thing that i should avoid and if so what is the better aproach.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,749
Location
Chesapeake, VA
Programming Experience
10+
And what library is that? Can you provide a link to where you go the DLL?

If you don't have documentation, how did you know to call Authenticate() on it?
 

algebgeo

Member
Joined
May 9, 2020
Messages
8
Programming Experience
1-3
The creator of the DLL created a sample C++ application which have the function used in the DLL so i knew from this sample the APIs to use from this DLL and in this sample application they are using also a Timer i guess it was a Win32 SetTimer function so i got that from this sample application i converted this sample app to C# WPF APP after a while i thought of changing the dispatchertimers to something that will be faster and not resources consuming
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,749
Location
Chesapeake, VA
Programming Experience
10+
Well, then you are basically up that creek I was talking about earlier. Since you don't know any other way to get the FaceCamera to tell you went something has happened, you are stuck in a situation where you have to poll the FaceCamera.

The WPF dispatch timers are not quite like the WinForms/Win32API timers. The former is based on the .NET Framework's threading timers while the latter is based on the Windows GDI timer objects. The latter is limited resource and relatively expensive. The former is pretty cheap.
 

algebgeo

Member
Joined
May 9, 2020
Messages
8
Programming Experience
1-3
Thank you for your help so you think that i have to poll the API as the C++ sample application do but the problem is when i run the app and check the cpu usage i find it reaches 15% i think its quite cpu consuming for a small application that does not thing except detecting faces and showing images stream from the camera.
 

NoUserHere

Well-known member
Joined
Sep 5, 2018
Messages
2,138
Programming Experience
10+
I'd be iffy about using any DLL that uses 15% CPU. And that's assuming your PC is relatively modern.

Why did you settle on this specific library?
Why settle on a DLL that has almost no instructions?
Why settle on this DLL when there are many libraries out there for the choosing?

In this day and age there is a library for almost everything. Why not use something else?
What does this library do that others do not?

It really sounds like you need to put more research into what you're trying to do. If this is for face detection, you can find many other sources all across the net, and most of which will be documented. . .


I suggest you search up other alternatives.
 

algebgeo

Member
Joined
May 9, 2020
Messages
8
Programming Experience
1-3
I am a new employee in a company this company some how bought this DLL with some camera devices that rely on this DLL they hired me and gave me this camera device and the DLL that relys on and they want a c# app for this specific camera with this specific DLL i argued with them that we can create our own apis that we can control but they didn't agree or they don't want to pay more for this camera they only need a C# app for the camera with this DLL so i do what they asked for the c# application is working perfect but the problem of CPU usage is what annoying me i can lower the cpu usage by removing capturing images stream from the camera as it is not important for face detection apis.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,749
Location
Chesapeake, VA
Programming Experience
10+
Time to learn some assembly programming and reverse engineer the C++ unmanaged DLL to see how that DLL works. Perhaps you may discover it has other entry points. Perhaps one of the other entrypoints is a way to register a callback so that you can get the effect of events which is what you are looking for. That's at best. At worse, you'll understand why the DLL is such a CPU hog.

I have a feeling that software that your company bought was this seemingly defunct FaceCode: FaceCode. Get the software safe and easy.
 

algebgeo

Member
Joined
May 9, 2020
Messages
8
Programming Experience
1-3
I already spent a while doing a reverse engineer to this DLL and i didn't find any more APIs than what they are using in their sample C++ Application and the software that my company bought is from an unknown company and the camera device there is no thing on it no company name or anything it is like a black box
 
Last edited:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,749
Location
Chesapeake, VA
Programming Experience
10+
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.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,130
Location
Sydney, Australia
Programming Experience
10+
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.
 

algebgeo

Member
Joined
May 9, 2020
Messages
8
Programming Experience
1-3
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.
 

NoUserHere

Well-known member
Joined
Sep 5, 2018
Messages
2,138
Programming Experience
10+
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.
 

algebgeo

Member
Joined
May 9, 2020
Messages
8
Programming Experience
1-3
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%
 
Top Bottom