Search code examples
c#.netprogramming-languages

C# - Define scope for an Event Handler


I am rewriting my last question, just because I am trying to attack the problem from differente angles..

The problem is this. I have a class written in C#, with two public methods, and an eventhandler that triggers everytime any of the methods is completed.

I created this class adapting the code from a Form.

Now, if I reference this class from a Windows Process project written in VB.Net, and I call any of the two methods, the EventHandler will NOT trigger

Could this problem be related to EventHandler's scope or anything like that?

If necessary, I can post code

Thanks

++++++++++++++++++++++++ UPDATE ++++++++++++++++++++++++

Ok.. here is the code. Originally I was calling the methods from a class, but I modified the whole project just to see if the problem had to do with trying to raise the event from the class... So I have this c# webserver listening on port 8080, and upon http request calls ENROLL or IDENTIFY according to URL parameters.

Just to clarify a bit. This is a webserver that will run in a computer which has a USB fingerprint scanner connected. Then, the web application will do an http request to that computer in order to execute an IDENTIFICATION or ENROLLMENT, actions which are programmed inside the webserver, manipulating the methods included in the scanner's driver which I got with the SDK.

In have a C# demo project which came with the driver's SDK, a simple form with buttons calling ENROLL or IDENTIFY methods from the CLICK event. After the CLICK event is finished (inside of which is executed the ENROLL or IDENTIFY methods) then the Event is triggered and the EventHandler executes.

So now I have all the code in the same project, but it is still behaving the same way... it goes into the ENROLL method which executes the StartEnroll method from the WisSensorNLibLib class, but the event is not triggered nor captured by the class' custom Event Handler...

Maybe I am misplacing the definitions or the instantiations... I don't know. But the event is not triggered...

So here is the code... (and below I will paste the original Demo program code, so maybe by comparison someone can find out what the problem could be)


namespace WinServer
    {
        using System;   
        using System.IO;
        using System.Net;
        using System.Net.Sockets;
        using System.Text;
        using System.Threading;       
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;           
        using System.Data.SqlClient;
        using System.Drawing; 
        //************************************
        //This is the class that belongs to the Fingerprint Scanner
        //************************************
        using WisSensorNLibLib;


    class WinServer 
    {    
        private TcpListener myListener ;
        private int port = 8080 ; // Select any free port you wish        
        private Int16 id;
        private string indx;
        //************************************
        //Create instance of WisSensorN     
        //************************************          
        WisSensorN WisObj = new WisSensorN();          
        private String mod;        
        private Int32 Rows;

        public WinServer()
        {
            try
            {
                myListener = new TcpListener(IPAddress.Any, port) ;
                myListener.Start();                 
                Thread th = new Thread(new ThreadStart(StartListen));
                th.Start() ;
            }
            catch(Exception e)
            {
                Console.WriteLine("An Exception Occurred while Listening :" +e.ToString());
            }
        }

        public void SendHeader(string sHttpVersion, string sMIMEHeader, int iTotBytes, string sStatusCode, ref Socket mySocket)
        {   
        //************************************
        //Code not related to understand specific problem was removed 
        //************************************          
        }

        public void SendToBrowser(String sData, ref Socket mySocket)
        {
        //************************************
        //Code not related to understand specific problem was removed 
        //************************************
        }  



        public void SendToBrowser(Byte[] bSendData, ref Socket mySocket)
        {
        //************************************
        //Code not related to understand specific problem was removed 
        //************************************
        }


        // Application Starts Here..
        public static void Main() 
        {
            WinServer MWS = new WinServer();
        }


        public void StartListen()
        {
        //Call ENROLL method
        Enroll();
        mySocket.Close();                       

        }


        //**********************************
        //EVENT CAPTURE
        //**********************************
        public void WisObj_DataEvent(WisSensorNLibLib.DATA data, string str)
        {

            switch (data)
            {
                //ENROLL
                case DATA.DATA_ENROLL:

                    try
                    {
                        Console.WriteLine("success"); 
                    }
                    catch (Exception ee)
                    {
                        Console.WriteLine(ee.Message);                        
                    }                    
                    break;

                //IDENTIFY
                case DATA.DATA_IDENTIFY_CAPTURE:

                    try
                    {
                        Console.WriteLine("Success");
                    }
                    catch (Exception ee)
                    {
                        Console.WriteLine(ee.Message);                       
                    }                    
                    break;
            }
        }

        //**********************************
        //ENROLL
        //After this method ends, WisObj_DataEvent should capture the event
        //**********************************

        public void Enroll()
        {
            try
            {
                WisObj.Open();
                WisObj.DataEvent += new _IWisSensorNEvents_DataEventEventHandler(WisObj_DataEvent);
                WisObj.StartEnroll();
            }
            catch
            {
                Console.WriteLine(ee.Message);
            }
        }

        //**********************************
        //IDENTIFY
        //After this method ends, WisObj_DataEvent should capture the event
        //**********************************

        public void Identify()
        {
            try
            {
                WisObj.Open();
                WisObj.DataEvent += new _IWisSensorNEvents_DataEventEventHandler(WisObj_DataEvent);
                WisObj.IdentifyCapture();
            }
            catch
            {
                Console.WriteLine(ee.Message);
            }
        }


        public void WisClose()
        {
            WisObj.Close();
        }

    }
}

Original Demo program code:


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using WisSensorNLibLib;

namespace OR200N_Demo
{
    public partial class Form1 : Form
    {
        private const int MaxUser = 10;
        WisSensorN WisObj = new WisSensorN();
        string[] DB = new string[MaxUser];
        int nEnrolled = 0;
        string indx;
        string msg;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            MessageBox.Show("Please make sure the scanner is plugged!!");
            WisObj.Open();
            WisObj.DataEvent += new _IWisSensorNEvents_DataEventEventHandler(WisObj_DataEvent);
            WisObj.SetDisplay((int)FingerPic.Handle);
        }

        private delegate void CompleteHandler(string bir);
        private void Complete(string str)
        {
            Status.Text = str;
        }

        private void WisObj_DataEvent(WisSensorNLibLib.DATA data, string str)
        {
            switch (data)
            {
                case DATA.DATA_ENROLL:
                    indx = nEnrolled.ToString();
                    msg = "User #" + indx + " is enrolled successfully!!";
                    this.Invoke(new CompleteHandler(Complete), new object[] { msg });
                    DB[nEnrolled] = string.Copy(str);
                    nEnrolled++;
                    break;

                case DATA.DATA_IDENTIFY_CAPTURE:
                    int nMatched;
                    nMatched = WisObj.Identify(str, DB);
                    if (nMatched < 0)
                    {
                        msg = "No valid finger matched!!";
                        this.Invoke(new CompleteHandler(Complete), new object[] { msg });
                    }
                    else
                    {
                        indx = nMatched.ToString();
                        msg = "User #" + indx + " is matched!!";
                        this.Invoke(new CompleteHandler(Complete), new object[] { msg });
                    }
                    break;

                case DATA.DATA_VERIFY_CAPTURE:
                    break;
            }
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            WisObj.Close();
        }

        private void Enroll_Click(object sender, EventArgs e)
        {
            if (nEnrolled >= MaxUser)
            {
                MessageBox.Show("Exceed maximum enrolled number (10)!!");
            }
            else
            {
                Status.Text = "Please put your finger on the scanner!!";
                WisObj.StartEnroll();
            }
        }

        private void Identify_Click(object sender, EventArgs e)
        {
            Status.Text = "Please put your finger on the scanner!!";
            WisObj.IdentifyCapture();
        }
    }
}

Solution

  • Firstly, it is event, not event handler, which is being raised.
    Event handler is a method that subsribes to a given event.

    Secondly, scope only governs member visibility at compile time and does not affect event subscriptions. If event is visible, it works.

    From what you posted, one may only conclude that:

    1. You may not subscribe to event in VB .NET code or may handle it incorrectly;
    2. When called from VB .NET code, these methods may act differently (different parameters being passed? exeptions thrown?) and may not raise the event.

    So far, this is all I can say until I see the code.
    Can you trim it down to simplest reproducible case?