Visual Studio For Mac Subscribe To Eventhandler Trigger

Active1 year ago
  1. Visual Studio For Mac Subscribe To Eventhandler Triggered
  2. Visual Studio For Mac Subscribe To Eventhandler Triggered

I'm currently creating an application in C# using Visual Studio. I want to create some code so that when a variable has a value of 1 then a certain piece of code is carried out.I know that I can use an if statement but the problem is that the value will be changed in an asynchronous process so technically the if statement could be ignored before the value has changed.

Is it possible to create an event handler so that when the variable value changes an event is triggered? If so, how can I do this?

It is completely possible that I could have misunderstood how an if statement works! Any help would be much appreciated.

Click 'Microsoft.NET,' then click 'Visual Studio' to open the software. Open your VB.NET project, then double-click the VB.NET source code file you want to use to detect a keyboard key press. Click the Windows 'Start' button and select 'All Programs.' Click 'Microsoft.NET,' then click 'Visual Studio' to open the software. We make sure that the WorkStarted event handler is not null, then call the event handler passing in the class object o and the WorkStartedEventArgs object e. Now that we have all of our class library finished, let's get to work on the user interface and the code behind it.

Heinrich Ulbricht
7,6693 gold badges40 silver badges68 bronze badges
James Mundy

Visual Studio For Mac Subscribe To Eventhandler Triggered

James Mundy
1,9543 gold badges27 silver badges45 bronze badges

4 Answers

Seems to me like you want to create a property.

This allows you to run some code any time the property value changes. You could raise an event here, if you wanted.

Jonathan WoodJonathan Wood
43.5k59 gold badges202 silver badges321 bronze badges

You can use a property setter to raise an event whenever the value of a field is going to change.

You can have your own EventHandler delegate or you can use the famous System.EventHandler delegate.

Usually there's a pattern for this:

  1. Define a public event with an event handler delegate (that has an argument of type EventArgs).
  2. Define a protected virtual method called OnXXXXX (OnMyPropertyValueChanged for example). In this method you should check if the event handler delegate is null and if not you can call it (it means that there are one or more methods attached to the event delegation).
  3. Call this protected method whenever you want to notify subscribers that something has changed.

Here's an example

The advantage of this approach is that you let any other classes that want to inherit from your class to change the behavior if necessary.

If you want to catch an event in a different thread that it's being raised you must be careful not to change the state of objects that are defined in another thread which will cause a cross thread exception to be thrown. To avoid this you can either use an Invoke method on the object that you want to change its state to make sure that the change is happening in the same thread that the event has been raised or in case that you are dealing with a Windows Form you can use a BackgourndWorker to do things in a parallel thread nice and easy.

akappel
1291 gold badge2 silver badges15 bronze badges
Beatles1692Beatles1692

The .NET framework actually provides an interface that you can use for notifying subscribers when a property has changed: System.ComponentModel.INotifyPropertyChanged. This interface has one event PropertyChanged. Its usually used in WPF for binding but I have found it useful in business layers as a way to standardize property change notification.

In terms of thread safety I would put a lock under in the setter so that you don't run into any race conditions.

Here are my thoughts in code :) :

Hope this is helpful :)

Daniel SandbergDaniel Sandberg
Russell TroywestRussell TroywestVisual studio for mac subscribe to eventhandler trigger thumb
7,7032 gold badges28 silver badges36 bronze badges

Visual Studio For Mac Subscribe To Eventhandler Triggered

Not the answer you're looking for? Browse other questions tagged c#visual-studiosilverlightwindows-phone-7event-handling or ask your own question.