Vb.Net waiting for process 30secs - vb.net

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

Related

vb.net - Timer stops working when stopped and started

I have an app that runs several timers.
The first is a heartbeat timer, HB_Timer, which fires off a backgroundworker at the set interval which runs a sql command to register a 'heartbeart'.
The second is a reinit timer, Reinit_Timer, which again fires off a backgroundworker to query global settings from the database and, if the value for heartbeat interval has changed, stops HB_Timer, changes HB_Timer.Interval accordingly and then restarts HB_Timer.
I've amended the code below slightly to put it in its most simplistic format.
The heartbeat interval in the DB is saved in seconds, so i multiply by 1000 to get ms before applying the interval...
Public Function Endpoint_ReInit()
Try
HB_Timer.Stop()
hb_interval = ReadSQLValue(<the data i need to read...>)
If hb_interval > 0 Then HB_Timer.Interval = (hb_interval * 1000)
HB_Timer.Start()
For some reason, once the timer is stopped, it wont start again programatically, and I can't see why.
I've added console.writeline on the tick event of the timer and on firing the backgroundworker... both work before it is stopped and then restarted and then neither work (obviously, if the first doesnt then the second definitely wont).
I wondered if it was going to quickly so have tried sleeping for periods between stopping and starting, but that didn't work.
I even tried adding a temp button on my form to see if the timer could be manually restarted, and that doesn't work either.
I have error handling on all my functions etc and no exceptions are thrown.
I'm at a bit of a loss.... can anyone help point me in the right direction?
At a guess the value returned from the SQL query is large, so it's not that the timer is stopped, it's just that it is taking a very long time before it ticks.
Or something crashes in your backgroundworker and thus your timer.Start() is never called (the call to ReadSQLValue() never returns). If ReadSQLValue() uses a BackgroundWorker, though I'm not really sure how you're managing the sync/async mixing, be aware that when a BackgroundWorker experiences an exception in DoWork() it doesn't may not end up in your code at the time it occurs. The BGW just fires the RunWorkerCompleted event with an EventArgs that has an exception in the Error property)
When I work with timers I tend to set them to some low interval, say 1 second, and if I want something to happen every minute for one process and two minutes for another I have a couple of ints that count down from 60/120 respectively and when they hit 0, I do the processes, and reset the ints back to 60/120. I don't stop/start/change the timer interval

Event handler freezes Front panel

I am using simple event handler in the while loop.
I have value change event for the boolean button. There is some code that takes 3-4 seconds to execute.
The problem is I am not able to click anything on my Front panel during this period. Is it possible to allow the user to click on other controls when event handler is working on some case (as I understand the event handler is able to collect all events and process them ASAP)?
I fully agree with Mikhail N Zakharov's answer, but anyway your problem can be easily solved by just unchecking the checkbox named Lock panel until the case for this event complates
Please see screenshot below.
PS. Once again it is not the best practise to make event structure to work for 3-4 seconds.
I think you need to restructure your application to make it more responsive. LabVIEW best development practices suggest keeping event handler code as fast as possible. One of the ways to handle this would be to send a message into the queue on the change of this Boolean control and process the queue in a separate loop.

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.

Visual Basic wait without stopping program?

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

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