Solved

VB.Net.  Close programatically opened form automatically

Posted on 2011-09-10
7
379 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 86

Expert Comment

by:Mike Tomlinson
ID: 36516727
You can also place "Application.DoEvents()" inside your loop.
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 86

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
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.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

688 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