系统无效操作异常错误 [英] System Invalid Operation Exception error

查看:139
本文介绍了系统无效操作异常错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各位大家好!昨天我创建了两个表单,第一个表单是客户端网络的侦听请求,第二个表单是表单警告(MessageBox自定义),如MessageBox,但是当客户端向第一个表单发送连接请求时,表示第二个表单会显示,但是点击任何按钮关闭表格我收到以下信息:



标题:System.InvalidOperationException

详情:

撤消操作遇到的上下文与相应的Set操作中应用的上下文不同。可能的原因是在线程上设置了上下文而没有恢复(撤消)。



The First From:

Hello everybody! Yesterday I have created two forms, the first form listen requests of client out network, the second form is form alert (MessageBox custom) like MessageBox, but when a client sends a connection request to the first form, affter the form second will show, but affter click on any button to close form and I received an message below:

Title: System.InvalidOperationException
Details:
The Undo operation encountered a context that is different from what was applied in the corresponding Set operation. The possible cause is that a context was Set on the thread and not reverted(undone).

The First From:

//Nhận các yêu cầu và kiểm tra và gửi phản hồi
        private void OnReceive(IAsyncResult ar)
        {
            try
            {
                EndPoint receivedFromEP = new IPEndPoint(IPAddress.Any, 0);

                //Get the IP from where we got a message.
                clientSocket.EndReceiveFrom(ar, ref receivedFromEP);

                //Convert the bytes received into an object of type Data.
                Data(byteData);

                //Act according to the received message.
                switch (cmdCommand)
                {
                    //We have an incoming call.
                    case Command.Invite:
                        {
                            if (bIsCallActive == false)
                            {
                                //We have no active call.                                
                                //Ask the user to accept the call or not.                                
                                
                                DialogResult result = ThongBaoCoDienThoai.HienHopThoai(strName);                                
                                
                                if (result == DialogResult.Yes)
                                {
                                    //SendMessage(Command.OK, receivedFromEP);                                    
                                    //Bắt đầu kết nôi
                                    //**********************************GOI PHUONg THUC GUI NHAN DONG BO AM THANH****************                                    
                                }
                                else
                                {
                                    //The call is declined. Send a busy response.
                                   //SendMessage(Command.Busy, receivedFromEP);
                                }                        
                                
                            }
                            else
                            {
                                //We already have an existing call. Send a busy response.
                                SendMessage(Command.Busy, receivedFromEP);
                            }
                            break;
                        }

                    //OK is received in response to an Invite.
                    case Command.OK:
                        {
                            //Start a call.
                            //**********************************GOI PHUON THUC GUI NHAN DONG BO AM THANH****************
                            break;
                        }

                    //Remote party is busy.
                    case Command.Busy:
                        {
                            MessageBox.Show("User busy.", "VoiceChat", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            break;
                        }

                    case Command.Bye:
                        {
                            //Check if the Bye command has indeed come from the user/IP with which we have
                            //a call established. This is used to prevent other users from sending a Bye, which
                            //would otherwise end the call.
                            if (receivedFromEP.Equals(otherPartyEP) == true)
                            {
                                //End the call.

                            }
                            break;
                        }
                }

                byteData = new byte[1024];
                //Get ready to receive more commands.
                clientSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref receivedFromEP, new AsyncCallback(OnReceive), null);
            }
            catch (Exception ex)
            {
                MessageBox.Show("OnReceive: " + ex.Message, "Error");
            }
        }





第二种形式:





The Second Form:

static ThongBaoCoDienThoai tbcdt;
        static DialogResult kq = DialogResult.No;
        private Timer thoiGian;         
        int henGioTat; //Hẹn thời gian tắt form
        SoundPlayer player; //Khởi tạo thiết bị phát (Loa)
        
        public ThongBaoCoDienThoai()
        {
            InitializeComponent();
            try
            {
                henGioTat = 30;
                thoiGian = new Timer();
                thoiGian.Interval = 1000;
                thoiGian.Enabled = true;
                thoiGian.Start();
                thoiGian.Tick += new EventHandler(this.thoiGian_Tick);

                //Hiển thị hộp thông báo ở góc màng hình dưới bên phải
                Rectangle r = Screen.PrimaryScreen.WorkingArea;
                this.StartPosition = FormStartPosition.Manual;
                this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width, Screen.PrimaryScreen.WorkingArea.Height - this.Height);

                //Phát âm thành khi form được mở  
                player = new SoundPlayer(Properties.Resources.ringing);
                player.Load();
                player.PlayLooping();

            }
            catch (Exception ex)
            {
                MessageBox.Show("ThongBaoCoDienThoai_Load :" + ex.Message, "Error");
            }
        }

        public static DialogResult HienHopThoai(string tenNguoiGoi)
        {            
            try
            {
                tbcdt = new ThongBaoCoDienThoai();
                tbcdt.lbThongBao.Text = tenNguoiGoi + " đang gọi........";
                tbcdt.ShowDialog();                
            }
            catch (Exception ex)
            {
                MessageBox.Show("HienHopThoai: "+ex.Message,"Error");
            }
            return kq;
        }        

        private void btHuy_Click(object sender, EventArgs e)
        {
            tbcdt.thoiGian.Stop();
            tbcdt.thoiGian.Dispose();
            kq = DialogResult.No;
            tbcdt.Dispose();            
        }

        private void btNhan_Click(object sender, EventArgs e)
        {
            ThongBaoCoDienThoai tbcdt = new ThongBaoCoDienThoai();
            alertControl1.Show(tbcdt);
            tbcdt.thoiGian.Stop();
            tbcdt.thoiGian.Dispose();
            kq = DialogResult.Yes;
            tbcdt.Dispose();
        }        

        private void thoiGian_Tick(object sender, EventArgs e)
        {
            henGioTat--;

            if (henGioTat == 0)
            {
                tbcdt.thoiGian.Stop();
                tbcdt.thoiGian.Dispose();
                tbcdt.Dispose();
            }
        }

        private void ThongBaoCoDienThoai_FormClosing(object sender, FormClosingEventArgs e)
        {
            player.Stop();            
        }





非常感谢!



Thank very much!

推荐答案

您需要做一些事情才能到达底部。



首先,如果你'尚未在调试器中运行您的应用程序,请执行此操作。并且,在抛出用户未处理的异常时将调试器设置为停止。这将使您尽可能接近调用异常的代码(或框架代码)中的位置。



其次,您可能需要阅读关于线程和计时器。你有第二种形式的计时器;你正在播放声音(我基本上什么都不知道,所以无法帮助,但预计Windows可能会做各种可能涉及线程的事情)。



第三,我将特别找出异常中提到的Undo()方法的全部内容;我还将研究如何,何时以及通过什么代码更改线程上的上下文。我怀疑这很容易,但这看起来是你问题的根源。



对不起我不能再有用了。



Chris
There are some things you need to do to get to the bottom of this.

First, if you're not already running your app in the debugger, do so. And, set the debugger to stop when user-unhandled exceptions are thrown. That will get you as close as possible to the place in your code (or the framework code) where the exception is being called.

Second, you probably need to read up about threads and timers. You've a timer in your second form; you're playing a sound (about which I know basically nothing, so can't help, but anticipate that Windows may be doing all sorts of things possibly involving threads).

Third, I'd specifically find out what the Undo() method mentioned in the exception is all about; I'd also look into how, when and by what code a context on a thread is being changed. I doubt it'll be easy, but that's looks to be the root of your problem.

Sorry I can't be any more helpful.

Chris


这篇关于系统无效操作异常错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆