Show a progress bar while a function runs in a loop until it returns a value - vb.net

I have a function that runs an sql query for data that may or may not be there. Since I need to run this function continually until it returns the proper value how can I run a progress bar until the loop finishes.
status = Logic.ProcessResource(currentInstance)
While status.woID.Count <= 0
status = Logic.ProcessResource(currentInstance)
End While
How can I run this and show another form with a progress bar until the loop exits?

My comments as an answer...
Put that code into a different thread, then use a ProgressBar in "Marquee" mode to indicate an operation that is ongoing, but has no known ending time.
Yes...but you still need to put the query/loop in a different thread...otherwise the main UI thread will to be to busy to animate and remain responsive to the user.
Look at the BackgroundWorker control, or using a Task, with Async/Await.
You'd show the form, start the worker, wait for worker to finish, then close the form. The BackgroundWorker() has UI friendly events like RunWorkerCompleted that are already marshaled to the UI thread for you.

Related

Cannot update User Control Label Text from parent event until the event method reaches return statement

The main form of my application has an event that gets triggered upon completion of a certain task. The event completion launches a background thread and the called method in that thread runs a loop which takes a while to complete. This loop updates (or is supposed to update) the user on what's going on during its run by editing the label->Text property of the UserControl along with a percentage of completion in a different label in the same UserControl. There are some more labels in this UserControl that are updated, but are not of the concern for this question.
Here is how the methods and statements are called.
Event Completed -> Background Thread
Background Thread -> Process()
Process() -> for loop()
for loop() -> UserControl->SetMessage(System::String^ msg) and UserControl->SetPercentage(int Percentage)
(1) UserControl->SetMessage(System::String^ msg) -> stores msg in a private UserControl member and executes this->Invoke->SetMessage_sub()
(2) UserControl->SetPercentage(int prcnt) -> stores prcnt in a private UserControl member and executes this->Invoke->SetPercentage_sub()
(1) SetMessage_sub() -> label1->Text = _msg;
(2) SetPercentage() -> label1->Text = _prcnt;
However, I found that the labels were not getting updated for as long as the loop ran. The labels only updated once the loop finished its run inside Process().
I used the System::Console::WriteLine method to figure out where the problem occurs. I found out that all the methods were being called. But the execution would stop at where the program would reach label->Text property. Please help me out.
I have run into a similar problem recently and found that the GUI update calls must be made from either a thread running in the background or a background worker. Try calling the method that updates your GUI from a background thread or a background worker.
Edit 1 - it sounds like you've already tried a background thread. Try using a BackgroundWorker object to accomplish the task.
Edit 2 - upon re-reading your question, it occurred to me that the problem you are facing is the GUI not loading until the event method has returned. I encountered the same problem and asked my question here. To my surprise, all I got was downvotes and no answers. A comment tried to refer me to using BGWorker class to update GUI but none of them read my question carefully. I know background threads and background workers. I have tried both but the GUI does not update until return is called from the event method. In my case, after calling the BackgroundWorker to do the processing, I ran an empty while loop in the event method and called break when the BGWorker finished. But the GUI never updated until break was executed. So, my solution was to create a separate event for the processing and processing completion. The completion of the initial event calls the processing method and then processing method's completion calls the processionf completion event method.

How to add row to end of datagridview that is filled in by a background worker inside custom control

In the project I am working on right now, there is a tab in a custom control that is filled in after the current run finishes. So once the current run finishes, the main function calls one of the custom control's public functions that uses a background worker to fill in a datagridview in the custom control. From the main function, I want to add a row to the end of the datagridview once it is filled in. However, each time I have tried to add the row, it says the datagridview has 0 rows in it and adds it as the first item. The solution I am trying to find is how to recognize when the background worker in the custom control finishes, so I know when to add the row to the end of dgv. I thought this code would do the job, but it just loops infinitely:
While dgvInCustomControl.Rows.Count = 0
Threading.Thread.Sleep(5000)
End While
My suspicion is that the main function is unable to determine how many rows there actually are in the datagridview, and that it will always add the row as the first item. Any help is appreciated on how to approach this problem!
You could raise an event from the custom control, after the BackgroundWorker has finished. Let the main class listen to that event and do whatever it needs to do when it fires.
Just keep in mind that updating windows controls from background threads is usually not working and that you should use delegate functions to invoke the update on the main thread.

When backgroundworker completes, how to give priority to update the UI thread?

I have a background worker I'm using to run through a large function in hopes of quickening my window load and responsiveness. When I put this function in the backgrounderworker's doWork event I'm just wondering if there's anyway I can prioritize that text to update as soon as the BGW is complete rather than waiting for the UI thread to finish and then update the textbox. Is this possible? I only ask because it seems like it's taking quite some to update the textbox after running through the function(which takes about 1.5-2s) but even if I start the BGW to begin even before the window loads(takes a good 5 seconds to load), it's still the last item on my window to update so I would like to(if possible), halt the UI thread from updating the UI until I update with what this function returns, then continue updating the rest of the labels.
Also, is there anyway to update two separate items at once or is it restricted to the one UI thread?
If you want to update UI elements, you have to do it on the UI thread. When the BackgroundWorker is finished, the RunWorkerCompleted event is raised on the UI thread. If the UI thread is busy doing something, then the completed event has to wait. In general, there's no safe way to interrupt the UI thread, make it process the RunWorkerCompleted event, and then go back to what it was doing.
UI elements must be updated from the UI thread. So you can't update two separate items at once.
I don't know how your initialization is structured, but if you have one group of items that you can initialize before the BGW is finished, and another group that can't update before the BGW is done, then do the first group and stop. Then have the RunWorkerCompleted handler do its update and all the rest of the updates. So it would look something like:
FormLoad()
start background worker
do first group of updates
RunWorkerCompletedHandler()
update from BGW calculation
do rest of updates

How to update a NSTextField Value during an action?

I am running a lengthly task in an Action and I would like to have a display of where I am at. For that I created a Text Field and I tried it with setStringValue:
[textField setStingValue: [NSSting stringWithFormat:#"%ld",currentValue]]
The code works but unfortunately it is not updating the NSTextField after every iteration but rather when the whole Action is done.
What am I doing wrong?
This is because applications with the Cocoa framework use an event loop to perform operations, and events occur in a completely serial fashion.
An event is basically any kind of action that the framework designer could not predict or found convenient to have run in a delayed manner. Since you can't predict when clicks will be performed, they need to be considered events; and for efficiency reasons (since you don't want to repaint a component multiple times if you don't need to), the repaint actions are events too.
Your action runs in response to a user event (for instance, a click on a button is an event) and therefore blocks all other events waiting in the queue until it's complete. However, components are repainted in response to a different, framework-triggered event, and as such the text field must wait until your action completes to repaint itself. This is why you cannot visually change the value of a text field from inside an action.
In order to notify your user of the progress of your task, you'll need to run it on a different thread. There's a lot to say about threads, so you should probably read some about them. I'm also sure that there are plenty of examples of how to run a long action in a background thread and update the UI accordingly for Cocoa all over the Internet.
When you click on a UI component, and it enters the Action block, the code is running on the main thread, the same thread that is painting the UI. If you run a long running operation in that block, it isn't going to paint until you are done because it is busy doing whatever you have it doing - you have hijacked the paint thread.
As said elsewhere, you need to spawn another thread, and then have the new thread perform the long running operation, and occasionally send messages to have the UI be updated by the main thread.
As a next step, go read the Apple documentation on NSThread, specifically:
+ (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument
Be aware that threading is a non-trivial domain area, and be ready for some wierd behavior if you aren't careful.

VB 2010 - Stop a loop with a button

I am using VB 2010 , I have a loop that takes something like 5 minutes until it's finish.
I want to allow the user the option to stop it in the middle or whenever they want.
There's a button which starts the loop , and after I click it the loop starts running and the button is sort of "stuck".
I saw something called "background work" in the VB Toolbox, can it be a solution to my problem?
Start your loop in separate thread and set a flag which directs the action of the loop . Keep polling for this flag to see whether user wants to stop the thread inside the loop on thread . See BackgroundWorker
I think background worker would work, but a very simple solution if to create a boolean variable (visible in scope to your stop button and the logic loop) that controls stopping inside your loop. Your stop button would set the variable to true and the next time that code is hit, it would stop. you may need an application.doevents inside your loop at the end to allow the button's event to fire. This is not an ideal way, but is certainly simple.