Click here to Skip to main content
16,016,605 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
//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:

C#
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!
Posted

1 solution

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900