Solved

Pause APP

Posted on 2006-06-08
6
276 Views
Last Modified: 2010-04-23
I am very new to development so please bear with me here.  

I have a form that loads upon running the app.  That form has text and images on it and upon loading calls a vbscript and runs it without any issues.  Now what I need to do is tell the form to stay open for 20 seconds then show a closeso button  the user can close the application.  20 seconds is plenty of time for the vbscript to finish running.

Any help would be greatly appreciated.

Todd
0
Comment
Question by:Todd Wedlake
6 Comments
 
LVL 8

Expert Comment

by:doraiswamy
Comment Utility
1) You would need to disable the close icon on the right hand top corner of the form.
2) Have a CLOSE button. Initially disable it.
3) Have a timer. Set the interval property to 20000, which would give 20 second delay.
4) When the timer event occurs, enable the CLOSE button.
5) In the clicked event of the CLOSE button, end the app

Hope this helps. If you have any specific question, do ask.
0
 
LVL 11

Expert Comment

by:anyoneis
Comment Utility
Here's a bit of advice: Don't use a timer to perform this operation, rather monitor the process and then enable the close button when it has completed.

[code]

Public Class Form1

    Dim m_process As System.Diagnostics.Process

    Private Sub OnFormLoad(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim command As String = "C:\Windows\System32\prnjobs.vbs -l"
        m_process = New System.Diagnostics.Process()
        m_process.StartInfo.FileName = "prnjobs.vbs"
        m_process.StartInfo.Arguments = "-l"
        m_process.Start()

        Dim delay As Integer = 20 * 1000

        Me.CloseButton.Enabled = False
        Me.AbortButton.Enabled = False

        If m_process.WaitForExit(delay) Then
            Me.CloseButton.Enabled = True
        Else
            Me.AbortButton.Enabled = True
        End If

    End Sub

    Private Sub OnCloseButton(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseButton.Click
        Me.Close()
    End Sub

    Private Sub OnAbortButton(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AbortButton.Click
        m_process.Kill()
        Me.Close()
    End Sub
End Class

[/code]

David


David
0
 
LVL 2

Author Comment

by:Todd Wedlake
Comment Utility
David.  your code does the trick.  However, the process (vbscript) runs before the dialog box ever comes up.  Is there a way to make the dialog come up first, then run the script, then enable the close button.  and I'll add one more wish here.  :)
Is there a way to make the dialog come up, run the script, enable the close button, and if the user doesn't click the close button within say 30 seconds, to go ahead and close the dialog and end the app?


thanks
todd
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
Comment Utility
Here is my version...

(VB.Net 2005 - earlier versions have a Closing event instead of FormClosing)

Public Class Form1

    Private p As Process
    Private WithEvents tmr As New System.Windows.Forms.Timer()
    Private FormIsClosing As Boolean = False

    Private T As System.Threading.Thread
    Private Delegate Sub ProcessExited()

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' force the form to completely show before continuing
        Me.Show()
        Application.DoEvents()

        ' start up the vbs (hidden) and the monitor thread
        Dim vbs As String = "C:\Documents and Settings\Michael\My Documents\1 VB Code\2 VBScript Testing\Pause.vbs"
        p = New Process
        p.StartInfo.FileName = vbs
        p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
        p.Start()

        If Not (p Is Nothing) Then
            T = New System.Threading.Thread(AddressOf Me.MonitorProcess)
            T.Start()
        End If

        ' start up the timer
        tmr.Interval = 30000 ' 30 seconds
        tmr.Start()
    End Sub

    Private Sub p_Exited()
        ' this is run if the vbs file exits by itself (or is forcefully closed)
        ' marshal the call from the monitor thread onto'
        ' the main UI thread via a Delegate and Invoke()
        If Me.InvokeRequired Then
            Me.Invoke(New ProcessExited(AddressOf p_Exited))
        Else
            Me.Close()
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub

    Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick
        Me.Close()
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        FormIsClosing = True
        ' kill the vbs process if it is still running
        If Not (p Is Nothing) Then
            If Not p.HasExited Then
                p.Kill()
            End If
        End If
    End Sub

    Private Sub MonitorProcess()
        ' wait for the vbs to exit or the form to close
        If Not p.HasExited Then
            While (Not FormIsClosing) AndAlso (Not p.HasExited)
                System.Threading.Thread.Sleep(50)
            End While
        End If
        If Not FormIsClosing Then
            ' if the form isn't closing then close it
            Me.p_Exited()
        End If
    End Sub

End Class
0
 
LVL 11

Expert Comment

by:anyoneis
Comment Utility
It looks like Idle_Mind has got your question covered!

David
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

15 Experts available now in Live!

Get 1:1 Help Now