Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Console Application

Posted on 2013-06-13
5
Medium Priority
?
254 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 2000 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Loops Section Overview
Suggested Courses

916 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