Visual Basic wait without stopping program? - vb.net

I want to make my program wait 1 second or so before my player can move again but Sleep freezes everything in the program...
How would I make one event wait not the whole program?

use timer to perform the task.
just disable the particular UI component and start the timer once the timer runs for 1 second or more re-enable the specific UI component.
Thread.Sleep(1000);
is only helpful in background threads.

You can use this
System.Threading.Thread.Sleep(100)
The values are in milliseconds

Related

Show a progress bar while a function runs in a loop until it returns a value

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.

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 can I STOP the JSFL process in flash CS5?

I write a JSFL script, have a very long loop, when this script is running, I can't stop it. only using the Alt-F4 to kill the Flash CS5, then restart it. this is too crazy. How can I stop the JSFL script without kill the Flash?
Here's a better approach: inside your loops, put a condition to check if a key was pressed. If the key was pressed, break the loop.
if(fl.tools.shiftIsDown)
{
stopScript = true;
return;
}
I used the "stopScript" var because I needed to break more then one loop to stop the script. Good luck!
There are a few advices:
Optimize your script.
Limit input
Divide script logic into functional chunks that you can call them in a row every EnterFrame event. You can embed that script info WindowsSWF and use it's as EnterFrame event beacon (every frame WindowSWF panel calls a function in JSFL). You can add a button in a panel that stops calling JSFL function every frame.

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.

Vb.Net waiting for process 30secs

I have three events to fire on button click.
after running the first event i want to wait for 30sec to wait for nex event to fire.
how i can wait( i mean looping for 30 secs).
Thanks,
Nag
You can try this to your code directly:
MessageBox.Show("Test") ' Execute your method 1
System.Threading.Thread.Sleep(30000)
MessageBox.Show("Test2") ' Proceed with the other one :)
If you wait on the UI thread you'll block the whole UI and Windows will show your application as non-responding.
Better to:
Update the UI to show it is busy, including disabling controls to block user input.
Use a timer control (details depend on WinForms or WPF) to trigger an event after the time delay
Do the work in the timer's event handler.
If the work is CPU or IO intensive (ie. likely to block for more than a few tens of milliseconds) then perform the work in the threadpool (eg. BackgroundWorker component). Remember you'll need to use Control.Invoke to make any changes to the UI from the worker thread.
You can use Thread System.Threading.Thread.Sleep(30000); to hold execution.
use a timer for it and set an interval of 30 seconds to timer (1sec = 1000)
timer1.Interval=30000