1

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?

enter image description here

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

Community
  • 1
  • 1
aminvincent
  • 553
  • 1
  • 12
  • 43

1 Answers1

1

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.

Community
  • 1
  • 1
khargoosh
  • 1,450
  • 15
  • 40
  • i tried it but when i run my project i got first message clearly but when the second message incoming i got message like this `cross thread operation not valid: 'MyForm' accessed from a thread other than the thread it was created on` any suggest? – aminvincent Feb 18 '16 at 02:58
  • You have to make a thread safe call to form.Close so that it is run on the form's UI thread. – khargoosh Feb 18 '16 at 03:00
  • where i must call `ThreadSafeClose()` ? in form that i show as dialog message or another? tell me please,.thanks – aminvincent Feb 18 '16 at 04:07
  • I have already shown you in my example above. `form.ThreadSafeClose()` should be called when you need to close the form from another thread. This is how a form can be closed (or any method call or property of the form accessed) from a thread that is not the form's UI thread. – khargoosh Feb 18 '16 at 04:10
  • sorry I did not check your suggest code above,.. it really wonderfull and it work,.. thanks so much – aminvincent Feb 18 '16 at 04:25