Solved

Console Application

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

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

Suggested Solutions

Title # Comments Views Activity
TFS 2015 Access denied. 1 34
asp.net figure out error 3 41
RestClient.cs fails at (HttpWebResponse)request.GetResponse()) 4 47
Run software updates from the website 6 45
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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