程序代码: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的后面去。请问,这个问题怎么解决,在线等待

解决方案 »

  1.   


    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的后面就可以了
      

  2.   

    回复2楼zhuqueta0101 :    把frmWait.Close(); 放到show的后面也不行,MessageBox会跑到等待画面的后面还有没有其它的办法,比如说让MessageBox直接显示在最上层,或是找到主窗体,让MessageBox依赖主窗体来显示?。。
      

  3.   

    改为:
    MessageBox.Show(主窗口的句柄,.........)
    试试呢
      

  4.   

    谢谢,你的方法可以实现我的要求,代码贴出来看看:public  void AsyncCallbackMethod(System.IAsyncResult myIAsyncResult)
    {
       frmWait.Close();
       if (trueOrfalse == 0)
       {
           MessageBox.Show(this,"リベート取込は完了しました。", this.Text,
               MessageBoxButtons.OK, MessageBoxIcon.Information);
       }
       else
       {
           MessageBox.Show(this,"リベート取込は失敗しました、ご確認ください。",
               this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
       }
    }
      

  5.   


    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(); 
      

  6.   


    因为messageBox与主窗体不在同一线程,所以它不会置于主窗体之上,messagebox只是在自己同线程享有topmost。使用同步消息发送。或者让messagebox获取主窗体的handle