Solved

VB.Net.  Close programatically opened form automatically

Posted on 2011-09-10
7
362 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 2

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

16 Experts available now in Live!

Get 1:1 Help Now