Solved

Close form within Timer Addhandler

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

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 63

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

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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.
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 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…

829 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