当收到新消息时关闭MessageBox [英] Close MessageBox when incoming new message

查看:201
本文介绍了当收到新消息时关闭MessageBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了简单的应用程序来使用C#和GsmComm库发送和接收消息。如果有新的传入消息,我的应用程序将显示messageBox新消息到达。我的问题是当我有很多新的消息时,messageBox会显示如此多的messageBox notication。如何使用代码显示最后一个单个消息框?





这个mycode:

  private void comm_MessageReceived(object sender,MessageReceivedEventArgs e)
{
var obj = e.IndicationObject;
if(obj是MemoryLocation)
{
var loc =(MemoryLocation)obj;
var msg = string.Format(存储中收到的新消息\{0} \,索引{1}。,
loc.Storage,loc.Index);
MessageBox.Show(msg);
return;
}
}

我混淆解决这个问题,我试图用另一种方式使用窗体显示新的传入的notif form1.showDialog(); 但同样的问题首先形式show不能在新窗体打开时关闭。这是我的参考: https://stackoverflow.com/a/13445167/3319555



我非常感谢任何人都可以帮助我......谢谢

解决方案

如果您使用第二种解决方案使用 form.ShowDialog()显示表单,您可以将表单存储在列表中。然后,当需要显示一个新表单时,可以遍历列表并使用 form.Close()关闭每个打开的表单。假设你的comm_MessageReceieved方法在另一个线程上运行,我认为它是通过一个IO完成端口来驱动的,那么这样的事情可能是这样吗?

 列表与LT; MyForm的> formList =新列表< MyForm>(); 

只读对象formListLock = new object();

private void comm_MessageReceived(object sender,MessageReceivedEventArgs e)
{
///您需要锁定列表才能进行线程安全访问
lock(formListLock)
{
///迭代列表的副本以避免在迭代中突变列表
foreach(formList.ToList()中的MyForm表单)
{
表单。 ThreadSafeClose();
}
}

string msg =message;
using(MyForm form = new MyForm(msg))
{
lock(formListLock){formList.Add(form); }
form.ShowDialog();
lock(formListLock){formList.Remove(form); }
}
}

这只是我的头顶,但可能是另一个可能的方向。

您必须对form.Close()进行线程安全调用,以便它可以在窗体的UI线程上运行。阅读有关调用此处的信息。关于这个话题有很多关于SO的信息。这可能就像在你的表单类中添加如下方法一样简单:

  public void ThreadSafeClose()
{
if(this.InvokeRequired)
{
this.Invoke(new Action(Close)); ///或BeginInvoke ...
}
else
{
Close();


$ / code $ / pre

阅读更多关于列表的信息: https://msdn.microsoft.com/en-us/library /6sh2ey19%28v=vs.110%29.aspx



阅读更多关于锁定语句的信息: https://msdn.microsoft.com/en-us/library/c5kehkcz.aspx



在此处阅读有关线程同步的更多信息: https: //msdn.microsoft.com/en-us/library/ms173179.aspx



还有很多线程安全的集合可能可以满足您的需求,例如 ConcurrentBag


i created simple application to send and receive message using C# and GsmComm Library. if there is a new incoming message my application will show messageBox that new message arrived. my problem is when i have many new message, messageBox will show so many messageBox notication. How can I just show the last single message box using code?

this mycode:

private void comm_MessageReceived(object sender, MessageReceivedEventArgs e)
{
    var obj = e.IndicationObject;
    if (obj is MemoryLocation)
    {
        var loc = (MemoryLocation)obj;
        var msg = string.Format("New message received in storage \"{0}\", index {1}.",
                                loc.Storage, loc.Index);
        MessageBox.Show(msg);
        return;
    }
}

i confuse to fix this, i tried to another way using form to show new incoming notif form1.showDialog(); but same problem first form show cannot be closed when new form opened. this my reference: https://stackoverflow.com/a/13445167/3319555

I really thanks if anyone can help me..thanks

解决方案

If you're using your second solution of displaying a form with form.ShowDialog() you can store the forms in a list. Then, when a new form needs to be displayed, you can iterate through the list and close each open form with form.Close(). Assuming that your comm_MessageReceieved method is run on another thread, which I assume is driven via an IO completion port, then something like this perhaps?

List<MyForm> formList = new List<MyForm>();

readonly object formListLock = new object();

private void comm_MessageReceived(object sender, MessageReceivedEventArgs e)
{
    /// you need to lock the List for thread safe access
    lock (formListLock) 
    {
        /// iterate over a copy of the list to avoid mutating the list under iteration
        foreach (MyForm form in formList.ToList())
        {
            form.ThreadSafeClose();
        }
    }

    string msg = "message";
    using (MyForm form = new MyForm(msg))
    {
        lock (formListLock) { formList.Add(form); }
        form.ShowDialog();
        lock (formListLock) { formList.Remove(form); }
    }
}

This was just off the top of my head but might be another possible direction you could take.

You will have to make a thread safe call to form.Close() so that it is run on the form's UI thread. Read about invoke here. There's a lot of information on SO about this topic. This could be as simple as adding something like the following method to your form class:

public void ThreadSafeClose()
{
    if (this.InvokeRequired)
    {
        this.Invoke(new Action(Close));  /// or BeginInvoke...
    }
    else
    {
        Close();
    }
}

Read more about Lists here: https://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx

Read more about the lock statement here: https://msdn.microsoft.com/en-us/library/c5kehkcz.aspx

Read more about thread synchronisation here: https://msdn.microsoft.com/en-us/library/ms173179.aspx

There are also numerous thread-safe collections that could possibly suit your needs, e.g. ConcurrentBag.

这篇关于当收到新消息时关闭MessageBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆