[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 382
  • Last Modified:

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.
0
emdsft
Asked:
emdsft
1 Solution
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
emdsftAuthor Commented:
Thank you very much. That worked like a charm.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now