程序代码:private int trueOrfalse = -1;
private delegate void MsgWait();
private frmWaiter frmWait;private void button1_Click(object sender, System.EventArgs e)
{
ThreadStart start = new ThreadStart(DataRebateExcute);
start.BeginInvoke(new AsyncCallback(AsyncCallbackMethod),null);
MsgWait wait = new MsgWait(ShowForm);
BeginInvoke(wait);
}public void DataRebateExcute()
{
for (int i = 0; i < 50; i++)
{
Thread.Sleep(100);
} if(DateTime.Now.Minute % 2 == 0)
{
trueOrfalse = 0;
}
else
{
trueOrfalse = 1;
}
} //这是个等待画面。(如:等待中)
public void ShowForm()
{
frmWait = new frmWaiter();
frmWait.ShowDialog();
}public void AsyncCallbackMethod(System.IAsyncResult myIAsyncResult)
{
frmWait.Close();
if (trueOrfalse == 0)
{
MessageBox.Show("成功!", this.Text,MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
MessageBox.Show("失败!",this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}单击button1,执行完线程中的计算后,回调AsyncCallbackMethod(System.IAsyncResult myIAsyncResult)这个函数,弹出对话框。但是,弹出的对话框,不是总是在前面,如果单击主画面,这个消息框就会跑到主Form的后面去。请问,这个问题怎么解决,在线等待
private delegate void MsgWait();
private frmWaiter frmWait;private void button1_Click(object sender, System.EventArgs e)
{
ThreadStart start = new ThreadStart(DataRebateExcute);
start.BeginInvoke(new AsyncCallback(AsyncCallbackMethod),null);
MsgWait wait = new MsgWait(ShowForm);
BeginInvoke(wait);
}public void DataRebateExcute()
{
for (int i = 0; i < 50; i++)
{
Thread.Sleep(100);
} if(DateTime.Now.Minute % 2 == 0)
{
trueOrfalse = 0;
}
else
{
trueOrfalse = 1;
}
} //这是个等待画面。(如:等待中)
public void ShowForm()
{
frmWait = new frmWaiter();
frmWait.ShowDialog();
}public void AsyncCallbackMethod(System.IAsyncResult myIAsyncResult)
{
frmWait.Close();
if (trueOrfalse == 0)
{
MessageBox.Show("成功!", this.Text,MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
MessageBox.Show("失败!",this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}单击button1,执行完线程中的计算后,回调AsyncCallbackMethod(System.IAsyncResult myIAsyncResult)这个函数,弹出对话框。但是,弹出的对话框,不是总是在前面,如果单击主画面,这个消息框就会跑到主Form的后面去。请问,这个问题怎么解决,在线等待
解决方案 »
- 求高手帮助
- c# treeview 添加节点
- BeginEdit对ListView的Item有效,但是对SubItems无效,如果要修改SubItems怎么办???
- “服务器遇到内部错误。。。请关闭服务器的 .config 文件中的 customErrors。”异常怎么处理
- 短信平台设计,有经验有想法的人,请进。
- [文件]菜单指定ImageIndex能显示ImageList中的菜单,为什么文件下面的菜单显示不了?
- C# createGraphics() 函数只能在Form.cs里面调用么?
- Reflector反编译,帮我看看代码,新手有点不懂
- c#写 彩票选号程序,该 如何下手呀?
- ajax与自定义控件
- winform C#如何调用存储过程返回表
- 【求助】一个文件掉另一个文件函数
public void AsyncCallbackMethod(System.IAsyncResult myIAsyncResult)
{
frmWait.Close();
if (trueOrfalse == 0)
{
MessageBox.Show("成功!", this.Text,MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
MessageBox.Show("失败!",this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
} 在这里出错,你异步的回调AsyncCallbackMethod是在frmWait的基础上的,这个时候MessageBox.Show依赖于frmWait申请内存,但你又调用了frmWait.Close(); 释放,所以弹出框无法获得他依赖的窗体导致到了后面你把frmWait.Close(); 放到show的后面就可以了
MessageBox.Show(主窗口的句柄,.........)
试试呢
{
frmWait.Close();
if (trueOrfalse == 0)
{
MessageBox.Show(this,"リベート取込は完了しました。", this.Text,
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(this,"リベート取込は失敗しました、ご確認ください。",
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
public void ShowForm()
{
frmWait = new frmWaiter();
frmWait.ShowDialog();
if (trueOrfalse == 0)
{
MessageBox.Show("成功!", this.Text,MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
MessageBox.Show("失败!",this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
} public void AsyncCallbackMethod(System.IAsyncResult myIAsyncResult)
{
frmWait.Close();
}
因为messageBox与主窗体不在同一线程,所以它不会置于主窗体之上,messagebox只是在自己同线程享有topmost。使用同步消息发送。或者让messagebox获取主窗体的handle