Visual Studio 2005 - Form hangs on a form.show call within a timer's event handling routine

I am using Visual Studio 2005.

I have a form with a simple timer on it. When the timer goes off it does a form2.show to bring up another from non-modally. The Form2 comes up but it hangs after that.

A form2.showdialog works fine. Its just when its brought up in a non-modal mode that this happens.

Also on any UI event handler (Button, label, list, listview etc..) it works fine if I try to show form2 in a modal or a non-modal mode.

Please help.
emdsftAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Make sure that you are actually using a System.Windows.Forms.Timer from your ToolBox.

If you use any of the other timers, they run in their own threads and you need to use a Delegate to show your second form from their events.
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
If you are using the proper Timer then show us your code please and explain what (if any) code runs in the Form2 load event.
0
broadbentCommented:
Have you disabled the timer when form2 shows?
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

manojkumarpsCommented:
Hello emdsft,
         The timer once finished should be disabled or disposed before the form2 is shpwed. As this is not a modal form the control would still be in the form1. If the timer after stopping is not disabled the form2 will obiviously not work. So as soon as the timer finishes the job set timer.disable = true and timer = nothing for more security. And then form2.show.

This should solve your problem.

Regards,
manoj
0
emdsftAuthor Commented:
I have to use System.Timers.Timer as I am actually simulating a problem that I have in another program. I have a DLL that fires datachange events. In the client that is using the DLL I need to show a form non-modally when I get this event. This is best simulated by the System.Timers.Timer.

It works if I use the System.Windows.Forms.Timer

Is there any way we can make it work with a System.Timers.Timer. I tried disposing it off and set it to nothing before showing Form2 but that did not help. Also I don't think I can use the same approach for the datachange event from my DLL.

Public Class Form1
    Private WithEvents Core_Timer As System.Timers.Timer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Core_Timer = New System.Timers.Timer(1000)
        Core_Timer.Start()
    End Sub

    Private Sub Core_Timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Core_Timer.Elapsed
        Core_Timer.Stop()
        Core_Timer.Dispose()
        Core_Timer = Nothing
        Form2.Show()
    End Sub
End Class
0
emdsftAuthor Commented:
In my simulation program Form2 just has a button on it which does nothing. In my actual program the form has a listview and a button.
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Use a Delegate and the Invoke function to marshal the call from the Timer thread to the Main UI Thread:

    Private Delegate Sub ShowForm2Delegate()

    Private Sub Core_Timer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Core_Timer.Elapsed
        Core_Timer.Stop()
        Core_Timer.Dispose()
        Core_Timer = Nothing
       
        ShowForm2()
    End Sub

    Private Sub ShowForm2()
        If Me.InvokeRequired() Then
            Dim sf2d As New ShowForm2Delegate(AddressOf Me.ShowForm2)
            Me.Invoke(sf2d, New Object() {})
        Else
            Form2.Show()
        End If
    End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
emdsftAuthor Commented:
Thank you very much. That worked like a charm.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.