Click here to Skip to main content
15,891,136 members
Home / Discussions / C#
   

C#

 
GeneralRe: does MethodInvoker will not work on win XP? Pin
Gilbert Consellado2-Feb-16 2:17
professionalGilbert Consellado2-Feb-16 2:17 
QuestionRe: does MethodInvoker will not work on win XP? Pin
Alan N1-Feb-16 23:09
Alan N1-Feb-16 23:09 
AnswerRe: does MethodInvoker will not work on win XP? Pin
Gilbert Consellado2-Feb-16 2:19
professionalGilbert Consellado2-Feb-16 2:19 
QuestionRetrieve a date from the result of a query in a variable c # Pin
Ibrahim.elh31-Jan-16 23:07
Ibrahim.elh31-Jan-16 23:07 
AnswerRe: Retrieve a date from the result of a query in a variable c # Pin
Sascha Lefèvre31-Jan-16 23:25
professionalSascha Lefèvre31-Jan-16 23:25 
GeneralRe: Retrieve a date from the result of a query in a variable c # Pin
Ibrahim.elh1-Feb-16 3:05
Ibrahim.elh1-Feb-16 3:05 
GeneralRe: Retrieve a date from the result of a query in a variable c # Pin
Sascha Lefèvre1-Feb-16 3:16
professionalSascha Lefèvre1-Feb-16 3:16 
QuestionReactive Extensions and WeakEvents Pin
Kenneth Haugland30-Jan-16 3:23
mvaKenneth Haugland30-Jan-16 3:23 
I'm basically following the approach here:
Functional Fun: Weak Events in .Net, the easy way[^]

So I designed a helper to get the IObserveble:
C#
public static IObservable<EventPattern<PropertyChangedEventArgs>> ObservePropertyChanged(this INotifyPropertyChanged collection, string PropertyName)
{
    return Observable.FromEventPattern<PropertyChangedEventHandler, PropertyChangedEventArgs>(
            handler => (sender, e) => handler(sender, e),
            handler => collection.PropertyChanged += handler,
            handler => collection.PropertyChanged -= handler)
            .Where(evt => evt.EventArgs.PropertyName == PropertyName);
}

Then I copied the SubscribeWeakly method:
C#
public static IDisposable SubscribeWeakly<T, TTarget>(this IObservable<T> observable, TTarget target, Action<TTarget, T> onNext) where TTarget : class
{
    var reference = new WeakReference(target);

    //if (onNext.Target != null)
    //{
    ////    throw new ArgumentException("onNext must refer to a static method, or else the subscription will still hold a strong reference to target");
    //}

    IDisposable subscription = null;
    subscription = observable.Subscribe(item =>
    {
        var currentTarget = reference.Target as TTarget;
        if (currentTarget != null)
        {
            onNext(currentTarget, item);
        }
        else
        {
            subscription.Dispose();
        }
    });

    return subscription;
}

My question is simply, is this really all that is required to create a WeakEvent Listener with Rx? I'm really confused as the ObservePropertyChange method subscribes to the event, seemingly, in the normal strong reference way. I thought Rx changed the event into a sequence of the type you are getting, and that it enables common methods you would like to use in async programming?

Also, the code blocks that I commented out, never returned null, even in the code originally posted on the web page. It was supposed to check if the onNext was connected to any non-static incidence, but as far as I can tell, that will never happen?

I also have sort of a side question, and that is how do I enable to write an Expression in the code that allows me to write:
ObservePropertyChanged(Myclass, s => s.MyPropertyInMyClass)
I understand how to use normal Expressions like:
C#
Expression<Func<int, int, string>> expression = (a, b) => "soemthing";

What I dont get, is how to referance the incomming class, and specify the returning property?
AnswerRe: Reactive Extensions and WeakEvents Pin
Sascha Lefèvre30-Jan-16 4:20
professionalSascha Lefèvre30-Jan-16 4:20 
GeneralRe: Reactive Extensions and WeakEvents Pin
Kenneth Haugland30-Jan-16 5:27
mvaKenneth Haugland30-Jan-16 5:27 
GeneralRe: Reactive Extensions and WeakEvents Pin
Sascha Lefèvre30-Jan-16 6:20
professionalSascha Lefèvre30-Jan-16 6:20 
GeneralRe: Reactive Extensions and WeakEvents Pin
Kenneth Haugland30-Jan-16 7:29
mvaKenneth Haugland30-Jan-16 7:29 
GeneralRe: Reactive Extensions and WeakEvents Pin
Sascha Lefèvre30-Jan-16 8:06
professionalSascha Lefèvre30-Jan-16 8:06 
AnswerRe: Reactive Extensions and WeakEvents Pin
Sascha Lefèvre30-Jan-16 11:42
professionalSascha Lefèvre30-Jan-16 11:42 
GeneralRe: Reactive Extensions and WeakEvents Pin
Kenneth Haugland31-Jan-16 1:19
mvaKenneth Haugland31-Jan-16 1:19 
GeneralRe: Reactive Extensions and WeakEvents Pin
Sascha Lefèvre31-Jan-16 1:42
professionalSascha Lefèvre31-Jan-16 1:42 
GeneralRe: Reactive Extensions and WeakEvents Pin
Kenneth Haugland31-Jan-16 4:24
mvaKenneth Haugland31-Jan-16 4:24 
GeneralRe: Reactive Extensions and WeakEvents Pin
Sascha Lefèvre31-Jan-16 6:34
professionalSascha Lefèvre31-Jan-16 6:34 
GeneralRe: Reactive Extensions and WeakEvents Pin
Kenneth Haugland31-Jan-16 7:29
mvaKenneth Haugland31-Jan-16 7:29 
GeneralRe: Reactive Extensions and WeakEvents Pin
Kenneth Haugland31-Jan-16 11:27
mvaKenneth Haugland31-Jan-16 11:27 
QuestionTableLayoutPanel and .. surprise surprise .. weird behaviour 8) Pin
Emanuele Bonin30-Jan-16 1:24
Emanuele Bonin30-Jan-16 1:24 
Answer[SOLVED] TableLayoutPanel and .. surprise surprise .. weird behaviour 8) Pin
Emanuele Bonin30-Jan-16 2:17
Emanuele Bonin30-Jan-16 2:17 
GeneralRe: [SOLVED] TableLayoutPanel and .. surprise surprise .. weird behaviour 8) Pin
BillWoodruff30-Jan-16 2:46
professionalBillWoodruff30-Jan-16 2:46 
GeneralRe: [SOLVED] TableLayoutPanel and .. surprise surprise .. weird behaviour 8) Pin
Emanuele Bonin30-Jan-16 6:18
Emanuele Bonin30-Jan-16 6:18 
AnswerRe: TableLayoutPanel and .. surprise surprise .. weird behaviour 8) Pin
BillWoodruff30-Jan-16 2:39
professionalBillWoodruff30-Jan-16 2:39 

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.