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:
private void OnReceive(IAsyncResult ar)
{
try
{
EndPoint receivedFromEP = new IPEndPoint(IPAddress.Any, 0);
clientSocket.EndReceiveFrom(ar, ref receivedFromEP);
Data(byteData);
switch (cmdCommand)
{
case Command.Invite:
{
if (bIsCallActive == false)
{
DialogResult result = ThongBaoCoDienThoai.HienHopThoai(strName);
if (result == DialogResult.Yes)
{
}
else
{
}
}
else
{
SendMessage(Command.Busy, receivedFromEP);
}
break;
}
case Command.OK:
{
break;
}
case Command.Busy:
{
MessageBox.Show("User busy.", "VoiceChat", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
break;
}
case Command.Bye:
{
if (receivedFromEP.Equals(otherPartyEP) == true)
{
}
break;
}
}
byteData = new byte[1024];
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;
SoundPlayer player;
public ThongBaoCoDienThoai()
{
InitializeComponent();
try
{
henGioTat = 30;
thoiGian = new Timer();
thoiGian.Interval = 1000;
thoiGian.Enabled = true;
thoiGian.Start();
thoiGian.Tick += new EventHandler(this.thoiGian_Tick);
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);
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!