Threading using BackgroundWorker - multithreading

I have a scenario. I have a list of user id's displayed on the windows form. As soon as I click one of the user id's I go and get the user details from the DB. To retain the responsiveness of the application, on selection changed event of the listbox, I create new BackgroundWorker(BW) objects and hit the DB. I show 'Searching user 'abc'...' in the status bar.
Now, if the user moves between the user ids using arrow keys (4-5 times), by above design I have created multiple BW objects to make the request. But finally when the data comes back for particular user (which may not be the one where user is currently selected in the listview), since it was async call, I still end up displaying all the users in the status bar.
What I would like to do is, I want to go and get the details only for the last user. Till that time I want to display 'Searching user...' only.
Please let me know solution for this...

When the user switches users, you can cancel the worker processes that are currently running (check to make sure they are running). I believe that would accomplish what you are after.

How about waiting for a second or two before you start your Background Worker?
Once the user clicks on a user id, start a timer with 1 second interval and after that one second, start your BackgroundWorker. If user clicks on a different user id, reset the timer. This way, if user keeps clicking on different user ids in quick succession, you won't do anything. Once user has taken a break, you start your background worker.

One option would be to keep a string field in the form containing the user ID. In the background worker, after you hit the DB, check that this field is still equal to the user ID that it got passed in, EDIT: and, if it's different, make the result null. Manipulate the field using the methods on the Interlocked class to avoid the need to lock. That's wrong; reference types can be read and written atomically with Interlocked.
2nd EDIT: You could also return the original user ID from the background worker along with the result, and check that it's the most recent one clicked in your Completed handler.
Alternatively, if you're keeping references to all of the BackgroundWorkers, you could use their cancellation support.

I've used this to cancel BackgroundWorkers before and it worked great.
public void cancelWorker( BackgroundWorker worker )
{
if (worker != null)
{
if (worker.IsBusy)
{
worker.CancelAsync();
while (worker.IsBusy)
{
Application.DoEvents();
}
}
}
}
I've heard controversy over using Application.DoEvents(); but I had problems with endless loops if I used Thread.sleep or other.
You might want to use polling of some sort so you don't end up with backgroundWorkers fumbling over eachother - especially if the callback ends up calling something like the UI on a separate thread so they can actually cause race conditions.

Related

NetSuite: Calling an API in real time as field values get updated

I'm looking for a way (using SuiteScript 2.0) to handle real-time persistent (stored) field updates, where a field might have changed in NetSuite (for example a lead time was just updated), and it doesn't matter if a user saved the change, or some other automated process changed that field. I just want to be able to pick up on that change:
The moment that it's done, and
Without regard for who or what kicked it off (e.g. it could be a person, but it could also be an automated change from a workflow, or a formula on the field itself which pulls values from another field)
Doing some research I found some options that looked somewhat promising at first. One being the afterSubmit event in a client script, and the other being the fieldChanged event. My issue however is, from what I understood those only really seem to be triggered by a user manually going in and making those changes, but this is only one part of the puzzle and doesn't seem to cover changes made outside of the scope of the user making those changes. Is that correct however? Or would one of those events still be able to capture changes done to that field regardless of who (or what) initiated or triggered the change, and right at the moment the change was saved/ persisted to the database?
UserEvents are basically triggers. In their deployment records you can set the context in which they fire so you can get them to fire in all circumstances (called contexts in Netsuite) but one.
That circumstance is User Events are not fired for record saves made in User Event scripts. i.e., if an AfterSubmit UserEvent script loads, changes and saves your record a fresh user event will not be fired.

In PyQt, I need to handle both itemClicked and itemSelectionChanged, but not both at the same time

I've created a QTreeWidget that displays data that's pulled from 3rd party API calls.
I want the data to be fresh when the user either clicks on or uses the arrow keys to navigate to any QTreeWidgetItem. This means making an API call each time the user clicks or uses an arrow key. Importantly: If the user clicks on the same item a second time, I would like the item to display fresh data (in other words, make another API call and populate with new data, aka refresh)
So far, I've tried:
connecting both itemSelectionChanged and itemClicked to the same update function. This works, but it means that for every click on a new QTreeWidgetItem, I get two calls to the update function (one from itemClicked and the other from itemSelectionChanged), and therefore two API calls that do the exact same thing. Not ideal.
Only handling itemClicked, but then using an event filter to look for Key_Up or Key_Down and then manually emitting an itemClicked. The problem with this is that the key event is handled before the selection is changed, so when using the arrow keys, I'm always getting data for the last QTreeWidgetItem selected, not the current QTreeWidgetItem.
I thought about creating a very short timer or a flag and starting/setting at the start of the update function. If the timer is running or the flag is set, don't run the function (the idea being that the first slot would run, but the second would not because the flag is set/timer is running), but that seems both sloppy and prone to race conditions.
Unfortunately, using a QTreeView with a QAbstractItemModel is not an option.
With that, is there any way to handle both a repeated click on the same item and arrow keys to select new items without double-calling the same update function?
As #ekhumoro pointed out, this can be done by handling both itemClicked and itemSelectionChanged, but introducing some logic so that both functions don't run at the same time.
When a user clicks on the widget, an itemClicked signal is fired. When the user changes selection, the itemSelectionChanged signal is fired. In my case, then, I've created two slots (functions): update (a slot for itemSelectionChanged) and updateFromClick (a slot for itemClicked). updateFromClick checks to see if the selection has changed (this means you always have to store and keep track of the current selection). If, in updateFromClick, I see that the selection has changed, then I do nothing, as update will have been called from itemSelectionChanged. If, in updateFromClick, I see that the selection has not changed, I know that itemSelectionChanged has not been fired, so I call the update function explicitly from updateFromClick.
Again, this is just a long-winded version of #ekhumoro's comment that I'm putting here so the question doesn't go unanswered.

is plugin execution synchronized for delete message over multiple records?

I am new to Dynamics CRM and I could not find any information so far that would apply to my scenario.
We have a Program entity that can have/reference multiple program dates entities, and each program date can have/reference multiple event entities.
We have a view displaying all events associated with the program, and on the program form we want to display in a text the total number of events. From the view user can select multiple events then click delete button. This is a button that comes with CRM, not a custom one.
If I create a plugin for Delete message on event entity and want to query both program and Program date filed that keeps the total number of events then subtract one I am not sure these plugins will execute in sequence or concurrently thus having a race condition.
Now using javascript I also do not know if it is possible to read entity records PK and hook up into the pop up Confirm Deletion dialog box and execute after that a web service call
Any ideas?
Thanks
Dan
The deletes will process synchronously for the current user, but there is nothing that is preventing another user from performing deletes on Events that are also associated to the Program.
One option that you could look at is creating a flag on the Program and Program Date entities called "Events Deleted". Then create another plugin on the read that checks to see if it's "Events Deleted" flag has been flipped, and if so, recalculates the value, updates it and clears it's flag so it doesn't get calculated again.

Calling a method according to a timer set by the user

I am working on a JSF application that connects with Twitter.
In one of my views, I have a button that when clicked, calls a method that connects with a Twitter account retrieving new tweets and doing some processing on them.
Next to the button I want to let the user to set a timer in order that this action is done automatically. For example if the user selects '2minutes', Twitter must be checked every 2 minutes automatically and the results be refreshed.
Can someone help with some general explanations how can I do this?
Take a look at this Poll - Start/Stop
in your case
interval="#{myBean.someIntervalNum}"
and so on...

How should an IPhone notification-type application show user there is a notification unobtrusively

I have a program that basically just queries a webservice, and if any data is returned it will show that information to the user, but if there is nothing to display it will just wait until the next scheduled time and run.
Right now it just puts a table in front of the user, there is nothing in the status bar that they can then select and see the actual information.
So, I have two questions.
Is there a better way to show
notification information to a user
that is less intrusive?
Should the View, with a UITable,
have a button to dismiss the view
when they are finished?
UPDATE:
It seems my question isn't clear enough, so I have the data showing up in the table properly.
My problem is before I display the data.
Currently, periodically the timer will fire off my method, and if there is any events pending for the user the table will show up right in front of them.
That is bad design, IMO, so what I would like is some simple way to let them know that there is something pending and they can look at it when they get a moment.
Ideally I would have something in the status bar to show that they have some events, but it appears that isn't an option that I can see.
One option is to see if I can have the table come up minimized and have some sound or vibration go off, but again that can be bothersome to the user, as it may interrupt what they are doing.
The proper solution appears to be that while in the background just put up a notification if there is any new information, so the user can close the notification or switch to view the details.
This isn't the ideal solution, but appears to be the best choice on the iPhone.

Resources