Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, June 12, 2006 5:34 PM | 1 vote
Hi,
I have created a simple VB.NET application with one form and a few modules. When a user clicks on a run button of the form the application starts and goes through differet cycle, while updating the user on status bar, runs for less than four hours.
The application works fine, it gives desired result. However, it is not able to refresh the user form, meaning status bar remains same after some time (it suppose to increment). Even when I take mouse on the form it shows timer symbole instead of default one.
My application commands another device using GPIB. Basically, PC running my application commands the device. If I look the display of device it gives correct information.
Please help me with this issue,
Thank you,
Shalin.
All replies (11)
Monday, June 12, 2006 6:13 PM âś…Answered | 1 vote
It sounds as if you are looping and are compute bound.
On an outer loop... every once in a while do a
Application.doevents()
Monday, June 12, 2006 7:25 PM | 1 vote
As renee has stated - this can often occur in looping situations where the application is trying to update a control in every iteration of the loop and the loop is running quickly it doesnt appear to process the windows requests and the controls dont seem to reflect the current state.
What application.doevents does is waits until all the windows requests have been processed before continuing and hence you controls do get updated.
However something to bear in mind, just stick this in a loop can severely affect you system performance if the application is doing something which is looping a lot of times. You may want to consider doing the application.doevents periodically - say every 1000 loops or even every x number of seconds to update the UI.
This you can do with either a counter on the loop or a timer. What this does is periodically issue an application.doevents to update the UI but does so only periodically and therefore you dont take the big performance hit on the application.
Monday, June 12, 2006 8:34 PM
Thank you spotty and reneec for prompt reply.
Your analysis is correct. Program is running through a loop and each iteration has specific dwell time. For some iterations dwell time could be up to 3 secs and rest of them have dwell time of tenth of a second.
Just want to clarify.... application.doevents() can be within the loop, however, it should occur periodically.
My concern is time spent in doing application.doevens(). Would it change my dwell time significantly?
I will give this a try and will update you.
Thank you,
Shalin.
Monday, June 12, 2006 9:44 PM
You are correct in your understanding.
I'm not sure what you are referring to as dwell time if you have a large number of iterations occuring, very fast and are trying to update the UI on every single iteration and putting the applciation.doevent in every iteration can slow you application down quite a bit.
Hence updating every x number of iterations or every x number of seconds rather than every iteration unless it is critical you update UI for every interation in which case you'll have to take the performance hit.
Monday, June 12, 2006 10:45 PM
You may also want to check out the BackgroundWorker class... Personally, I prefer this over the Application.DoEvents. Jessica has an interesting article on the porential issues with Application.DoEvents here:
http://blogs.msdn.com/jfoscoding/archive/2005/08/06/448560.aspx
Best regards,
Johan Stenberg
Tuesday, June 13, 2006 11:44 PM
DoEvents will work, but only to a certain extent.
Public Sub DoSomething()
Do
DoSomethingElse() 'This takes 2 seconds
DoItAgain() 'This takes 1 second
Application.DoEvents() 'This will only allow you controll of your form every 3 seconds.
'and it will difficult to catch.
Loop Until IsDone
End Sub
Have you used Delegates? You could use delegates to run the method asynchronously. I do it all of the time. It is a method of multithreading. You can have delegate subs or functions. You just have to remember anything done in the DoSomethingSub when it is run via the delegate it is in its own thread. ie. If you throw an exception in the DoSomething method it will go no were. here is a link to some more examples: http://www.codeproject.com/vb/net/Delegate.asp
Dim Delegate Sub DoSomethingDelegate
Public Sub DoSomethingAsynchronously()
Dim dlg As New DoSomethingDelegate(AddressOf DoSomething)
dlg.BeginInvoke(AddressOf DoSomethingFinished, dlg) ' You pass it the method to run ' when it is finished and a ' reference to the delegate.
'Starts the DoSomething method and then keeps going
'giving you full control of the form.
End Sub
Public Sub DoSomethingFinished(ByVal ar As IAsyncResult)
Dim dlg As m_ProcessAccountsDelegate = DirectCast(ar.AsyncState _
, m_ProcessAccountsDelegate) 'Get reference to thread.
dlg.EndInvoke(ar) 'Stops the thread.
'Clean up if necessary.
End Sub
Wednesday, June 14, 2006 1:28 PM
Hi all,
Thank you all for your suggestions. I was out of office yesterday and could not go through everything. I will try today.
BTW: Sorry to use term I use specific to my application. Dwell time you can define as the time program needs to stay in perticular iteration. It is not equal for all iterations.
Thank you again,
Shalin.
Wednesday, June 14, 2006 6:50 PM
Hi all,
I think it is a bad day today. I could not really do anything. My code was not able to compile. I create an excel file in the same program, I used Microsoft.Office.Interop.Excel namespace. It was working fine before. I don't know what happened and now it is not able to find this namespace. I checked the reference to the Excel 11.0 object and it is there.
I do not understand what's going on. Can anybody help me here?
Thank you,
Shalin.
Wednesday, July 5, 2006 7:48 PM
Can you post a sample of your code?
Wednesday, March 31, 2010 11:59 AM
I have a similar problem where no looping is involved! I am simply waiting for a stored procedure to run a process on SQL Server (normally taking approximately 10 mins). My front-end VB application is fine as long as I don not anything else on my machine. If I dare to loose focus by moving away to another application (e.g. to open an email) on moving back I have no chance of getting the VB application to respond until the job has actually finished. Thought Windows was supposed to be a multitasking operating system?
Saturday, March 26, 2011 8:38 PM
I used to use a do loop with a doevents every x number if increments. For example:
dim x as integer
do while true
x += 1
if x mod 1000 = 0 then Application.DoEvents
loop
This method eats up too many cpu cycles just iterating the counter.
I found a better way is to use a Timer. Add a Timer control and set these properties:
Name = loopTimer
Increment = 1000 * number of seconds to wait
Enabled = False
Double click the timer and add Application.DoEvents to the Tick Event Sub
When you are ready to loop and call doevents set loopTimer.Enable = True
If you want it to stop set loopTimer.Enabled = False
i use this timer for a runing clock on the status bar. Interval is set to 1000 (1 second)
Private
Sub timerStart_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles
timerStart.Tick
tslabDateTime.Text = Now.ToString
End Sub