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.
 
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.
 
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.
 
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.
 
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.
 
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?
 
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
 
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.
 
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.
 
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.
 
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.
 
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.
 
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:

Latest posts

Back
Top Bottom