Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8165473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:32:41+00:00 2026-06-06T19:32:41+00:00

I want to add more functionality to a project I have that makes use

  • 0

I want to add more functionality to a project I have that makes use a number of classes packaged in the NET Framework. These same classes provide a number of properties which can be quite useful adapting the functionality of my project, however one thing that these classes lack is Events.

If each property had a appropriate event that would fire whenever the value of such property changed, I could then assign a event handler that would act based on those properties value.

I made a sample case bellow to illustrate my goal in the most simpler way I could think off.

Sample case:

The System.Net.Sockets.Socket class (Socket on MSDN Docs) has a property
named Connected that basically returns true if the socket is
connected to a specified end point otherwise returns false.

What I would like to accomplish is simple. I would like to keep this
property under “watch” and when the value of it changes, fire a event.

Doing that to one of my own classes it would be simple although a bit tiresome using the INotifyPropertyChanged interface, simply because always that my code changed the value of the property I would have to fire the event manually. Unfortunately, to best of my knowledge, not even this kind of procedure can be applied to the existing Socket class distributed within NET Framework.

Well, this question is becoming quite extensive, sorry, but I hope it gave an insight to my goal.

Now simply putting it, I want to watch the Connected property of the Socket class and when the value of it changes, fire an event. And if it would be possible to also use such approach to watch variables as well properties, it would be awesome, not just for me, but for everyone who stumbles across this question on SO.

A simple and lightweight approach is preferred of course, but most of all, I want to understand how it can be done, so in the future I can apply it in mass scale to other classes.

I realize I’m asking a lot. Many thanks.

Any questions just ask.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:32:44+00:00Added an answer on June 6, 2026 at 7:32 pm

    I implemented a basic class that should get you started. I’m sure a fully functional, production-ready, thread-safe class would require a bit more work, plus you need to implement your own strategy for when to poll for value changes.

    public class TargettedObserver<T>
    {
        private static readonly EqualityComparer<T> EqualityComparer = EqualityComparer<T>.Default;
    
        private Func<T> ValueTarget;
        private T OldValue;
    
        public event ObservedValueChangedEventHandler<T> ValueChanged;
    
        public TargettedObserver(Func<T> valueTarget)
        {
            this.ValueTarget = valueTarget;
            OldValue = ObtainCurrentValue();
        }
    
        public bool CheckValue()
        {
            T oldValue = OldValue;
            T newValue = ObtainCurrentValue();
    
            bool hasValueChanged = CompareValues(oldValue, newValue);
    
            if (hasValueChanged)
            {
                OldValue = newValue;
                NotifyValueChanged(oldValue, newValue);
            }
    
            return hasValueChanged;
        }
    
        private void NotifyValueChanged(T oldValue, T newValue)
        {
            var valueChangedEvent = ValueChanged;
            if (valueChangedEvent != null)
                valueChangedEvent(this, new ObservedValueChangedEventArgs<T>(oldValue, newValue));
        }
    
        private static bool CompareValues(T oldValue, T newValue)
        {
            return !EqualityComparer.Equals(oldValue, newValue);
        }
    
        private T ObtainCurrentValue()
        {
            return ValueTarget();
        }
    }
    

    And the event handling:

    public class ObservedValueChangedEventArgs<T> : EventArgs
    {
        public T OldValue { get; private set; }
        public T NewValue { get; private set; }
    
        public ObservedValueChangedEventArgs(T oldValue, T newValue)
        {
            this.OldValue = oldValue;
            this.NewValue = newValue;
        }
    }
    
    public delegate void ObservedValueChangedEventHandler<T>(TargettedObserver<T> observer, ObservedValueChangedEventArgs<T> eventArgs);
    

    Usage looks something like this:

    public class TestClass
    {
        private Socket MySocket;
        private static TargettedObserver<bool> SocketConnectedObserver;
    
        public void Main()
        {
            MySocket = new Socket();
            SocketConnectedObserver = new TargettedObserver<bool>(() => MySocket.Connected);
            SocketConnectedObserver.ValueChanged += ReportSocketConnectedStateChanged;
            PerformSocketConnection();
    
            MainThread.Invoke(PollSocketValue);
        }
    
        private void PollSocketValue()
        {
            SocketConnectedObserver.CheckValue();
            MainThread.Invoke(PollSocketValue);
        }
    
        private void ReportSocketConnectedStateChanged(TargettedObserver<bool> observer, ObservedValueChangedEventArgs<bool> eventArgs)
        {
            Console.WriteLine("Socket connection state changed!  OldValue: " + eventArgs.OldValue + ", NewValue: " + eventArgs.NewValue);
        }
    }
    

    Notice the constructor takes a simple lambda expression that can evaluate the value you’re wanting to observe.

    Also note that MainThread.Invoke is just a pseudocode to show it polling for a change on every main thread loop. I’m sure there are nicer strategies (background thread with a timer interval) for example that could be implemented in a nice, reusable way. Still more work to be done in terms of deregistering the observer. Could probably make some nice factory methods or lambda delegates so you don’t need to keep the TargettedObserver instance floating around and reduce the amount of wiring/manual code. But at least this should be a start.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented a binary search tree and I want to add more functionality
I have several asp.net website projects for various sites. Currently I want to add
Hello friends i want to add more effects on my div when i click
In my application i want to add more than two markers in a Google
I want to add some more library and helpers file into Pyrocms which is
I developed small CakePHP application, and now I want to add one more table
I want to add a context menu to the tableview, more specifically I want
Suppose I have a class MyClass to which I want to add certain 'observer'
Background: We have app a, b, and plan to add more apps into this
I want to add functionality like in twitter when scroll down div elongates horizontally

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.