Results 1 to 15 of 15

Thread: [RESOLVED] timer on winform

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2007
    Posts
    694

    Resolved [RESOLVED] timer on winform

    Hi,
    In my windows form project, every time there is a call to a long running method, I show a small form which has a label control that says, please wait and then after the long running call I hide that form . i.e.

    ShowForm();
    LongRunningMethod();
    HideForm();

    Question:
    How can I make the label control to blink every second while the showForm is being shown?
    Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: timer on winform

    I have to ask, why would you want to? I suggest that you display a for with "Please wait..." in the title bar and a ProgressBar with its Style set to Marquee. That is a UI that users are familiar with for that type of thing.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2007
    Posts
    694

    Re: timer on winform

    Do you mean, not to have a timer but instead to have a progress bar?
    Note that I do not know how long the long running tasks can take?
    Thanks

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: timer on winform

    Quote Originally Posted by arkiboys View Post
    Do you mean, not to have a timer but instead to have a progress bar?
    Note that I do not know how long the long running tasks can take?
    Thanks
    That's exactly what I mean. When I ask a question and someone says ProgressBar, Style and Marquee, the first thing I do is look up those terms. As a developer, you should always be thinking "look first, ask questions later". Having done so here would have rendered your statement (which has been curiously ended with a question mark) redundant.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2007
    Posts
    694

    Re: timer on winform

    If I use the progress bar, then how can the values be added to the progress bar?
    I mean, I do not know how long one of the long processes may take and so not sure how to place this information on the progress bar.
    Any thoughts please?
    Thanks

  6. #6
    Lively Member
    Join Date
    Mar 2007
    Posts
    84

    Re: timer on winform

    Could you just not breakdown the process into stages and once each is reached it just updates the ProgressBar?

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2007
    Posts
    694

    Re: timer on winform

    Quote Originally Posted by Madcat1981 View Post
    Could you just not breakdown the process into stages and once each is reached it just updates the ProgressBar?
    No, because say a stored procedure has to be run and each time it takes different length of time.
    Thanks

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: timer on winform

    You keep asking questions that are answered in the documentation. That would be the same documentation that you should already have read on your own but you apparently still havem't read even after I told you to in post #4. Are you not reading it for a specific reason or are you just too lazy? I genuinely want to know because I'm trying to help but you're making no effort to help yourself.

  9. #9
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: timer on winform

    While I agree with everything jmc said, I'll still provide this incase you still want to use it.
    csharp Code:
    1. #region "BlinkLabel"
    2.  
    3. public class BlinkLabel : Label
    4. {
    5.  
    6.     private System.Windows.Forms.Timer g_objTimer = new System.Windows.Forms.Timer();
    7.     private int g_iBlinkTime = 0;
    8.     private bool g_bVisible = true;
    9.  
    10.     private bool g_bIsBlinking = false;
    11.  
    12.     private bool g_bDefaultVisible = true;
    13.  
    14.     [System.ComponentModel.DefaultValue(true)]
    15.     public new bool Visible {
    16.         get { return g_bDefaultVisible; }
    17.  
    18.         set {
    19.             g_bDefaultVisible = value;
    20.  
    21.             if (base.DesignMode == true) {
    22.                 base.Visible = true;
    23.             } else {
    24.                 base.Visible = value;
    25.             }
    26.  
    27.             if (g_bDefaultVisible == true & g_bIsBlinking == true) {
    28.                 g_objTimer.Interval = g_iBlinkTime;
    29.                 g_objTimer.Enabled = true;
    30.  
    31.                 //Remove any previous handlers
    32.                 g_objTimer.Tick -= Elapsed;
    33.                 g_objTimer.Tick += Elapsed;
    34.  
    35.             }
    36.  
    37.             if (g_bDefaultVisible == false & g_bIsBlinking == true) {
    38.                 g_objTimer.Enabled = false;
    39.                 g_objTimer.Tick -= Elapsed;
    40.  
    41.             }
    42.  
    43.  
    44.         }
    45.     }
    46.  
    47.     [System.ComponentModel.DefaultValue(0)]
    48.     public int BlinkTime {
    49.         get { return g_iBlinkTime; }
    50.  
    51.         set {
    52.             g_iBlinkTime = value;
    53.  
    54.  
    55.             if (base.DesignMode == false) {
    56.  
    57.                 if (g_iBlinkTime != 0) {
    58.                     //If the control's visible property is false
    59.                     //go into blink mode but dont show the control
    60.                     if (g_bDefaultVisible == true) {
    61.                         g_objTimer.Interval = g_iBlinkTime;
    62.                         g_objTimer.Enabled = true;
    63.  
    64.                         g_objTimer.Tick += Elapsed;
    65.                     }
    66.  
    67.                     g_bIsBlinking = true;
    68.  
    69.                 } else {
    70.                     g_objTimer.Enabled = false;
    71.                     g_objTimer.Tick -= Elapsed;
    72.                     base.Visible = g_bDefaultVisible;
    73.                     g_bIsBlinking = false;
    74.                 }
    75.             }
    76.         }
    77.     }
    78.  
    79.  
    80.     private void Elapsed(object sender, System.EventArgs e)
    81.     {
    82.         if (g_bVisible)
    83.             g_bVisible = false;
    84.         else
    85.             g_bVisible = true;
    86.  
    87.  
    88.         base.Visible = g_bVisible;
    89.     }
    90.  
    91. }
    92.  
    93. #endregion
    Its a BlinkLabel I wrote some months back for the exact same reason that you want it. I used it for a "Please wait" message while waiting for an SMO query for database instances to complete. Just use it like a normal label and set its BlinkTime property to something greater than zero for it to start blinking. I suggest 500(which is 500 milliseconds). Anything else seems too slow or too fast but then thats a matter of preference.

    Trivia: This was written in VB.Net....just used an online converter to get C# code.
    Last edited by Niya; Feb 7th, 2012 at 03:08 AM. Reason: Trivia

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2007
    Posts
    694

    Re: timer on winform

    Quote Originally Posted by jmcilhinney View Post
    You keep asking questions that are answered in the documentation. That would be the same documentation that you should already have read on your own but you apparently still havem't read even after I told you to in post #4. Are you not reading it for a specific reason or are you just too lazy? I genuinely want to know because I'm trying to help but you're making no effort to help yourself.
    Solved and Thank you

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2007
    Posts
    694

    Re: timer on winform

    Quote Originally Posted by Niya View Post
    While I agree with everything jmc said, I'll still provide this incase you still want to use it.
    csharp Code:
    1. #region "BlinkLabel"
    2.  
    3. public class BlinkLabel : Label
    4. {
    5.  
    6.     private System.Windows.Forms.Timer g_objTimer = new System.Windows.Forms.Timer();
    7.     private int g_iBlinkTime = 0;
    8.     private bool g_bVisible = true;
    9.  
    10.     private bool g_bIsBlinking = false;
    11.  
    12.     private bool g_bDefaultVisible = true;
    13.  
    14.     [System.ComponentModel.DefaultValue(true)]
    15.     public new bool Visible {
    16.         get { return g_bDefaultVisible; }
    17.  
    18.         set {
    19.             g_bDefaultVisible = value;
    20.  
    21.             if (base.DesignMode == true) {
    22.                 base.Visible = true;
    23.             } else {
    24.                 base.Visible = value;
    25.             }
    26.  
    27.             if (g_bDefaultVisible == true & g_bIsBlinking == true) {
    28.                 g_objTimer.Interval = g_iBlinkTime;
    29.                 g_objTimer.Enabled = true;
    30.  
    31.                 //Remove any previous handlers
    32.                 g_objTimer.Tick -= Elapsed;
    33.                 g_objTimer.Tick += Elapsed;
    34.  
    35.             }
    36.  
    37.             if (g_bDefaultVisible == false & g_bIsBlinking == true) {
    38.                 g_objTimer.Enabled = false;
    39.                 g_objTimer.Tick -= Elapsed;
    40.  
    41.             }
    42.  
    43.  
    44.         }
    45.     }
    46.  
    47.     [System.ComponentModel.DefaultValue(0)]
    48.     public int BlinkTime {
    49.         get { return g_iBlinkTime; }
    50.  
    51.         set {
    52.             g_iBlinkTime = value;
    53.  
    54.  
    55.             if (base.DesignMode == false) {
    56.  
    57.                 if (g_iBlinkTime != 0) {
    58.                     //If the control's visible property is false
    59.                     //go into blink mode but dont show the control
    60.                     if (g_bDefaultVisible == true) {
    61.                         g_objTimer.Interval = g_iBlinkTime;
    62.                         g_objTimer.Enabled = true;
    63.  
    64.                         g_objTimer.Tick += Elapsed;
    65.                     }
    66.  
    67.                     g_bIsBlinking = true;
    68.  
    69.                 } else {
    70.                     g_objTimer.Enabled = false;
    71.                     g_objTimer.Tick -= Elapsed;
    72.                     base.Visible = g_bDefaultVisible;
    73.                     g_bIsBlinking = false;
    74.                 }
    75.             }
    76.         }
    77.     }
    78.  
    79.  
    80.     private void Elapsed(object sender, System.EventArgs e)
    81.     {
    82.         if (g_bVisible)
    83.             g_bVisible = false;
    84.         else
    85.             g_bVisible = true;
    86.  
    87.  
    88.         base.Visible = g_bVisible;
    89.     }
    90.  
    91. }
    92.  
    93. #endregion
    Its a BlinkLabel I wrote some months back for the exact same reason that you want it. I used it for a "Please wait" message while waiting for an SMO query for database instances to complete. Just use it like a normal label and set its BlinkTime property to something greater than zero for it to start blinking. I suggest 500(which is 500 milliseconds). Anything else seems too slow or too fast but then thats a matter of preference.

    Trivia: This was written in VB.Net....just used an online converter to get C# code.
    Thank you

  12. #12
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: [RESOLVED] timer on winform

    Also, if you're gonna use my BlinkLabel or even a marquee progress bar as jmc suggested, make sure that your long running method doesnt lock up the UI. Use timers or a different thread or even DoEvents so that the control, which ever one you're gonna use can redraw themselves.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2007
    Posts
    694

    Re: timer on winform

    Quote Originally Posted by Niya View Post
    While I agree with everything jmc said, I'll still provide this incase you still want to use it.
    csharp Code:
    1. #region "BlinkLabel"
    2.  
    3. public class BlinkLabel : Label
    4. {
    5.  
    6.     private System.Windows.Forms.Timer g_objTimer = new System.Windows.Forms.Timer();
    7.     private int g_iBlinkTime = 0;
    8.     private bool g_bVisible = true;
    9.  
    10.     private bool g_bIsBlinking = false;
    11.  
    12.     private bool g_bDefaultVisible = true;
    13.  
    14.     [System.ComponentModel.DefaultValue(true)]
    15.     public new bool Visible {
    16.         get { return g_bDefaultVisible; }
    17.  
    18.         set {
    19.             g_bDefaultVisible = value;
    20.  
    21.             if (base.DesignMode == true) {
    22.                 base.Visible = true;
    23.             } else {
    24.                 base.Visible = value;
    25.             }
    26.  
    27.             if (g_bDefaultVisible == true & g_bIsBlinking == true) {
    28.                 g_objTimer.Interval = g_iBlinkTime;
    29.                 g_objTimer.Enabled = true;
    30.  
    31.                 //Remove any previous handlers
    32.                 g_objTimer.Tick -= Elapsed;
    33.                 g_objTimer.Tick += Elapsed;
    34.  
    35.             }
    36.  
    37.             if (g_bDefaultVisible == false & g_bIsBlinking == true) {
    38.                 g_objTimer.Enabled = false;
    39.                 g_objTimer.Tick -= Elapsed;
    40.  
    41.             }
    42.  
    43.  
    44.         }
    45.     }
    46.  
    47.     [System.ComponentModel.DefaultValue(0)]
    48.     public int BlinkTime {
    49.         get { return g_iBlinkTime; }
    50.  
    51.         set {
    52.             g_iBlinkTime = value;
    53.  
    54.  
    55.             if (base.DesignMode == false) {
    56.  
    57.                 if (g_iBlinkTime != 0) {
    58.                     //If the control's visible property is false
    59.                     //go into blink mode but dont show the control
    60.                     if (g_bDefaultVisible == true) {
    61.                         g_objTimer.Interval = g_iBlinkTime;
    62.                         g_objTimer.Enabled = true;
    63.  
    64.                         g_objTimer.Tick += Elapsed;
    65.                     }
    66.  
    67.                     g_bIsBlinking = true;
    68.  
    69.                 } else {
    70.                     g_objTimer.Enabled = false;
    71.                     g_objTimer.Tick -= Elapsed;
    72.                     base.Visible = g_bDefaultVisible;
    73.                     g_bIsBlinking = false;
    74.                 }
    75.             }
    76.         }
    77.     }
    78.  
    79.  
    80.     private void Elapsed(object sender, System.EventArgs e)
    81.     {
    82.         if (g_bVisible)
    83.             g_bVisible = false;
    84.         else
    85.             g_bVisible = true;
    86.  
    87.  
    88.         base.Visible = g_bVisible;
    89.     }
    90.  
    91. }
    92.  
    93. #endregion
    Its a BlinkLabel I wrote some months back for the exact same reason that you want it. I used it for a "Please wait" message while waiting for an SMO query for database instances to complete. Just use it like a normal label and set its BlinkTime property to something greater than zero for it to start blinking. I suggest 500(which is 500 milliseconds). Anything else seems too slow or too fast but then thats a matter of preference.

    Trivia: This was written in VB.Net....just used an online converter to get C# code.
    Hi,
    Created a class inside winform project.
    Named it BlinkLabel which inherits from Label.

    Question:
    How do I use this for a form which has a label and needs to blink?
    Thanks

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: timer on winform

    Quote Originally Posted by arkiboys View Post
    Hi,
    Created a class inside winform project.
    Named it BlinkLabel which inherits from Label.

    Question:
    How do I use this for a form which has a label and needs to blink?
    Thanks
    It's a control. You use it like any other control. You use it INSTEAD of a regular Label.

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2007
    Posts
    694

    Re: [RESOLVED] timer on winform

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width