日期:2014-05-18  浏览次数:20966 次

向非托管代码传递委托时,托管应用程序必须让这些委托保持活动状态,直到确信不
代码如下
C# code
namespace ConsoleApplication1
{
    class Program
    {
        public static string ly_mc_0 = "";//通道1名称
        public static string ly_mc_1 = "";//通道2名称

        public delegate void p_MyPhonicCallBack(PhonicDLL.EPhonicEvent eventType, int channelType, int channelID, int iParam1, int iParam2);// 定义委托

        p_MyPhonicCallBack pdataRec = new p_MyPhonicCallBack(MyPhonicCallBack);


        public static void MyPhonicCallBack(  PhonicDLL.EPhonicEvent eventType, int channelType, int channelID, int iParam1, int iParam2)
        {
            
        }

        static void Main(string[] args)
        {
            try
            {

                [color=#FF0000]PhonicDLL.tpi_SetEventNotifyCallBackProc(pdataRec);[/color]                PhonicDLL.tpi_OpenDevice();


                string receiveStr = "";
                // 把IP地址转换为IPAddress的实例
                //IPAddress ipAd = IPAddress.Parse("10.53.32.252");
                IPAddress ipAd = IPAddress.Parse("192.168.1.104");
                // 初始化监听器, 端口为8008
                TcpListener myList = new TcpListener(ipAd, 8888);

                // 开始监听服务器端口
                myList.Start();
                while(true)
                {
                    //测试加的,防止板卡自己关闭///
                    PhonicDLL.tpi_GetCardCount(1);
                    //测试加的,防止板卡自己关闭///

                    receiveStr = "";
                    // 输出服务器启动信息
                    Console.WriteLine("在8888端口启动服务...");
                    Console.WriteLine("本地节点为:" + myList.LocalEndpoint);
                    Console.WriteLine("等待连接.....");

                    // 等待处理接入连接请求
                    // 新建立的连接用套接字s表示
                    Socket s = myList.AcceptSocket();
                    Console.WriteLine("连接来自 " + s.RemoteEndPoint);

                    // 接收客户端信息
                    byte[] b = new byte[100];
                    int k = s.Receive(b);
                    Console.WriteLine("已接收...");
                    
                    for (int i = 0; i < k; i++)
                    {
                        receiveStr += Convert.ToChar(b[i]);
                       // Console.Write(Convert.ToChar(b[i]));
                    }
                    Console.WriteLine("命令 " + receiveStr);
                    ////////////////处理电话部分begin///////////////
                    //指令格式 操作类型(1:拨号 0:挂断)|ChannelType|GetChannelID|电话号码|录音文件名
                    if (receiveStr != "")
                    {
                        string[] arr = receiveStr.Split('|');

                        
                    }
                    else
                    {
                        ASCIIEncoding asen = new ASCIIEncoding();
                        s.Send(asen.GetBytes("-1"));
                        Console.WriteLine("\n已发送回应信息");

                        // 善后工作,释放资源
                        s.Close();
                    }
                    /////////////////处理电话部分end/////////////
                    // 处理客户端请求,给客户端回应
                   
                }
                
                myList.Stop();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error..... " + e.StackTrace);
                
            } 

        }
        
    }
}



出现如题错误,网上查了资料,照资料改,出现红色部分运行不了,求解决

------解决方案--------------------
所谓“保持活动状态”,就是保证其不被GC回收。例如:

1. 如果你的pdataRec是static的,例如放到一个static的集合里,然后等回调之后才从集合里清除掉。
2. 显示地使用 GC.KeepAlive 来阻止 pdataRec 被处理掉。
------解决方案--------------------
这个能编译通过吗?

在static函数Main里面调用的
PhonicDLL.tpi_Set