Solved

Close form within Timer Addhandler

Posted on 2007-04-11
3
892 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

747 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

11 Experts available now in Live!

Get 1:1 Help Now