Solved

Pause APP

Posted on 2006-06-08
6
278 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
ID: 16866361
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
ID: 16866556
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
ID: 16870235
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
ID: 16870598
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
ID: 16873718
It looks like Idle_Mind has got your question covered!

David
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

863 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

25 Experts available now in Live!

Get 1:1 Help Now