locked
Showing and closing a short message form ??? RRS feed

  • Question

  • I have a short message form class:

        public partial class formShortMessage : Form {
            // den tid meddelelsen skal vises i mSek
            private long tid;
    
            public formShortMessage(string med, long Tid) {
                InitializeComponent();
                label1.Text = med;
                tid = Tid;
            }
    
            private void formShortMessageForm_Load(object sender, EventArgs e) {
                if (tid!=-1) {
                    timer1.Interval = (int)tid;
                    timer1.Enabled = true;
                }
                const int margin = 50;
                this.Width = margin + label1.Text.Length * 10 + margin;
                this.CenterToScreen();
                this.Show();
            }
    
            private void timer1_Tick(object sender, EventArgs e) {
                this.Close();
            }
        }
    
    

    and when I need to show a short message, msg,  I use the method visSM in this code snip:

            static public void visSM(string msg, long tid) {
                Thread smThread = new Thread(sM);
                smThread.Start(new myParam(msg, tid));
            }
    
            static void sM(object mObj) {
                formShortMessage frm = new formShortMessage(((myParam)mObj)._m, ((myParam)mObj)._t);
                frm.ShowDialog();
                frm.Dispose();
            }
    
            class myParam {
                public string _m;
                public long _t;
    
                public myParam(string m, long t) {
                    _m = m;
                    _t = t;
                }
            }
    
    


    The variable 'tid' is the amount of mSec that the short message is shown and it functions very well !

    NOW I want a change so that IF tid=-1 (the time to show the message) then it should be shown untill the short message form is closed by a NEW short message.

    I can find out NOT to start the timer when tid=-1 as you can see in the code, and then the new shortmessage form WILL stay open, but how should I close it BEFORE the next short message is  shown ?

    BEFORE I initialize the new short message I have tried to run through all open forms until I find one of type ShortMessage - but I never find one - even though I can SEE one on the screen - something like this:

            public formShortMessage(string med, long Tid) {
                findAndCloseAllShortMessageForms();  <<<<<<<<<<<<<<<<<<<<< throu all open forms and close if SM-form
                InitializeComponent();
                label1.Text = med;
                tid = Tid;
            }
    
    
    

    but it wont do the job !

     If the solution has to do with 'delegates' then please teach me with my sample code.


    Best regards KSor, Denmark
    Friday, September 9, 2011 5:48 AM

Answers

All replies

  • You have to provide the formshortmessage with a reference to the caller (as sMobj, probably) and - if tid is -1 - set a handler for a caller.Event, such as CallerCallsMeClose.

    So, When caller creates the formshortmessage (I write this in VB, but I'm only explaining the concept

    RaiseEvent CallerCallsMeClose<br/>Dim frm As New FormShortMessage(msg, t, Me)
    frm.ShowDialog()
    frm.Dispose
    

    in formshortmessage constructor

    Public Sub New(byval med as string, byval tid as long, byVal frm As smObj)
    _Caller = frm
    AddHandler _Caller.CallerCallsMeClose, AddressOf myMethodToClose
    End Sub
    



     

     


    please, mark this as answer if it is THE answer
    ----------------
    Diego Cattaruzza
    Microsoft MVP - Visual Basic: Development
    blog: http://community.visual-basic.it/Diego
    web site: http://www.visual-basic.it
    Friday, September 9, 2011 6:45 AM
  • Hi DiegoCattaruzza

    Thx, but sorry,  I'm not that experienced, so I can 'translate' your solution ;-((


    Best regards KSor, Denmark
    Friday, September 9, 2011 10:58 AM
  • http://msdn.microsoft.com/en-us/library/aa645739(VS.71).aspx
    please, mark this as answer if it is THE answer
    ----------------
    Diego Cattaruzza
    Microsoft MVP - Visual Basic: Development
    blog: http://community.visual-basic.it/Diego
    web site: http://www.visual-basic.it
    • Marked as answer by Ksor Friday, September 9, 2011 12:44 PM
    • Unmarked as answer by Ksor Friday, September 9, 2011 1:08 PM
    Friday, September 9, 2011 12:12 PM
  • I don't understand it - can you use my sample code in the solution ?

    Maybe someone else can teach me.


    Best regards KSor, Denmark
    Friday, September 9, 2011 1:10 PM
  • my fault, I didn't see the magic word 'static'. But you use a Thread, so, if 'static' subroutines are in a static class named (for example) viSMhelper, you can try this code (I don't like C#, so be comprehensive against my mistakes, if there are any :D):

        public partial class formShortMessage : Form
        {
            // den tid meddelelsen skal vises i mSek
            private long tid;
    
            public formShortMessage(string med, long Tid)
            {
                InitializeComponent();
                label1.Text = med;
                tid = Tid;
            }
    
            private void formShortMessageForm_Load(object sender, EventArgs e)
            {
                if (tid != -1)
                {
                    timer1.Interval = (int)tid;
                } else {
                    timer1.Interval = 100;
                }
                timer1.Enabled = true;
    
                const int margin = 50;
                this.Width = margin + label1.Text.Length * 10 + margin;
                this.CenterToScreen();
                this.Show();
            }
    
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                if (tid != -1)
                {
                    this.Close();
                }
                else
                {
                    if (viSMhelper.formShortMessageHasToClose == true)
                    {
                        this.Close();
                    }
    
                }
            }
        }
    
        static public class viSMhelper
        {
    
            static public bool formShortMessageHasToClose;
    
            static public void visSM(string msg, long tid) 
            {
                Thread smThread = new Thread(sM);
                smThread.Start(new myParam(msg, tid));
            }
    
            static void sM(object mObj) 
            {
                formShortMessageHasToClose = true;
                Thread.Sleep(100);
                formShortMessageHasToClose = false;
                formShortMessage frm = new formShortMessage(
                    ((myParam)mObj)._m, ((myParam)mObj)._t);
                frm.ShowDialog();
                frm.Dispose();
            }
    
            class myParam
            {
                public string _m;
                public long _t;
    
                public myParam(string m, long t)
                {
                    _m = m;
                    _t = t;
                }
            }
    
        }
    
    


    please, mark this as answer if it is THE answer
    ----------------
    Diego Cattaruzza
    Microsoft MVP - Visual Basic: Development
    blog: http://community.visual-basic.it/Diego
    web site: http://www.visual-basic.it
    Friday, September 9, 2011 9:32 PM
  • Hi DiegoCattaruzza

    That's much better and it works, BUT ...

    i plain words, what's the idea using the timer when tid=-1 ?

    A short message form that was shown because tid=-1 should stay open until the NEXT short message form is shown no matter what tid is in the 'next' form.

    It seems like short message forms with tid=-1 only can be closed by short message forms where tid=-1

    In fact I can see that the LAST SM-form with tid=-1 is NOT closed by last SM-form where tid=2000 to show that all operations is done.

    What are the purpose of :

                formShortMessageHasToClose = true;
                Thread.Sleep(100);
                formShortMessageHasToClose = false;

    I beleave strange things could happen IF I show SM-forms where tid<=100 - right ?


    Best regards KSor, Denmark
    Saturday, September 10, 2011 10:58 AM
  • the timer thick event must be able to raise before you reset formshortmessagehastoclose to false.

    If tid = -1 the timeer interval is set to 100, exactly as the sleep interval. So you are almost sure that the thick event raises before you create the new formshortmessage instance.

    When you set the flag true, you are sure that the formmessage closes (if its tid is -1), or (if tid is different) the flag doesn't influence it.

    If you want to close formmessage in any case, you have to test the flag all times (but I didn't undertand this, by your posts)

    And I agree with you about the tid < 100 (Visual basic is not for ultra-frequent thicks)

     


    please, mark this as answer if it is THE answer
    ----------------
    Diego Cattaruzza
    Microsoft MVP - Visual Basic: Development
    blog: http://community.visual-basic.it/Diego
    web site: http://www.visual-basic.it

    Saturday, September 10, 2011 12:00 PM
  • You wrote:

    "the timer thick event must be able to raise before you reset formshortmessagehastoclose to false."

    yeah, but that's when the timer SHOULD be used (when 'tid' is NOT -1) - right ? - it did work well before, so why change that ?

    maybe I should refrase:

    what's the idea - in plain words - of using the flag formShortMessageHasToClose ?

    What is wrong in my original idea by closing ALL open (if any) SM-forms BEFORE showing af new one ? (- except that I myself can't implement it !) - I think it's more simple and has no inherent chance of failure or strange behavior - why didn't I find any of these, when I can SEE one at the screen ?

     


    Best regards KSor, Denmark
    Saturday, September 10, 2011 1:15 PM
  • the logic of my implementation is:

    if tid is a number, the timer interval is that number. If tid is -1, the timer interval is 100.

    at thick event, if tid is a number, the form closes. If tid is -1, the form closes only if the flag is true. Else it reamin opened indefinitely.

    So, when you have to open a smf (shortmessageform), I take care to close any opened smf (only those ones with tid -1). I accomplish this task setting the flag true, waiting 100 milliseconds to be sure the smf catch it, and resetting the flag false, avoiding that the next smf with tid -1 closes in 100 milliseconds.

    Your idea of closing ANY smf is not good (I think, without knowing all your shoes) because, if there is a sense to set a showing time (let's say of 2 seconds), what is the sense to close it before the 2 seconds have been spent?

    On the contrary, closing only the 'indefinite' smf has 'all the sense on your world', as you described it

    If you wish to close 'all' smf, this mean: you want to show only one smf at a time, indipendently of the tid, you have to decide what to do when you need to create a smf and the previous smf is still showing because the time is not over.

    Anyway, you can implement also this:

    If you want close ANY smf, you have the problem to divide the timer work:

    the timer interval is set always to 100. the tid value is for a variable, not for the interval. And you set another variable (elapsed) to the same value, if it is not -1.

    In the thick event you verify first if flag is true (and if so, closes)

    second, if tid = -1, exit sub

    third, if tid is not -1, decrement 'elapsed' of 100. If 'elapsed' is zero or negative, closes, else exit sub

     

     


    please, mark this as answer if it is THE answer
    ----------------
    Diego Cattaruzza
    Microsoft MVP - Visual Basic: Development
    blog: http://community.visual-basic.it/Diego
    web site: http://www.visual-basic.it
    Saturday, September 10, 2011 5:59 PM
  • If you wish to close 'all' smf, this mean: you want to show only one smf at a time, indipendently of the tid, you have to decide what to do when you need to create a smf and the previous smf is still showing because the time is not over.

    Anyway, you can implement also this:

    If you want close ANY smf, you have the problem to divide the timer work:

    I beleave not, because I don't want to use the timer when tid=-1 - it is non sence - Occams razor ;-))

    It does no harm to close ALL smf's because the nature of the 'short message' - if a sm with tid=2000 should be overwrited by a sm with tid=-1 BEFORE it has spend 2000 msec on the screen - it doesn't matter because the user has his focus is on the NEW sm in the advancing process shown to him in short messages.

    Meanwhile I beleave I found out WHY I don't find any open smf's in my original idea - it's because the open smf's is in another thread - so I beleave the problem has changed now: how do I close ALL forms in another thread.


    Best regards KSor, Denmark
    Sunday, September 11, 2011 3:32 AM
  • Hi Ksor,

    To close form in another thread, or  Invoke the UI from another thread please check http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/2cb9cd98-b638-4378-9349-fbb6598e5092/


    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Helen Zhou Monday, September 19, 2011 7:25 AM
    Monday, September 19, 2011 7:25 AM