Solved

Close form within Timer Addhandler

Posted on 2007-04-11
3
893 Views
Last Modified: 2008-02-01
VS 2005, Trying to close a form within addhandler of timer function, but does not work. Code:

Public Class frmMain

    Dim aTimer As New System.Timers.Timer()

    Public Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click

        Dim str As String
        Dim str2 As String

        AddHandler aTimer.Elapsed, AddressOf OnTimedEvent

        aTimer.Interval = 5000
        aTimer.Enabled = True

        str = "C:\Program Files\Microsoft SQL Server\80\Tools\BINN\SQL Server Enterprise Manager.MSC"
        str2 = "C:\WINDOWS\system32\mmc.exe /s  """ + str + """"

        frmApp1.Show()
        Process.Start("outlook.exe")

     
        Me.Hide()
        'Terminate()
        'closewindows()

    End Sub

    Private Sub Terminate()
        Dim pk As Process()
        pk = Process.GetProcessesByName("OnePoint")
        For Each p As Process In pk

            p.Kill()

        Next

    End Sub

   'Private Sub OnTimedEvent(ByVal source As Object, ByVal e As Timers.ElapsedEventArgs)

    frmapp1.hide()

    End Sub

End Class
0
Comment
Question by:mwalsh2000
  • 2
3 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 18892427
Your OnTimedEvent is hiding the frmapp1 and not closing it.
0
 

Author Comment

by:mwalsh2000
ID: 18892475
It is actually not hiding or closing the form, it stays on the screen
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 18893906
Hi mwalsh2000;

The reason it was not working was do to the fact that you were using the default reference to the forms collections new in VS 2005 which does not seem to work like if you have created an instance of the form. Once that was fixed the next problem was that you was using a System.Timer which when you attempt to modify a control such as a form you throw a cross threading error so I changed it to the System.Windows.Form.Timer and now it should work OK. Now that said if you hide the main form without a way to make it visible again you will not be able to terminate the program.

Public Class frmMain

    Dim aTimer As New System.Windows.Forms.Timer
    Dim fapp1 As frmapp1

    Public Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click

        Dim str As String
        Dim str2 As String

        AddHandler aTimer.Tick, AddressOf OnTimedEvent

        aTimer.Interval = 5000
        aTimer.Enabled = True

        str = "C:\Program Files\Microsoft SQL Server\80\Tools\BINN\SQL Server Enterprise Manager.MSC"
        str2 = "C:\WINDOWS\system32\mmc.exe /s  """ + str + """"

        fapp1 = New frmapp1
        fapp1.Show()
        Process.Start("outlook.exe")


        Me.Hide()
        'Terminate()
        'closewindows()

    End Sub

    Private Sub Terminate()
        Dim pk As Process()
        pk = Process.GetProcessesByName("OnePoint")
        For Each p As Process In pk

            p.Kill()

        Next

    End Sub

    Private Sub OnTimedEvent(ByVal source As Object, ByVal e As System.EventArgs)

        fapp1.Hide()

    End Sub

End Class

Fernando
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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

911 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

21 Experts available now in Live!

Get 1:1 Help Now