C# Event handler Behavior - c#

I have this code
List<DaSubscription> lstSubscription=new List<DaSubscription>();
for(int i=0;i<20;i++)//20 is just to simulate the behavior
{
DaSubscription Generic=new DaSubscription();
Generic.DataChanged += new DataChangedEventHandler(Generic_DataChanged);
lstSubscription.add(Generic);
}
//EVENT Handler which is raised from a 3rd party library [COM]
void Generic_DataChanged(DaSubscription aDaSubscription, DaItem[] items, ValueQT[] values, int[] results)
{
UpdateDataChangedDTO(items, values);
}
As the same event handler [m_daSubscription_Generic_DataChanged] is assigned to the multiple instance of same class [m_daSubscription]. Question i have is if at the same time multiple instances invokes this handler how will be handled here. will there will be any instance it shall overwrite the data. or the event handler will be separate for each instance.

The event handlers execute seperately. It sounds like your worried about the parameters being overwritten by another call to the handler. That won't happen (I don't think it is even possible). Since it doesn't look like you are accessing any shared objects in the event handler, you should be perfectly safe.

Related

How do I make a function that does NOT run when a specific event is fired

Edit: For clarification, this is in Unity, and is tied to Update(). So the events will be triggered once per frame for any number of frames. Both events can be triggered independently by the user, but there is an overlap in which both can be triggered. In this case I only want one of the two methods that will be called by the two events to actually run.
I'm trying to make a method that is called by one event and other method that is called by a 2nd event be linked in a way that only one of those two methods can be called or ran at the same time.
The events are triggered by an action of the user, both events can be triggered at the same time. But I only want one of the two methods that would be called to actually run if both events are triggered.
I was thinking something along the like of the following example, obviously the example may not be accurate, but the gist of what I am trying to accomplish is there.
Pseudo Code Example:
public delegate void OneHandler();
public event OneHandler OneEvent;
private void One()
{
if(some requirement && TEvent != null)
{
TEvent();
}
}
public delegate void TwoHandler();
public event TwoHandler TwoEvent;
private void Two()
{
if(some requirement && TEvent != null)
{
TEvent();
}
}
SomeClass.OneEvent += ActionOne();
SomeClass.TwoEvent += ActionTwo();
private void ActionOne()
{
if(TwoEvent is not firing)
{
Do Something;
}
}
private void ActionTwo()
{
if(OneEvent is not firing)
{
Do Something;
}
}
A crude diagram. The tan area moves down/left, the green areas move diagonally. I essentially want the tan area which have their own events, to not run their methods if the mouse is within that entire area.
Events fire one at a time, not all at once. Even if the same user action was going to cause both events to fire one after the other, there is no way to know that both events will fire, or in what order they will fire.
To be more clear: I didn't mean events can never be fired concurrently. I mean that, in general, there is no piece of code that determines, for a particular situation, the complete set of events which will be fired. You can't tell ahead of time which will fire, or in which order, or even on which threads.
For example, a particular user interface action (or user gesture) might cause several events to fire. In general, you can't depend on the number of events to be fired, or the order in which they will be fired.
There are some situations, like in ASP.NET web forms, where you can be assured that if the user clicks a button which has a Click event handler, that the page Load event will fire and then that the button Click event will fire. But this is because this behavior is defined and documented that way. If the order changed, or if, for instance, the page Load event stopped firing, then this would break a large number of ASP.NET web forms applications.
If all you're trying to do is guarantee that concurrent triggering (like two threads running on different cores) will never be allowed, you'll want to use something like a mutex. You would have both handlers perform a WaitOne(0) to see if the other handler is working, returning immediately if the call returns false (since the other method must be executing) then in a guaranteed-execution block (such as a finally) you would release the mutex.
How about use bool variable as flag ?
private bool flag[2] = [false, false];
private void ActionOne()
{
flag[0] = true;
if(!(flag[0])&&flag[1]))
{
Do Something;
}
flag[0] = false;
}
private void ActionTwo()
{
flag[1] = true;
if(!(flag[0] && flag[1]))
{
Do Something;
}
flag[1] = false;
}

how to handle events: array of controls with c sharp (old com control)

I'm trying to work with an old com control (a control array), the following samples: 5435293, 39541, 5497403, 5738092 explain (or at least what I understand) how to handle events of control arrays with .net controls, so they have Sender and EventArgs.
My question will be: How can you handle the events of an old com control array?.
EDIT:
The array will be created dynamically at the start, for example: Q. How many connections do you want? A. 5
example:
the control has this event: control_connected(int status, string description)
I can make some function with the same arguments and asign it to the connected event, but i cant figure out how to do it with a control array.
Ty so much for your help, and sorry about the crappy English... I'm not a navite English speaker
COM events have a different modal, you do not have one handler per event, you have an event sink object that hooks every event the COM server plan to raise. If you just hook the ActiveX events with delegates, event sink RCWs will be created and may cause crashes later, so I assume you are creating your own event sink class.
Since you have your own event sink class, you must follow the event publisher's event signature. The signatures do not have a sender argument, since the COM server assumes you have a reference to the sender, thus there is no need to send it again every time an event is raised.
You can, of cause, cache the server's reference in your event sink object for later use. Your event sink object can declare its own version of managed events with a sender parameter, and pass the cached COM server as the sender argument when it raises events.
Something like
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
[TypeLibType(TypeLibTypeFlags.FHidden)]
[Guid("eventGuid")]
[CLSCompliant(false)]
public interface IEvent
{
[DispId(123)]
void control_connected(int status, string description);
}
public class EventSink:IEvent
{
object control;
public EventSink (object control)
{
this.control=control;
}
public event EventHandler<ControlConnectedEventArgs> ControlConnected;
void control_connected(int status, string description);
{
EventHandler<ControlConnectedEventArgs> temp=this.ControlConnected;
if(temp!=null)
temp(this.control, new ControlConnectedEventArgs(status,description));
}
}
If you have an array of COM servers, just declare an array of event sinks, attach each sink to each COM server with ConnectionPointCookie, and wire the event handlers from the event sink instead of the COM servers.

vb.net c# static event?

I have a plugin that creates an object that has an event. Is there a way for a seperate class to monitor this event, even though it does not have control of the object?
For example, I have a plugin that calls an object that uploads some data. When the data is uploaded, the event is triggered to say that this is so. In my client app, I want this to be notified when this event triggers so that it can do something.
The plugin and client will be in C#, and the event class is written in VB.net.
Would it simply be a case of the object notifying the plugin, and in turn, the plugin notifying the client app?
Is this possible?
Thanks.
EDIT (in response to request for code):
The code is fairly simple, it will be a case of in the DLL:
Object O = new Object();
O.CompleteEvent += (BLAH BLAH);
O.Run();
Now when run completes, it will trigger the CompleteEvent.
I want it to be noticed that this event has triggered within a completely different assembly that does not have the O object. The new assembly can monitor the event, however it will be outside the scope of the O object. I hope i'm explaining this okay?
As mentioned earlier, would it simply be a case of the dll then notifying the client assembly that the run is complete?
Events can be static or instance, just like any other class members. And you can subscribe static and instance methods to either type.

When observable created with Observable.FromEvent will be detached from event producer?

UPDATE: found similar question: Rx - unsubscribing from events
Given the code:
interface IBitmapCapturer {
event EventHandler<EventArgs<RawBitmap>> Captured;
void Start();
void Stop();
}
public class Camera {
IBitmapCapturer m_capturer;
public RawBitmap CaptureBitmap() {
IObservable<IEvent<EventArgs<RawBitmap>>> observable = Observable.FromEvent<EventArgs<RawBitmap>>(
handler => m_capturer.Captured += handler,
handler => m_capturer.Captured -= handler);
m_capturer.Start();
IEvent<EventArgs<RawBitmap>> evn = observable.First();
m_capturer.Stop();
return evn.EventArgs.EventData;
}
}
What I'm doing here is capturing a shot from Camera but the only first one. IBitmapCapturer is some DirectShow-related stuff and doesn't provide a method to "make a shot", only generating events with bitmaps. So I'm using Reactive Extensions to take the first event and unsubscribe.
My doubts are about "observable.First()" line.
Do I understand correctly that after calling observable.First() my observable is detached from m_capturer (event producer)? If not then how can I make sure that there're no subscribers on m_capturer.Captured?
The framework ensures that the IDisposable value returned from Subscribe is disposed of when the sequence completes or errors, and the IDisposable returned by FromEvent unsubscribes from the event.
Since First is a blocking implementation of Take(1), the source sequence will be disposed after the first value is received (or after it throws an exception when the source is empty).
In short, you can be sure that the event handler is being removed after your call to First completes.

Is it possible to attach an event handler before constructing an instance?

My app uses a logging class which is invoked by each module as it is constructed.
The logging class fires an event every time a new entry is added, so that the GUI can be updated.
Is there any way I can listen to events fired during the construction of an instance?
For instance, I currently have this in my calling class:
input = new Inputs.Webcam();
input.log.LogUpdate += new LogUpdateHandler(...);
But I also write to the log during the construction of the modules. (Currently this throws an error because there isn't a listener.) Is there any way to listen to these events?
This is completely impossible.
Instead, you can use a static event.
Could not you just pass the log handler method to the ctor?
var input = new Inputs.Webcam(new LogUpdateHandler(...));

Categories

Resources