简体   繁体   中英

Webcam frame event handler stop and resume c#

i have a problem with my program. I'm using Aforge for the video stream, ZXing for the qr code decoding part, c# and windows forms.

I have set up, in the initialize function, the event handler for each frame:

videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);

In the video_newFrame i have all the code i need in my application: it takes the frame, compute it, find the QR code, decode it, check "something" and then show a messageBox; and so on for every frame. My problem is: when i show the qr code, program computes it and the messagebox appears (with the ok button, so app should be stopped); if i get out of the camera field of view the paper with the qr code, i'm expecting that, when i will click on the ok button, it will go on in the code and the next frame will be empty (meaning without the paper with the qr code - so NO messagebox in the next frame!). But it isn't like that! The most of the times, the event handler has already fired up 1-2-3 call to the video_newframe, and that means that i will have another 1-2-3 messagebox. The code is this:

private void video_NewFrame2(object sender, NewFrameEventArgs eventArgs) 
{
Bitmap img = (Bitmap)eventArgs.Frame.Clone();
var result = reader.Decode(img);
if (result != null) {
    if (DialogResult.OK == MessageBox.Show("hello!"))
        // do something;
    img.Dispose(); }
}

I have tried different things: i've tried to unsubscribe from the event handler at the beginning and subscribe again at the end of video_newframe function:

videoSource.NewFrame -= video_NewFrame; // at the beginning, at first line
...
videoSource.NewFrame += video_NewFrame; // in the if of the DialogResult, in the "// do something"

but no results.

So i tried with a bool value: i added an if statement that check if the bool value is true and if it is, it puts "bool value = false" (to avoid the next event handling), it does the stuff and if value is false it returns; then, when it is doing the stuff, in the if of the DialogResult, in the "// do something", i've put "bool = true" again. No success for me.

So my problem still remains there: i just want the function to work always; but i want, somehow, that when it finds a result (a qr code) it stops firing events for just the time needed to show only one messagebox; so, for example, stop the event handling or something like that...

Hope you can help me! Thanks in advance! :)

EDIT after Bartosz answer:

first of all thank you! Now: i didn't know about singleton or what they are, so thank you also for the knowledge! Unfortunately, problem still remains (probably it is my fault in developing well the class); i have tried this:

public sealed class Singleton
{
    static readonly Singleton instance = new Singleton();
    static Singleton() { }
    Singleton() { }
    public static Singleton Instance
    {
        get
        {
            return instance;
        }
    }
    public bool alreadyWorking;
}

then, i made two test: make a global variable and use the Instance

Singleton test = Singleton.Istance; //don't know if this is good

and in the intialize method i have:

test.alreadyWorking = false; // global variable
Singleton.Istance.alreadyWorking = false; // istance

then i modified the video_newframe function like this:

private void video_NewFrame2(object sender, NewFrameEventArgs eventArgs) 
{
 if (!test.alreadyWorking) // or if (!Singleton.Istance.alreadyWorking)
 {
    test.alreadyWorking = true; // or Singleton.Istance.alreadyWorking = true;

    Bitmap img = (Bitmap)eventArgs.Frame.Clone();
    var result = reader.Decode(img);
    if (result != null) {
        if (DialogResult.OK == MessageBox.Show("hello!"))
            // do something;
        img.Dispose(); 
        test.alreadyWorking = false; // or Singleton.Istance.alreadyWorking = false;
    }
 }
 else
     return;
}

and i thought it was right but...unfortunately there still are more than one messageBox :(

Am i doing something wrong in the singleton implementation? Thx again...!

Instead of unsubscribing from the event, create a boolean singleton somewhere in your application. Inside your video_NewFrame2 method check whether the singletons value is true or false. If true, return from the method; if false, set it to true and continue with your job. Once job is done, set the singletons value back to false (you can do that either instantly after the job is done or after some delay). On how to implement a singleton in C# refer to Google and this specifically good article:

http://csharpindepth.com/Articles/General/Singleton.aspx

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