In a dataflow, I have two different events that I want to cause the same action to be triggered.
This is inside of a dataflow symbol. One is a trigger symbol property, the other is the onLoop event of a Stopwatch block. I'm trying to bind these both to the invoke trigger on a State block.
If I drag one, then the other to the invoke trigger, it only keeps the most recent binding.
I tried binding the symbol property to the onLoop event on the Stopwatch, and then binding that to the invoke action on the State block. And this works when I invoke the trigger from the symbol properties. But when in use, this does not seem to work, and only the Stopwatch's onLoop is actually invoking the trigger.
Bind your trigger property and your onLoop property to an "Event Hub" block. Then bind your Event Hub to the invoke property of your action block. The Event Hub will effectively do an "or" for you.
Related
How can I make sure I have received event (A) from an aggregate and event (B) from another aggregate before invoking a method from a third aggregate in the event handler? For example, I need productPaid event and productInStock event to buy a product?
Maybe you can do a query in the handler to some aggregate to check if the other event already happened.
Or, you need a "process manager" instead of an "event handler".
You need a state that is shared by the two event handlers, something that when you receive an event allows you to record that it happened (some storage), and when you handle an event you can check if you already received the other one.
You can think to a process manager like an aggregate that receives events instead of commands (it cannot reject them!)
we started using vs code analysis to improve our code base
is it possible to supress ca1801 - review unused parameters in event handlers?
we have thousands of event handlers like
Private Sub lsbRatings_Loaded(sender As ListBox, e As RoutedEventArgs)
and often times we dont utilize the passes parameters, but we dont really have a choice in the signature...
though i do want the warning to show when there's a truly unused parameter somewhere in code
thanks!
CA1801 does attempt to ignore event handlers. However, it identifies them based on a signature that matches the standard convention for .NET event handlers, including an assumption that the sender argument will be of type System.Object. Since your sender is of type ListBox, it is not being recognized by the rule as an event handler.
Is it equivalent/better to work
with the Event module on Event type
or with Observable on the publish property of an event
Functionally it seems equivalent, and I guess the difference is 'semantic' :
Are we inside the boundary where it makes sense to have access to the internal state of
the event ?
Or are we considering this event interface as a passive source from which a stream was exposed to us
Is that the correct thinking ?
The main difference between Event and Observable is in how they handle state and un-subscription.
Event functions attach to the source event and do not give you any way to unsubscribe. If you use stateful combinators (like Event.scan) and then attach multiple observers to the resulting event, then they will all see the same state.
Observable functions construct "specification" of the processing pipeline. When you attach a handler to IObservable value, you get back an IDisposable that can be used to remove all handlers. Each handler attached to IObservable will get a new state (because the runtime creates a new processing chain according to the "specification").
In practice, the main difference is in the statfullness - if you want to share state, you can use the Event module - implementing the same using Observable is possible but harder.
If you're using events inside async, then you should use Observable and AwaitObservable (instead of built-in AwaitEvent), because using event combinators will leak memory - it will attach event handlers that are never removed.
I developed a class (in C#) for sending and receiving messages over network. It creates a new thread (listener thread) which waits till a new message arrives then raises an event.
The problem is the event is raised in the listener thread and when I want to use this class in a wpf application, a run-time error occurs trying to handle the event
The error is:The calling thread cannot access this object because a different thread owns it.
Is there any proper way to deal with this situation when the event raises in the mentioned class?
You've got to be on the UI thread to update UI objects. You can use the window's Dispatcher to execute code there:
this.Dispatcher.Invoke(new Action(() =>
{
// Code that updates UI here
}));
BackgroundWorker explicitly supports marshaling to the UI thread. You have to use it though, call its ReportProgress() method. While optimized for reporting progress, you don't have to use it for that. There's an overload that accepts an object, you can pass anything you want. The event handler gets it as the e.UserState value. From there, you could use that object directly or use it to re-raise another set of events.
Do beware thread-safety requirements for that object. The worker keeps running and is not in any way synchronized with the execution of the ProgressChanged event handler. So it should no longer update the object. Best to create a new instance of it after calling ReportProgress().
I have an object that is updated from a polling loop on a thread. This object fires particular events when data changes, etc.
I'm trying to use this object in conjunction with a windows form, where I create event handlers on the form to update the UI. Of course, this causes cross-thread operation exceptions if I try to manipulate the UI directly in these handlers.
I can get it to work by going through the standard procedure of checking InvokeRequired, using a delegate, blah blah blah. But I want to publish this object as a library, and I don't want end-users to have to worry about all that.
I want my object to somehow take care of synchronizing those event callbacks with the form so that end-users can manipulate the UI elements in those handlers worry-free.
Is there a way to do this??
If your object is always related to a single form, there is a simple trick indeed. The important fact here is, that you instanciate your object from the thread you like to affect the form later.
The trick is to instanciate a simple Control (new Control()) in your object in the constructor. When you perform logic on your form, use the Invoke/BeginInvoke methods on this simple control, to dispatch the action to the correct calling thread. So you have the dispatching logic directly in your object and there is no need for other users of your object to take care about this.