昨天我创建了两个表单,第一个表单监听客户端网络的请求,第二个表单是类似于MessageBox的表单警报(MessageBox自定义),但是当客户端向第一个表单发送连接请求时,表单第二个会显示,但点击任何按钮关闭表单后,我收到以下消息:
标题:System.InvalidOperationException 详细信息:撤消操作遇到的上下文与相应的 Set 操作中应用的上下文不同。可能的原因是在线程上设置了上下文并且未还原(撤消)。
第一个来自:
//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.
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");
}
}
第二种形式:
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();
}