internal string RaiseOnPromptEvent(string Message) { if (_loginPassword != null && Message == "Enter password: ") { //no need to raise an event... return(_loginPassword); } // Make a temporary copy of the event to avoid possibility of // a race condition if the last subscriber unsubscribes // immediately after the null check and before the event is raised. OnPromptEventHandler handler = OnPrompt; // Event will be null if there are no subscribers if (handler != null) { P4PromptEventArgs e = new P4PromptEventArgs(Message); // Use the () operator to raise the event. handler(this, e); return(e.Response); } else { return(string.Empty); } }
// Wrap event invocations inside a protected virtual method // to allow derived classes to override the event invocation behavior private string RaiseOnPromptEvent(string Message) { // Make a temporary copy of the event to avoid possibility of // a race condition if the last subscriber unsubscribes // immediately after the null check and before the event is raised. OnPromptEventHandler handler = OnPrompt; // Event will be null if there are no subscribers if (handler != null) { P4PromptEventArgs e = new P4PromptEventArgs(Message); // Use the () operator to raise the event. handler(this, e); return e.Response; } else { return string.Empty; } }
private void HandleOnPrompt(object sender, P4PromptEventArgs e) { e.Response = RaiseOnPromptEvent(e.Message); }