Solved

VB.Net.  Close programatically opened form automatically

Posted on 2011-09-10
7
369 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

730 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