Solved

Console Application

Posted on 2013-06-13
5
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

734 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