Unclicked Msgbox Holding "Pausing" Application

s-c

Member
Joined
Jun 25, 2007
Messages
10
Programming Experience
1-3
Using Visual Studio .NET 2002:
Trying to make a program that will run as a service (but right now implementing it as a forms app) that scan through the running processes looking for one in particular. This process must be closed by a certain time.

In case someone is using the application, I have popped up warnings at a certain time before the process will be killed. But it appears if the person forgot and just left their computer on, everything hangs on the first msgbox, and nothing happens.

Code is below, I'd be gracious.

VB.NET:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim local As Process() = Process.GetProcesses
        Dim i As Integer
        Dim tod As String
        tod = TimeString()
        tod = StrReverse(tod)
        tod = tod.Remove(0, 3)
        tod = StrReverse(tod)
        'MessageBox.Show(tod)
        If tod = "10:05" Then
            For i = 0 To local.Length - 1
                If Strings.UCase(local(i).ProcessName) = Strings.UCase("process") Then
                    MsgBox("process will shut down in 5 minutes", MsgBoxStyle.Critical, "process")
                Else
                    Me.Close()
                End If
            Next
        ElseIf tod = "10:08" Then
            For i = 0 To local.Length - 1
                If Strings.UCase(local(i).ProcessName) = Strings.UCase("process") Then
                    MsgBox("process OM will shut down in 2 minutes")
                Else
                    Me.Close()
                End If
            Next
        ElseIf tod = "10:09" Then
            For i = 0 To local.Length - 1
                If Strings.UCase(local(i).ProcessName) = Strings.UCase("process") Then
                    MsgBox("process will shut down in 1 Minute", MsgBoxStyle.Critical, "process")
                    MsgBox("It Is Recommended that you save your changes immediately", MsgBoxStyle.Critical, "process")
                Else
                    Me.Close()
                End If
            Next
        ElseIf tod = "10:10" Then
            For i = 0 To local.Length - 1
                If Strings.UCase(local(i).ProcessName) = Strings.UCase("process") Then
                    local(i).Kill()
                End If
            Next
            Me.Close()
        End If
    End Sub
 
ive the same issue when working with on comms stuff for plc's, the only way ive found round it is to not use messagebox, i created a panel that i bring to the front with a message i want displayed and an ok box that causes it to gotoback
 
Or you could just put the message box on a seperate thread so it runs asynchronously. Use the backgroundworker or System.Threading.Thread object to attach a sub to it and start the thread. Creating a wrapper class to this would make it simpler to customize your message boxes and re-use them if you need it more than once.
 
Like I mentioned, put the msgbox on a seperate thread. It's only a few lines of code and you don't have to re-invent the wheel like that project does. :eek:)

I'm typing this on a Mac and I'm going by memory so I cannot guarantee accuracy, but I think it's something like this (you SHOULD be able to copy and paste this verbatim):

VB.NET:
Imports Microsoft.VisualBasic

Public Class AsyncMsgBoxClass

    Public Event GotMsgBoxResult(sender As Object, e As DialogResult)

    Public msgText as String
    Public msgStyle As MsgBoxStyle
    Public msgCaption As String
    Public Result As DialogResult
    ' ... other vars as you see fit

    Public Sub Display()
        Me.Result = MsgBox(Me.msgText, Me.msgStyle, Me.msgCaption)

        ' fire an event which passes the result back to our owner
        RaiseEvent GotMsgBoxResult(sender as Object, Me.Result)
    End Sub
End Class

and here is the code you would put in your form or elsewhere to get the above to display a message box asynchronously, and fire an event handler when a result is gotten:

VB.NET:
Private Sub HelloWorldMsgBox
    Dim oThread as System.Threading.Thread
    Dim oMsgBox as New AsyncMsgBoxClass

    ' we want our routine below to fire when we get a result
    AddHandler oMsgBox.GotMsgBoxResult, AddressOf GotHelloWorldMsgBoxResult

    ' lets create a new thread class which we will point directly to the Display routine
    oThread = new Thread(AddressOf oMsgBox.Display)

    oMsgBox.msgText = "Hello, world! Do you want me here?"
    oMsgBox.msgCaption = "Cliche MsgBox"
    oMsgBox.msgStyle = MsgBoxStyle.Exclamation Or MsgBoxStyle.YesNo

    oThread.Start()
End Sub

Private Sub GotHelloWorldMsgBoxResult(sender As Object, e As DialogResult)
    Debug.WriteLine("Result is: " & e)

    If e = DialogResult.No Then
        Debug.WriteLine("Sad! :o(")
    End If

    ' or alternatively, cast the class and access it directly:
    Dim oSender As AsyncMsgBoxClass = DirectCast(sender, AsyncMsgBoxClass)
    Debug.WriteLine("Result is: " & oSender.Result)
End Sub

This should be a lot easier than making your own forms. :) Customize it to your liking. You can steal my code as long as you promise to make it 100% reusable in its own class!! :p
 
Using Visual Studio .NET 2002:
Trying to make a program that will run as a service (but right now implementing it as a forms app) that scan through the running processes looking for one in particular. This process must be closed by a certain time.

In case someone is using the application, I have popped up warnings at a certain time before the process will be killed. But it appears if the person forgot and just left their computer on, everything hangs on the first msgbox, and nothing happens.

MessageBox is a modal dialog; it is designed to stop the active thread at the time of showing. DONT use it if you want your thread to carry on!

Never, ever, ever try to write a service that pops up a message box or has any form of user interaction. That's not what a service is for.

Educate your users that they must finish that program by X time. Send them an email (or maybe you can use the Messenger service, like NET SEND) saying that they have 5 minutes to finish up, then close the app.


Also, never, ever look at times using =
If you computer is busy for some reason it might jsut be that the event doesnt fire until 10:06

Always use > or < when timing things to happen by a certain time. OK, with bigger time intervals it is less likely that something will go wrong.. but dont use =

Also, thats some lengthy time string code you have there.. just use a DateTime object!

VB.NET:
Dim dt as DateTime = DateTime.Now
'Exit the sub if it is before 605 minutes into the day
If dt.Hour() * 60 + dt.Minute() < 605 Then Return


If you mean 10 pm, use 1325 minutes into the day
 
There is some overloads of the MessageBox.Show method where you for MessageBoxOptions can specify enum value ServiceNotification (or DefaultDesktopOnly). This message is displayed on active desktop (or interactive). It is used in rare cases from services, usually when some error occur. Services usually only report to an event log, or use IPC methods to communicate with UI applications running in the interactive desktop.
 
Back
Top