I <3 Steve McConnell*
Coding Horror
programming and human factors
by Jeff Atwood

November 22, 2004

Full Threaded Shellicious

I couldn't resist adding some features to Shellicious. You can now run shell commands either asynchronously (as before) or synchronously, like so:

    Private WithEvents _s As New Shell
    Private _IsExecutionComplete As Boolean = False

    Public Sub Main()
        _s.UseNewThread = True
        _s.Execute("C:\LongRunningConsoleApp.exe")
        Do While Not _IsExecutionComplete
            '-- do other work here..
            Thread.Sleep(20)
        Loop
        Console.WriteLine("Exiting Sub Main()..")
        Console.ReadLine()
    End Sub

    Private Sub OutputLine(ByVal LineText As String) Handles _s.OutputLine
        Console.WriteLine(LineText)
    End Sub

    Private Sub ExecutionComplete(ByVal TimedOut As Boolean) _
        Handles _s.ExecutionComplete

        _IsExecutionComplete = True
        Console.WriteLine("execution complete; did we time out? " & TimedOut)
        If _s.ExitCode <> 0 Then
            Console.WriteLine(_s.Error)
        End If
        Console.WriteLine(_s.ExecutionTime)
        Console.WriteLine(_s.ExitCode)
    End Sub

I updated the code in the original post. And this time I remembered to give the threads names, which always helps in debugging:

The thread 'ShellErrorThread' (0xca4) has exited with code 0 (0x0).
The thread 'ShellOutputThread' (0x934) has exited with code 0 (0x0).
The thread 'ShellLaunchThread' (0x5c0) has exited with code 0 (0x0).

So far so good. The synchronous behavior respects the same .MaximumWaitSeconds property as before, and there's a new .CancelExecution method if you want to bail out on demand.

Posted by Jeff Atwood    View blog reactions

 

« Good programmers get off their butts Trapped in a Bitmapped World »

 

Comments

Great stuff! I have ported your work over to C#, posted below for the use of those who prefer it :)

I have a question for you too - I too wish to implement this for use on a webserver, whereby I need to call a seperate exe each time the website user requests a certain function. Did you ever have any worries about the performance of doing this (ie. calling an external exe), it doesn't seem as 'nice' to me as calling a referenced dll, but in this case that's not an option. I'm not sure whether I should be looking for another solution, or if my concerns over the performance and scalability of calling an external exe are unfounded... Any advice would be greatly appreciated :)

[ code removed -- c# version of Shell posted here ]

http://www.codinghorror.com/blog/archives/000134.html

Ben Empson on March 14, 2006 02:19 PM

I don't know if anyone would get this message and reply but I post it as I am desparate to solve a problem. Thanks for the above program I have got alot out of it and I used on NTBased Windows and it works fine. But for windows 98 it doesn't return anything. I replaced the cmd.exe with command.com but still doesn't work.

Anyone has any idea how to fix this problem, it is much appreciated.

Please send an email to behdadd@hotmail.com

Behdadd on May 9, 2006 06:04 PM







(hear it spoken)


(no HTML)




Content (c) 2008 Jeff Atwood. Logo image used with permission of the author. (c) 1993 Steven C. McConnell. All Rights Reserved.