?
Solved

Close form within Timer Addhandler

Posted on 2007-04-11
3
Medium Priority
?
900 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 2000 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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses
Course of the Month7 days, 21 hours left to enroll

765 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