Efficient implementation of threads in the given scenario - winforms

I've got a winforms application that is set up in the following manner: 2 buttons, a textbox, an class object MX with a collection K as its member, function X and another function, Y.
Function X parses a large database and enumerates some of its data in collection K.
Button 1 calls function X.
Function Y walks through the above collection and prints out the data in the textbox.
Button 2 calls function Y.
I'd like to call function X through a worker thread in such a way that:
The form remains responsive to user input. This comes intrinsically from the use of a separate thread.
There is never more than a single instance of function X running at any point in time.
MX/K can be accessed by both functions at all times.
What would be the most efficient implementation of the above environment ?

When you press button 1, you can call X using BackgroundWorker to run it in a separate thread. Then set a variable (or grey out Button 1) such that the user cannot run X again.
X can write to its own collection while it processes the DB. Then it can replace an instance variable with this collection. By only doing a single replacement you can avoid synchronization problems between X and the UI thread.
After X completes, you can use a BackgroundWorker event to let the UI know the operation is complete. Then you reset the same variable (or ungrey the button) to let the user know they can now run X again - if necessary.
What do you think? Does that help at all?

Related

Is Angular's foreach loop over an object asynchronous?

I read somewhere in the past that angular.foreach is asynchronous unlike looping over arrays which is synchronous. For a long time I was taking into account this and doing the necessary to avoid executing the code which comes after the loop before it's finishes all its iterations (by wrapping the angular.foreach inside an anonymous JavaScript function which calls a callback which will be executed once the loop finishes all iterations).
(function(callback){
angular.foreach(..)
callback();
})(callback)
But I had a conversation with a collegue who didn't agree that angular.foreach is asynchronous and I also couldn't find that information again which makes me confused now.
no. Take a look at the docs
Furthermore your code wouldn't work if foreach would be asynchronous.
If foreach would be async, the callback would be called immediately after calling foreach and foreach would be put onto the eventqueue which would execute it some time in the future.
Javascripts concurrency model does not have threads but instead uses an eventloop. This means every async operation is pushed onto the eventqueue and executed later.
Have a look into the MDN
There may be a scenario where you want to make code behave asynchronously.
I had a scenario where I used local storage to store an ad-hoc user selected collection of jobs that I wanted to perform the same operation on.
I had a web service call to convert a list of job names into a returned a collection of job objects. I initially tried using a
foreach loop inside the subscribe pf the service layer, that operated on the results.
Then I tried calling another method within the foreach loop that as it performed the operations removed the job name from local storage when the operation posted to the web service correctly.
The problem was on the second iteration I read the collection of names from local storage again - before the set to remove had completed.
There was a lot of manipulation of the job and object properties to create the parameters passed on the function call, so I ended up refactoring the code, creating a value object interface and stored the information in a value object array for the whole job collection I had returned. I included the index of the job too in the value object.
I introduced a BehaviourSubject property to the class.
During the restructuring, I just added an entry to the value object array collection within the forEach loop instead. At the end of the loop. I sent next(0) to the BehaviourSubject to start the ball rolling.
Each time a job name was removed from local storage, I converted service to return a Promise.
Then in the code after the service was called I put this code in the then part, behaviour subject.next(index from value object +1)
In the initialisation I set the behaviour subject up with a -1 value..
Then in the subscription to the BehaviourSubject class I ignored -1,
And when the index +1 was > length of value object collection called completion routine - which bounce app back to prior page.
When the index was between 0 and 1 less than collection size, I just called the method that had originally been in the forEach loop with the value object entry with the value object match the index of the behaviour subject.
By doing this I had converted the behaviour of the forEach into something asynchronous.

Flink trigger on a custom window

I'm trying to evaluate Apache Flink for the use case we're currently running in production using custom code.
So let's say there's a stream of events each containing a specific attribute X which is a continuously increasing integer. That is a bunch of contiguous events have this attributes set to N, then the next batch has it set to N+1 etc.
I want to break the stream into windows of events with the same value of X and then do some computations on each separately.
So I define a GlobalWindow and a custom Trigger where in onElement method I check the attribute of any given element against the saved value of the current X (from state variable) and if they differ I conclude that we've accumulated all the events with X=CURRENT and it's time to do computation and increase the X value in the state.
The problem with this approach is that the element from the next logical batch (with X=CURRENT+1) has been already consumed but it's not a part of the previous batch.
Is there a way to put it back somehow into the stream so that it is properly accounted for the next batch?
Or maybe my approach is entirely wrong and there's an easier way to achieve what I need?
Thank you.
I think you are on a right track.
Trigger specifies when a window can be processed and results for a window can be emitted.
The WindowAssigner is the part which says to which window element will be assigned. So I would say you also need to provide a custom implementation of WindowAssigner that will assign same window to all elements with equal value of X.
A more idiomatic way to do this with Flink would be to use stream.keyBy(X).window(...). The keyBy(X) takes care of grouping elements by their particular value for X. You then apply any sort of window you like. In your case a SessionWindow may be a good choice. It will fire for each key after that key hasn't been seen for some configurable period of time.
This approach will be much more robust with regard to unordered data which you must always assume in a stream processing system.

saving temporary data on matlab gui

I have made a GUI with Pushbutton and an Edit text.
The first step is to enter initial data at the Edit text and then click on the Pushbutton.
Once the Pushbutton is clicked, it gathers some info from API and then it does some long calculations and shows the results in a table.
However, if I change my initial Edit text, i still want to use the API that was the same in the first click.
Is there a way to save the data and use it for the next Pushbutton clicks to save calculation time and not gather info from API everytime?
*I had like to save that data only as long as the code opened, just for the case i will need more calculations. I dont want to save the data with save function and then have MAT files on that folder.
Thank you.
You can attach data to a specific GUI objects in different ways
1. Using UserData
The property UserData of an GUI object can store a variable of your choice (to store several variables just collect them in a struct, cell or array.
Use the object handle to get or set the data. For example, to set/get the UserData of a edit text box with tag edit1
set(handles.edit1, 'UserData', 2)
get(handles.edit1, 'UserData')
ans =
2
Note that within a callback you can write hObject to get the current handle.
2. Using application data
A very simular method is to store data to a GUI object using setappdata and thereby creating your own key-value map associated with that object/handle. The difference against the UserData method is that you can create several different key-value pairs (so the need to collect everything in a struct/cell/array isn't as imminent).
setappdata(handles.edit1, 'Foo', 1);
setappdata(handles.edit1, 'Bar', 3);
getappdata(handles.edit1)
ans =
Foo: 1
Bar: 3
getappdata(handles.edit1, 'Foo')
ans =
1
3. Using guidata
Another variant is to assign a single varible (like for UserData) to the main GUI figure rather than to a specific handle. This is done by using guidata. No key/name is used to set the data.
guidata(anyHandleInGUI, myData)
The first input is a the main figure handle or any of its children.
guidata(handles.edit1, 5)
guidata(handles.edit1)
ans =
5
Suppose your pushbutton has the tag pushbutton1. Since guidata locates the root parent (the figure handle) you can get the same data using its handle.
guidata(handles.pushbutton1)
ans =
5

Change in evaluated expression when one term changes (multi-threaded)

In the C language, is there a way to dynamically see a change in the value of a variable based on change in another variable that it is related to (multi-threaded)?
For example, if I have a and b to be 2 globals, and I want a = 2*b always.
a = 2*b;
If b changes, in another thread, is there anyway to see that change in a if I switch into a thread that uses a?
To have a changed when b changes only works in dataflow driven languages like VHDL, but this is for designing hardware. In C variables are only read when they are needed and not when they are modified.
When you are using threads, you can notify other threads that a variable changed, with condition variables.

VisualBasic6 - read continuously a variable

I need read in an infinite loop some variables and in the case it changes the boolean status it must do something.
I tried to use a Do...Loop but the application crashes.
Is there a way in visual basic 6 to use an infinite loop without stunk?
My code:
Do
asd1 = readValue1
asd2 = readValue2
If asd1 <> asd1ex Then
Text1.Text = "yes"
End If
If asd2 <> asd2ex Then
Text1.Text = "no"
End If
Loop While True
Make a timer and on that timer check the status, instead of the loop.
Solved after comment that explained where the data was coming from (async COM component's property):
working with vb6 IDE on a realtime client-server project. I have to read some variables
and when one of these changes status it sends a socket message to
server. With the sleep it stuck equally
What did not help:
DoEvents and sleep
DoEvents
Sleep 100
might help, will need to refer to the windows function sleep. But VB6 is single thread (well one for UI and one for logic) so you should have a way to get out of the loop. What are you really trying to do? Can you describe at a top level?
Are you working on the VB6 IDE or in VBA code in Office?
For sleep to work declare:-
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
See this too https://stackoverflow.com/a/4540553/1643558
If your showing a form to the user to read value 1 and 2 then you can use a modal form and have a button to click when they are done, and hide the form only when you like the values. No need to have a loop then. Can show an error MsgBox on a modal form too.
See http://www.tek-tips.com/viewthread.cfm?qid=1117372
Maybe remove the sleep and only keep the DoEvents.
You could also make a timer and on that timer check the status, instead of the loop
It looks like you're trying to set up a sort of event handler. In effect, your loop is "listening" for a change to the variable. You don't explain how the variables get changed, and this is important . If whatever is changing the variables can also raise an event, then you're home free--you can get rid of your loop and use the event handler to send the socket message. (This is probably why Deanna asked how the variables change.) This is the preferred way to do what you want, so you should find ways to raise an event if the variables change.

Resources