C# - 定义事件处理程序的范围

时间:2011-02-28 16:36:54

标签: c# .net programming-languages

我正在重写我的最后一个问题,因为我试图从不同的角度来解决这个问题。

问题是这个。我有一个用C#编写的类,有两个公共方法,以及每当完成任何方法时触发的事件处理程序。

我创建了这个类来修改Form中的代码。

现在,如果我从用VB.Net编写的Windows Process项目中引用此类,并且我调用这两个方法中的任何一个,则EventHandler将不会触发

这个问题可能与EventHandler的范围有关吗?

如有必要,我可以发布代码

由于

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

好的..这是代码。最初我是从类中调用方法,但是我修改整个项目只是为了看问题是否与尝试从类中引发事件有关...所以我有这个c#webserver侦听端口8080,并且在http请求根据URL参数调用ENROLL或IDENTIFY。

只是澄清一下。这是一个将在连接了USB指纹扫描仪的计算机上运行的网络服务器。然后,Web应用程序将对该计算机执行http请求,以执行IDENTIFICATION或ENROLLMENT,这些操作在Web服务器内部编程,操作扫描仪驱动程序中包含的方法,这是我用SDK获得的。

在一个带有驱动程序SDK的C#演示项目中,这是一个带有按钮的简单表单,它从CLICK事件中调用ENROLL或IDENTIFY方法。 CLICK事件完成后(在其中执行ENROLL或IDENTIFY方法),然后触发事件并执行EventHandler。

所以现在我在同一个项目中拥有所有代码,但它的行为方式仍然相同......它进入ENROLL方法,该方法从WisSensorNLibLib类执行StartEnroll方法,但事件未被触发或捕获由班级'自定义事件处理程序...

也许我错放了定义或实例......我不知道。但事件并未触发......

所以这里是代码......(以下我将粘贴原始的Demo程序代码,所以可能通过比较,有人可以找出问题可能是什么)


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();
        }

    }
}

原创演示程序代码:


    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();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

首先,它是事件,而不是事件处理程序,它正在被提升 事件处理程序是方法,它将包含在给定事件中。

其次,范围仅在编译时控制成员可见性,并且不影响事件订阅。 如果事件可见,则有效。

根据您发布的内容,我们可能只得出结论:

  1. 您可能不会在VB .NET代码中订阅事件,也可能无法正确处理它;
  2. 从VB .NET代码调用时,这些方法可能会有不同的行为(传递不同的参数?抛出exeptions?)并且可能不会引发事件。
  3. 到目前为止,在我看到代码之前,这是我能说的全部内容 你能把它修剪成最简单的可重复的情况吗?