Click here to Skip to main content
15,887,027 members
Home / Discussions / C#
   

C#

 
GeneralRe: How do i learn c# programming fast Pin
Luc Pattyn3-Jun-09 10:01
sitebuilderLuc Pattyn3-Jun-09 10:01 
GeneralRe: How do i learn c# programming fast Pin
Luc Pattyn3-Jun-09 10:08
sitebuilderLuc Pattyn3-Jun-09 10:08 
GeneralRe: How do i learn c# programming fast Pin
Nagy Vilmos3-Jun-09 21:34
professionalNagy Vilmos3-Jun-09 21:34 
AnswerRe: How do i learn c# programming fast Pin
Christian Graus3-Jun-09 10:31
protectorChristian Graus3-Jun-09 10:31 
AnswerRe: How do i learn c# programming fast Pin
Guffa3-Jun-09 18:58
Guffa3-Jun-09 18:58 
QuestionNeed help with this Excel sheet issue - sheet listed that is not in the Excel file. [modified] Pin
DeepToot3-Jun-09 6:31
DeepToot3-Jun-09 6:31 
AnswerRe: Need help with this Excel sheet issue - sheet listed that is not in the Excel file. Pin
Mycroft Holmes3-Jun-09 14:27
professionalMycroft Holmes3-Jun-09 14:27 
QuestionClasses, events and best practices Pin
Raybarg3-Jun-09 5:54
professionalRaybarg3-Jun-09 5:54 
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()
    {
        InvalidationTimer.Stop();
        InvalidationTimer.Start();
    }
}


"Login" class has event to fire when timer triggers.
"customForm" class has event to fire when user activity occurs (to reset the timer).
In "Program" class constructor I add delegates to each of these events:
FirstForm.RefreshActivityEvent += new System.EventHandler<EventArgs>(RefreshActivity);
SecondForm.RefreshActivityEvent += new System.EventHandler<EventArgs>(RefreshActivity);
LoginObject.TimedEvent += new System.EventHandler<EventArgs>(FirstForm.LoginInvalidated);
LoginObject.TimedEvent += new System.EventHandler<EventArgs>(SecondForm.LoginInvalidated);

So, Program.RefreshActivity() will reset the LoginObject's timer. customForm.LoginInvalidated() is method I can overwrite to add "disabling features" code for each form and then call base class LoginInvalidated() method to maintain basic login functionality.

What I achieve with this is when timer reaches end, each form "knows" the login has expired... and when each form have any user activity, the main program knows this (in RefreshActivity method).

As this is the very first time I do anything like this, I would like experienced opinion if this is the "right way" of doing this?


Next step is to make event for login request in customForm class which would fire Login object in the Program object to pop up the login dialog and forward the information of the login status to the form, also firing LoginValidated in order to enable features that require login. But before I proceed in this, I'd like to have the comfort of knowing I am not doing this the "hard way" just because there's a feature I have not yet stumbled on that would be commonly used for something like this. Smile | :)

Sorry for the long post.
AnswerRe: Classes, events and best practices Pin
S. Senthil Kumar3-Jun-09 10:41
S. Senthil Kumar3-Jun-09 10:41 
GeneralRe: Classes, events and best practices [modified] Pin
Raybarg3-Jun-09 20:28
professionalRaybarg3-Jun-09 20:28 
GeneralRe: Classes, events and best practices Pin
S. Senthil Kumar3-Jun-09 23:29
S. Senthil Kumar3-Jun-09 23:29 
Questionmysql connector Pin
benjamin yap3-Jun-09 4:59
benjamin yap3-Jun-09 4:59 
AnswerRe: mysql connector Pin
Ennis Ray Lynch, Jr.3-Jun-09 5:03
Ennis Ray Lynch, Jr.3-Jun-09 5:03 
GeneralRe: mysql connector Pin
benjamin yap3-Jun-09 5:13
benjamin yap3-Jun-09 5:13 
GeneralRe: mysql connector Pin
Ennis Ray Lynch, Jr.3-Jun-09 5:14
Ennis Ray Lynch, Jr.3-Jun-09 5:14 
GeneralRe: mysql connector Pin
benjamin yap3-Jun-09 5:21
benjamin yap3-Jun-09 5:21 
AnswerRe: mysql connector Pin
Christian Graus3-Jun-09 10:32
protectorChristian Graus3-Jun-09 10:32 
QuestionArguments! C# Pin
Baeltazor3-Jun-09 3:51
Baeltazor3-Jun-09 3:51 
AnswerRe: Arguments! C# Pin
Pete O'Hanlon3-Jun-09 3:59
mvePete O'Hanlon3-Jun-09 3:59 
GeneralRe: Arguments! C# Pin
Baeltazor3-Jun-09 4:04
Baeltazor3-Jun-09 4:04 
GeneralRe: Arguments! C# Pin
Nagy Vilmos3-Jun-09 4:12
professionalNagy Vilmos3-Jun-09 4:12 
GeneralRe: Arguments! C# Pin
Pete O'Hanlon3-Jun-09 4:28
mvePete O'Hanlon3-Jun-09 4:28 
GeneralRe: Arguments! C# Pin
Nagy Vilmos3-Jun-09 4:37
professionalNagy Vilmos3-Jun-09 4:37 
GeneralRe: Arguments! C# Pin
Luc Pattyn3-Jun-09 6:09
sitebuilderLuc Pattyn3-Jun-09 6:09 
GeneralRe: Arguments! C# Pin
0x3c03-Jun-09 4:18
0x3c03-Jun-09 4:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.