Classes, events and best practices
-
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()
-
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()
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
-
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
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
-
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
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