Click here to Skip to main content
15,896,111 members
Home / Discussions / C#
   

C#

 
QuestionEvent for checking if a delegate/anonymous method result changed Pin
hoernchenmeister14-Dec-10 3:40
hoernchenmeister14-Dec-10 3:40 
AnswerRe: Event for checking if a delegate/anonymous method result changed Pin
Abhinav S14-Dec-10 5:14
Abhinav S14-Dec-10 5:14 
GeneralRe: Event for checking if a delegate/anonymous method result changed Pin
hoernchenmeister14-Dec-10 5:45
hoernchenmeister14-Dec-10 5:45 
AnswerRe: Event for checking if a delegate/anonymous method result changed Pin
Ian Shlasko14-Dec-10 5:25
Ian Shlasko14-Dec-10 5:25 
GeneralRe: Event for checking if a delegate/anonymous method result changed Pin
hoernchenmeister14-Dec-10 5:43
hoernchenmeister14-Dec-10 5:43 
GeneralRe: Event for checking if a delegate/anonymous method result changed Pin
Ian Shlasko14-Dec-10 6:07
Ian Shlasko14-Dec-10 6:07 
GeneralRe: Event for checking if a delegate/anonymous method result changed Pin
hoernchenmeister14-Dec-10 6:33
hoernchenmeister14-Dec-10 6:33 
GeneralRe: Event for checking if a delegate/anonymous method result changed Pin
Ian Shlasko14-Dec-10 7:30
Ian Shlasko14-Dec-10 7:30 
Well, if the control doesn't raise any useful events, your options are limited, and the timer solution is about all there is (Just make sure it's sleeping between checks, not calling continuously).

Maybe you could hide the complexity with private classes... I'm still in favor of the wrappers to merge all the different possibilities into a single event type... When you pass in the control, it'll check for a few common types (For a TextBox, it would look for TextChanged. For a checkbox, CheckedChanged. If it's an INotifyPropertyChanged, PropertyChanged). Depending on the type of control passed, it creates a different wrapper that hooks the necessary event and supplies a simple Changed event. If it can't find an event to hook, it falls back to the timer method...
public void MonitorObject(object obj, Delegate valueToMonitor) 
{
  if (obj is TextBox)
     Add(new TextChangedWatcher(obj as TextBox, valueToMonitor));
  else if ...
  else
     Add(new DefaultObjectWatcher(obj, valueToMonitor));
}

private class ObjectWatcher
{
  public event EventHandler Changed;
  private Delegate ResultCallback { get; set; }
  private object PreviousValue { get; set; }
  
  protected void OnCheckValue()
  {
    object newValue = ResultCallback.Invoke();
    if (!object.Equals(PreviousValue, newValue))
    {
      PreviousValue = newValue;
      if (Changed != null) Changed(this, EventArgs.Empty);
    }
  }
}
private class TextChangedWatcher : ObjectWatcher
{
  public TextChangedWatcher(TextBox ctl) 
  { 
    // hook TextChanged...
  }
}
private class CheckedChangedWatcher : ObjectWatcher
{
  // hook CheckedChanged
}
private class NotifyObjectWatcher : ObjectWatcher
{
  // hook PropertyChanged
}
private class DefaultObjectWatcher : ObjectWatcher
{
  // Create a timer
}

Anyway... The idea would be to hook events when possible, and only fall back to the timer when there are no known events to hook.
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels)

GeneralRe: Event for checking if a delegate/anonymous method result changed Pin
hoernchenmeister14-Dec-10 20:38
hoernchenmeister14-Dec-10 20:38 
QuestionDynamic Polymorphism Pin
Baji Jabbar13-Dec-10 23:31
Baji Jabbar13-Dec-10 23:31 
AnswerRe: Dynamic Polymorphism Pin
Hiren solanki13-Dec-10 23:48
Hiren solanki13-Dec-10 23:48 
AnswerRe: Dynamic Polymorphism Pin
Richard MacCutchan13-Dec-10 23:49
mveRichard MacCutchan13-Dec-10 23:49 
GeneralRe: Dynamic Polymorphism Pin
Hiren solanki13-Dec-10 23:57
Hiren solanki13-Dec-10 23:57 
GeneralRe: Dynamic Polymorphism Pin
Baji Jabbar14-Dec-10 0:00
Baji Jabbar14-Dec-10 0:00 
GeneralRe: Dynamic Polymorphism Pin
Pete O'Hanlon14-Dec-10 0:43
mvePete O'Hanlon14-Dec-10 0:43 
GeneralRe: Dynamic Polymorphism Pin
Richard MacCutchan14-Dec-10 1:02
mveRichard MacCutchan14-Dec-10 1:02 
Questionadding new data over ProcessLayer Pin
Erdinc2713-Dec-10 22:12
Erdinc2713-Dec-10 22:12 
AnswerRe: adding new data over ProcessLayer Pin
Hiren solanki13-Dec-10 23:17
Hiren solanki13-Dec-10 23:17 
GeneralRe: adding new data over ProcessLayer Pin
Erdinc2713-Dec-10 23:46
Erdinc2713-Dec-10 23:46 
AnswerRe: adding new data over ProcessLayer Pin
Hiren solanki13-Dec-10 23:55
Hiren solanki13-Dec-10 23:55 
GeneralRe: adding new data over ProcessLayer Pin
Erdinc2714-Dec-10 0:15
Erdinc2714-Dec-10 0:15 
AnswerRe: adding new data over ProcessLayer Pin
Hiren solanki14-Dec-10 0:23
Hiren solanki14-Dec-10 0:23 
GeneralRe: adding new data over ProcessLayer Pin
Erdinc2714-Dec-10 0:40
Erdinc2714-Dec-10 0:40 
AnswerRe: adding new data over ProcessLayer Pin
Hiren solanki14-Dec-10 0:45
Hiren solanki14-Dec-10 0:45 
GeneralRe: adding new data over ProcessLayer Pin
Erdinc2714-Dec-10 0:47
Erdinc2714-Dec-10 0:47 

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.