来电弹屏--线程间操作无效: 从不是创建控件的线程访问它

 //最近要做个来电弹屏,在主窗体添加了弹屏监听事件,当有来电时就弹出来窗体,显示来电会员信息。就在这一步,用了线程来弹屏,在弹出窗口绑定会员信息,结果“线程间操作无效: 从不是创建控件的线程访问它”
        //后面研究了线程,用了一个线程及委托来搞定了,记录下来,,怕以后又遇到。
        /// <summary>
        /// 监听消息
        /// </summary>
        /// <param name="m"></param>   
        protected override void DefWndProc(ref System.Windows.Forms.Message m)
        {
            switch (m.Msg)
            {
                case BriSDKLib.BRI_EVENT_MESSAGE:
                    {
                        BriSDKLib.TBriEvent_Data EventData = (BriSDKLib.TBriEvent_Data)Marshal.PtrToStructure(m.LParam, typeof(BriSDKLib.TBriEvent_Data));
                        string strValue = "";
                       
                        switch (EventData.lEventType)
                        {
                            //电话机摘机
                            case BriSDKLib.BriEvent_PhoneHook:
                                {
                                    strValue = "通道" + (EventData.uChannelID + 1).ToString() + ":电话机摘机";
                                } break;
                            //电话机挂机
                            case BriSDKLib.BriEvent_PhoneHang:
                                {
                                    strValue = "通道" + (EventData.uChannelID + 1).ToString() + ":电话机挂机";
                                    AddPhnoneLog(1);
                                } break;
                            //电话机来电响铃
                            case BriSDKLib.BriEvent_CallIn:
                                {
                                    ////两声响铃结束后开始呼叫转移到CC
                                    strValue = "通道" + (EventData.uChannelID + 1).ToString() + ":来电响铃";
                                } break;
                            //接收到来电号码
                            case BriSDKLib.BriEvent_GetCallID:
                                {
                                    //strValue = "通道" + (EventData.uChannelID + 1).ToString() + ":接收到来电号码 " + FromASCIIByteArray(EventData.szData);
                                    //接到来电,记录来电号码
                                    Str_Phone = FromASCIIByteArray(EventData.szData).Replace("\0","");

             //调用
                                    ShowPhone();
                                } break;
                            //停止呼入,产生未接号码
                            case BriSDKLib.BriEvent_StopCallIn:
                                {
                                   // strValue = "通道" + (EventData.uChannelID + 1).ToString() + ":停止呼入,产生一个未接电话 ";
                                  
                                    //添加一条未接记录
                                    AddPhnoneLog(0);
                                   
                                } break;

                            case BriSDKLib.BriEvent_GetDTMFChar: strValue = "通道" + (EventData.uChannelID + 1).ToString() + ":接收到按键 " + FromASCIIByteArray(EventData.szData); break;
                            case BriSDKLib.BriEvent_RemoteHang:
                                {
                                    strValue = "通道" + (EventData.uChannelID + 1).ToString() + ":远程挂机 ";
                                } break;
                            case BriSDKLib.BriEvent_Busy:
                                {

                                    strValue = "通道" + (EventData.uChannelID + 1).ToString() + ":接收到忙音,线路已经断开 ";
                                } break;
                            case BriSDKLib.BriEvent_DialTone:
                                strValue = "通道" + (EventData.uChannelID + 1).ToString() + ":检测到拨号音 ";
                                break;
                            case BriSDKLib.BriEvent_PhoneDial:
                                strValue = "通道" + (EventData.uChannelID + 1).ToString() + ":电话机拨号 " + FromASCIIByteArray(EventData.szData);
                                break;
                            case BriSDKLib.BriEvent_RingBack:
                                strValue = "通道" + (EventData.uChannelID + 1).ToString() + ":拨号后接收到回铃音 ";
                                break;
                            case BriSDKLib.BriEvent_DevErr:
                                {
                                    if (EventData.lResult == 3)
                                    {
                                        strValue = "通道" + (EventData.uChannelID + 1).ToString() + ":设备可能被移除 ";
                                    }
                                } break;
                            default: break;
                        }

                    } break;
                default:
                    base.DefWndProc(ref m);
                    break;
            }
        }

        /// <summary>
        /// 多线程显示来电会员信息
        /// </summary>
        private void ShowPhone()
        {
            Thread phoneThread = new Thread(new ThreadStart(ThreadShowPhone));
            phoneThread.IsBackground = true;
            phoneThread.Start();
        }

        /// <summary>
        /// 通过线程延时显示,避免登录加载速度慢
        /// 通过委托将弹屏显示为主窗体的模式窗体(不用委托将不能实现模式窗体)
        /// </summary>
        private void ThreadShowPhone()
        {
            ShowPhoneInvoke spInvoke = new ShowPhoneInvoke(InvokeShowPhone);
            this.BeginInvoke(spInvoke);
        }

        /// <summary>
        /// 显示窗口,http://www.jiwen123.com
        /// </summary>
        private void InvokeShowPhone()
        {
            comeMemInfo = new MemCard().GetModel(Str_Phone.Replace("\0", ""));
            if (comeMemInfo != null)
            {
                Phone phone = new Phone();
                phone.MemInfo = comeMemInfo;
                string condi = "MemID=" + comeMemInfo.CardID;
                phone.condition = condi.Split("|".ToCharArray());
                phone.ShowDialog();
            }
        }

posted on 2012-04-16 16:32  IT小小小鸟  阅读(442)  评论(0编辑  收藏  举报