Solved

Console Application

Posted on 2013-06-13
5
239 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

747 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

12 Experts available now in Live!

Get 1:1 Help Now