[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 405
  • Last Modified:

VB.Net. Close programatically opened form automatically

Hi Experts.  I have a form that is opened automatically when a button is clicked.  I have set a "for next" loop in the form so that it fades after a certain time if it isnt closed by the user.  My problem is that the entire program freezes while the "for next" loop runs.   I suspect that each instance of the form should be on a seperate thread to avoid this.  Is that correct?  Any solutions would be appreciated.   Thanks in advance.      
0
PNRT
Asked:
PNRT
1 Solution
 
nepaluzCommented:
usea backgroundworker to fade it. Show me your codein the for loop and I can wire up a backgroundworker for you.
0
 
Jacques Bourgeois (James Burger)Commented:
Do not use a For Next loop. Use a timer:

Add a Timer control to the form.

Set its Interval property to the time you want (it's in milliseconds: 10000 = 10 seconds)

fade the form in the Timer's Tick event.
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    'Fade your form
    Me.Close 'If you want
End Sub

Open in new window


Forms automatically runs on their own thread, so you do not have to care about defining threads between forms.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can also place "Application.DoEvents()" inside your loop.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Jacques Bourgeois (James Burger)Commented:
Application.DoEvents would work, but a loop is a very bad way of putting a delay anywhere.

The problem is that the loop won't run for the same length of time on different computers. It might be too short on fast machines, and too long on slower ones.

The timer is the best way to set a delay, because it is independent of the microprocessor speed.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Just depends on how you wrote your loop!  It could  be time based as well...instead of on a fixed Sleep() interval.

But yes, generally speaking, there is often a better choice than a tight loop with DoEvents() in it.  ;)
0
 
PNRTAuthor Commented:
Thanks everyone for the help.
I understand the timer to do the fading of the form.  But I need the form to appear and stay for say 20 seconds then start the fade which takes about two seconds.  Would I need two timers then?
0
 
Jacques Bourgeois (James Burger)Commented:
Not necessarily.

The interval of the timer could be reset for 2 seconds after the 20 seconds delay, but both jobs would have to be handled in the same event procedure, which would involve the use of a static variable or another method to know if you are going in the event for the first or second time.

Using 2 timers is thus not only easier, but I think would also be a better design. An object should usually be tasked with only one job.

Simply enable the second timer when the first one fires.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now