Solved

VB.Net.  Close programatically opened form automatically

Posted on 2011-09-10
7
360 Views
Last Modified: 2012-05-12
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
Comment
Question by:PNRT
7 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 36516654
usea backgroundworker to fade it. Show me your codein the for loop and I can wire up a backgroundworker for you.
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 36516690
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36516727
You can also place "Application.DoEvents()" inside your loop.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 40
ID: 36516748
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36516752
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
 
LVL 1

Author Comment

by:PNRT
ID: 36516797
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
 
LVL 40
ID: 36516807
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now