【问题标题】:Showing Form from a thread not rendering perfectly显示来自线程的表单无法完美呈现
【发布时间】:2013-11-20 10:33:40
【问题描述】:

我正在使用 jabber-net 开源库开发一个聊天应用程序.. 我的目标是在消息到来时显示一个表单(聊天窗口)。 但是当我使用这段代码时,Form出现在任务栏中,,,,没有完美呈现...... 看起来像这样...更多我只能在将鼠标悬停在任务栏上的图标时才能看到表单(Hail Windows 7)...任何形式都是这样的...

Click here for Output Image

我的代码是这样的......

    public jabber.client.JabberClient jabberClient1;
    jabberClient1.User = UserName;
    jabberClient1.Password = Password;
    jabberClient1.Resource = resource;
    jabberClient1.AutoRoster = true;
    jabberClient1.OnMessage += new MessageHandler(jabberClient1_OnMessage);

    private void jabberClient1_OnMessage(object sender, jabber.protocol.client.Message msg)
    {
        try
        {
            chatWindow chw = new chatWindow();
            chw.Left = 0;
            chw.Top = 0;
            chw.TopMost = true;
            //chw.LoadChat(msg.From.User, msg.From.Bare, "0");
            //chw.SetMessage(msg);
            chw.Show();
        }
    }

【问题讨论】:

  • 使用Control.BeginInvokeControl.Invoke 方法将此操作重定向到主应用程序线程。 jabberClient1_OnMessage 可能在任意线程上下文中被调用。

标签: c# multithreading winforms thread-safety xmpp


【解决方案1】:

你必须使用 chw.ShowDialog()

如果需要调用则使用

    private delegate void dlgInvokeRequired();

public void InvokeMethode()
{
    if (this.InvokeRequired == true) 
    {
     dlgInvokeRequired d = new dlgInvokeRequired(InvokeMethode);
     this.Invoke(d);
    } else 
    {
     chatWindow chw = new chatWindow();
     chw.Left = 0;
     chw.Top = 0;
     chw.TopMost = true;
     //chw.LoadChat(msg.From.User, msg.From.Bare, "0");
     //chw.SetMessage(msg);
     chw.Show();
    }
}

【讨论】:

  • 它不会工作....我自己解决了...我必须使用 JabberClient1.InvokeControl = FormInstance;并且,FormInstance 应该在聊天窗口出现之前显示出来......即,它可以是联系人窗口......
【解决方案2】:

我自己解决了... 我必须使用

JabberClient1.InvokeControl = FormInstance; 

而且,FormInstance 应该在聊天窗口出现之前显示...... 即,它可以是联系人窗口(名册)....

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多