Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Classes, events and best practices

Classes, events and best practices

Scheduled Pinned Locked Moved C#
csharphelpquestionlearning
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Raybarg
    wrote on last edited by
    #1

    Hi, I have just recently been jumped into C# and its all been a nice learning process. Many times I've had thought to ask here some questions but while typing in the question I have come to some ideas or questions that with google I could solve myself, coming into working solution. So now I am asking is this a good practice... What problem I had: Multiform MDI desktop application which had a expiring login system based on time from last user activity. Login dialog would be popped only when certain functionality is launched by user activity. When login has expired the application would just disable certain functionality. Reason for this behavior is to have the application work as a "viewer" all the time and require login only when certain "writing operation" would occur. Generalizing the problem I thought I need object to maintain whole application (class where the entry point is) which would handle all this using class that's designed to handle all login-related functionality. So, I wrote a "Login" class which is defined in "Program" class. I also wrote new class that inherits System.Windows.Forms.Form and all my forms inherit from this class. This "customForm" class I have all the basic login functionality:

    public partial class customForm : Form
    {
        public event EventHandler<EventArgs> RefreshActivityEvent;
        public void LoginInvalidated(object sender, EventArgs e)
        {
        }
        protected virtual void OnRefreshActivity(object source, EventArgs e)
        {
            EventHandler<EventArgs> handler = RefreshActivityEvent;
            if (handler != null)
            {
                handler(this, e);
            }
        }
    }
    public class Login
    {
        private System.Timers.Timer InvalidationTimer;
        public event EventHandler<EventArgs> TimedEvent;
        public string foo;
    
        public Login()
        {
            InvalidationTimer = new System.Timers.Timer();
            InvalidationTimer.Interval = 10000;
            InvalidationTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            InvalidationTimer.AutoReset = false;
            InvalidationTimer.Start();
        }
    
        protected virtual void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            EventHandler<EventArgs> handler = TimedEvent;
            if (handler != null)
            {
                handler(this, e);
            }
        }
        public virtual void Restart()
    
    S 1 Reply Last reply
    0
    • R Raybarg

      Hi, I have just recently been jumped into C# and its all been a nice learning process. Many times I've had thought to ask here some questions but while typing in the question I have come to some ideas or questions that with google I could solve myself, coming into working solution. So now I am asking is this a good practice... What problem I had: Multiform MDI desktop application which had a expiring login system based on time from last user activity. Login dialog would be popped only when certain functionality is launched by user activity. When login has expired the application would just disable certain functionality. Reason for this behavior is to have the application work as a "viewer" all the time and require login only when certain "writing operation" would occur. Generalizing the problem I thought I need object to maintain whole application (class where the entry point is) which would handle all this using class that's designed to handle all login-related functionality. So, I wrote a "Login" class which is defined in "Program" class. I also wrote new class that inherits System.Windows.Forms.Form and all my forms inherit from this class. This "customForm" class I have all the basic login functionality:

      public partial class customForm : Form
      {
          public event EventHandler<EventArgs> RefreshActivityEvent;
          public void LoginInvalidated(object sender, EventArgs e)
          {
          }
          protected virtual void OnRefreshActivity(object source, EventArgs e)
          {
              EventHandler<EventArgs> handler = RefreshActivityEvent;
              if (handler != null)
              {
                  handler(this, e);
              }
          }
      }
      public class Login
      {
          private System.Timers.Timer InvalidationTimer;
          public event EventHandler<EventArgs> TimedEvent;
          public string foo;
      
          public Login()
          {
              InvalidationTimer = new System.Timers.Timer();
              InvalidationTimer.Interval = 10000;
              InvalidationTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
              InvalidationTimer.AutoReset = false;
              InvalidationTimer.Start();
          }
      
          protected virtual void OnTimedEvent(object source, ElapsedEventArgs e)
          {
              EventHandler<EventArgs> handler = TimedEvent;
              if (handler != null)
              {
                  handler(this, e);
              }
          }
          public virtual void Restart()
      
      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      Looks fine to me, except for class names. I would also move the hooking up of LoginObject and form to a separate class. If there is more than one form, I'd also consider storing them in an array instead of individual variables. And oh, you're firing the TimedEvent from the System.Timers.Timer thread and have hooked up LoginInvalidated method on System.Windows.Form. Unless you're absolutely sure that you're not going to touch any UI objects, you'll have to use Invoke/BeginInvoke[^]

      Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

      R 1 Reply Last reply
      0
      • S S Senthil Kumar

        Looks fine to me, except for class names. I would also move the hooking up of LoginObject and form to a separate class. If there is more than one form, I'd also consider storing them in an array instead of individual variables. And oh, you're firing the TimedEvent from the System.Timers.Timer thread and have hooked up LoginInvalidated method on System.Windows.Form. Unless you're absolutely sure that you're not going to touch any UI objects, you'll have to use Invoke/BeginInvoke[^]

        Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

        R Offline
        R Offline
        Raybarg
        wrote on last edited by
        #3

        So you mean my LoginInvalidated method will be running in different thread than my System.Windows.Form object... Looks like I dug too deep finding implementation to this and it's time to try something else. Guess if I keep my MDI parent form as the host and run System.Windows.Forms.Timer to tell me about the expiration, I will be running the timer in same message pump and am safe to touch UI elements in the MDI parent and its MDI childs as theyre shown using .Show method? And, uh, how would you store multiple forms in an array? (edit) OR I could use static class for running the timer and just read its properties to find out the status of login? It would be less fancy though ;)

        modified on Thursday, June 4, 2009 2:37 AM

        S 1 Reply Last reply
        0
        • R Raybarg

          So you mean my LoginInvalidated method will be running in different thread than my System.Windows.Form object... Looks like I dug too deep finding implementation to this and it's time to try something else. Guess if I keep my MDI parent form as the host and run System.Windows.Forms.Timer to tell me about the expiration, I will be running the timer in same message pump and am safe to touch UI elements in the MDI parent and its MDI childs as theyre shown using .Show method? And, uh, how would you store multiple forms in an array? (edit) OR I could use static class for running the timer and just read its properties to find out the status of login? It would be less fancy though ;)

          modified on Thursday, June 4, 2009 2:37 AM

          S Offline
          S Offline
          S Senthil Kumar
          wrote on last edited by
          #4

          Raybarg wrote:

          So you mean my LoginInvalidated method will be running in different thread than my System.Windows.Form objec

          Yes.

          Raybarg wrote:

          Guess if I keep my MDI parent form as the host and run System.Windows.Forms.Timer to tell me about the expiration, I will be running the timer in same message pump and am safe to touch UI elements in the MDI parent and its MDI childs as theyre shown using .Show method?

          Yes.

          Raybarg wrote:

          And, uh, how would you store multiple forms in an array?

          Form []forms = new Form[2];
          forms[0] = FirstForm;
          forms[1] = SecondForm;
          ...

          or if the number of forms is not fixed, you could use a List <Form> instead.

          Raybarg wrote:

          I could use static class for running the timer and just read its properties to find out the status of login? It would be less fancy though

          I'd rather do it the way you're doing right now - firing a TimerExpired event.

          Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups