Solved

Close form within Timer Addhandler

Posted on 2007-04-11
3
896 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

730 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