Solved

Console Application

Posted on 2013-06-13
5
244 Views
Last Modified: 2013-07-05
Greeting,

I have the following code in VS2012 Console application. When I ran it, the command window just hangs... Any Suggestions?

Sub Main()
        Dim proc As New Process()
        Dim timeout As Integer = 10000
        proc.StartInfo.CreateNoWindow = True
        proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
        proc.StartInfo.FileName = "c:/MyFile.bat"
        proc.StartInfo.RedirectStandardError = True
        proc.StartInfo.RedirectStandardOutput = True
        proc.StartInfo.UseShellExecute = False

        proc.Start()
        proc.WaitForExit(timeout)

        Dim errorMessage As String = proc.StandardError.ReadToEnd()
        proc.WaitForExit()
        If errorMessage = String.Empty Then
            Console.WriteLine("Success")
        Else
            Console.WriteLine("Failed: " & errorMessage)
        End If
    End Sub


Thanks in advance.
0
Comment
Question by:mrong
  • 3
  • 2
5 Comments
 
LVL 1

Accepted Solution

by:
William Grant earned 500 total points
ID: 39245631
I can think of two reasons:  1) the .bat file does not exist and the program is not throwing an exception for no file and 2) the .bat file does not contain any instructions for the program to execute.
  I had the same issue (trying your code) and when I gave the bat file an instruction to execute, the program operated normally.  However, if your batch file has some content, perhaps that content is forcing the window to hang while it waits for some input or process completion.
0
 

Author Comment

by:mrong
ID: 39245733
The window will still hang even I have already set timeout?
Thanks.
0
 
LVL 1

Expert Comment

by:William Grant
ID: 39245776
Yes.  The program first tries to start the process for the batch file, and then wait for that to finish before moving to the timeout.  If you placed your timeout first, the window would stop for the timeout and then move to start the batch file process and would, most likely, hang there as well.  
  Does your batch file have any commands in it? That is where your issue is at.
0
 

Author Comment

by:mrong
ID: 39245814
Yes, the batch file execute a program which stored on the network. I'm working from home today but vpn to company network. Should be fine....

One more question, Can I execute the command directly within the console app other than calling the bat?

Thanks.
0
 
LVL 1

Expert Comment

by:William Grant
ID: 39245864
You can.  If you are trying to call the app while using vb.net to run some other processes you could try:

>>place the following line outside the main function:
Declare Function sys Lib "msvcrt.dll" Alias "system" (ByVal command As String) As Integer

>>then in the function where you wish to call the batch file add the following line:
sys("C:\MyFile.Bat")

If you happen to get a IPStackImbalance error; this is easily overcome by selecting Debug->Exceptions->Managed Debugging Assistants and deselecting ContextSwitchDeadlock and PInvokeStackImbalance checkboxes.

this information/solution was found at:  http://jakash3.wordpress.com/2010/02/07/execute-system-command-within-vb-console-app/
and  
http://stackoverflow.com/questions/10880198/pinvokestackimbalance-was-detected-visual-basic-2010
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

679 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