2014-12-06 133 views
-1

我有在我使用的Web引用显示帐户特定user.Here当前短信平衡一个Windows应用程序是我使用的代码:如何在C#中进行处理时显示“请稍候”消息框?

Thread t1 = new Thread(new ThreadStart(ShowWaitMessage)); 
      t1.Start(); 
      XmlNode xmlnde = ws.BSAcBalance(txtLoginId.Text, txtPassword.Text); 
      string sResponse = xmlnde.ChildNodes[0].InnerXml; 
      if (sResponse.Contains("Authentication Failed")) 
      { 
       t1.Abort(); 
       MessageBox.Show("Invalid login id or password !", "Information", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);      
      } 
      else 
      { 
       t1.Abort(); 
       MessageBox.Show("Total balance in your account is Rs : " + sResponse , "Information", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); 
      } 

public void ShowWaitMessage() 
    {    
     MessageBox.Show("Please Wait ......"); 
    } 

的ws.BSAcBalance是将应用程序连接到Web的方法,需要大约2到3秒才能执行。与此同时,我想显示一个“Please Wait”消息,我正在尝试使用线程并通过消息框显示消息。现在,一旦所需操作完成,我想要“请稍候”消息框隐藏,但那不会发生,我该怎么办?

+1

请等待不应该是一个消息框,在所有。它应该像启动画面或非模态窗口。它是一个简单的窗体,你可以使用'form.Close()'关闭它'永远不要使用'Thread.Abort'。 – 2014-12-06 12:40:42

+0

让你的用户界面脱离线程。显示消息,在另一个线程中进行调用,并在返回时使用该值调用UI。在回调函数中显示值并隐藏框。 – dbugger 2014-12-06 12:44:56

+0

将鼠标光标更改为等待光标可能就足够了吗? – RenniePet 2014-12-06 13:24:52

回答

0

MessageBox被设计为接受用户的输入。所以它不会提供任何“本地”方式来以编程方式关闭它。你有两个选择:

选项1.

一)定义一个新的类WaitForm,从System.Windows.Forms.Form而得;

b)定义公开方法CloseMeWaitForm中,一旦从外部调用,将执行Form.Close()

c)当需要显示等待消息并调用它的继承方法ShowDialog()时,创建一个WaitForm的实例。

d)一旦您的操作完成,请致电CloseMe从您的线程。

选项2:(力的MessageBox收盘)

使用Windows API函数FindWindow,这是不是本地的.NET。所以,你必须包括:

[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] 
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName); 
  • 然后,一旦操作完成,调用

IntPtr hWnd = FindWindowByCaption(IntPtr.Zero, <YourWaitMessageBoxTitle>); if (hWnd.ToInt32() != 0) PostMessage(hWnd, WM_CLOSE, 0, 0);

相关问题