Console and windows application in vb.net

Can I add a windows application to my console application

In other words can I have both under one .exe file

I know that I can add a form to a consol application and it will appear once I run the .exe file but the thing is that I can not interact with the form. It’s like freezes.



Answer this question

Console and windows application in vb.net

  • CHowell

    Thanks guys that helped.

    Now..

    Here is a little background of what I am trying to do..

    1) I need to be able to run a windows application which generates a batch file (.bat). [DONE]

    2) Once I open that batch file I want it to be able to pass a string to console which is included with the windows application.

    (Maybe this is confusing a bit)

    3) Using one (.exe) file for both lunching the windows form once and passing a string to it if runs from batch file another time.

    a) test.exe will lunch a form and generate a batch file.

    b) test.exe "string"

    I am trying to clarify as much as I could


  • JohnsonZhang

    OK - so youve generated the bat file and want to call it passing in a string from the windows application.

    Process.Start will enable you to do this.

    http://msdn2.microsoft.com/en-us/library/h6ak8zt5.aspx

    An Example

     ' Run the Foo.bat passing in the path to a htm file.
    Process.Start("Foo.bat", "C:\myPath\myFile.htm")

    Obviously the path parameter here can be a variable or property from you Windows Application.   You simply need to put this line after you have created the bat file, and determined the variab;e/property you wish to call the bat file with.

     

     


  • EricPaul

    Thanks Ali,

    Is this example written in C# or C++

    Sorry I am doing my coding using vb.net

    Maybe it not hard to convert these codes to vb.net

    But I am not sure how


  • AllanP

    Hi,

    If your form freezes then I think you have some sort of loop in your command application. If there is some loop then just put following statement in loop body some where.

    System.Windows.Forms.Application.DoEvents();

    For example I have a console application I added a form in it Form1.
    When I run application console continues running and I can freely interact with controls on Form1.

    namespace ConsoleApplication

    {

    class Program

    {

    static void Main(string[] args)

    {

    Form1 frm = new Form1();

    frm.Show();

    while (true)

    {

    Console.WriteLine (DateTime.Now.ToString ("HH:MM:ss"));

    System.Windows.Forms.Application.DoEvents();

    }

    }

    }

    }

    Hope this solve your problem.



  • victoriak68

    Hi,

    Following is same thing in VB.NET

    Module Module1

    Sub Main()

    Dim frm As New Form1()

    frm.Show()

    While (True)

    Console.WriteLine("monitoring some thing...")

    System.Windows.Forms.Application.DoEvents()

    End While

    End Sub

    End Module



  • Neil Walters

    Thnaks Spotty
  • hghua

    So you want to capture the output of the batch file for use in your windows application.

    In which case you can use something like the following to capture run the bat file and capture the output as a string - which you can then use in you windows application.

    Public Function ProcessStartAndCaptureOutput(byval CmdFile as string, ByVal args As String) As String
    'Diagnostics.Process.Start(CmdFile, args)
    Dim p As Diagnostics.Process = New Diagnostics.Process
    p.StartInfo.FileName = CmdFile
    p.StartInfo.Arguments = args
    p.StartInfo.UseShellExecute = False
    p.StartInfo.RedirectStandardOutput = True
    p.Start()
    Return p.StandardOutput.ReadToEnd
    p.WaitForExit()
    End Function


  • Scott W.

    In stead of throwing in a DoEvents, you should launch the form with the Run method from Windows.Forms.Application as below. If you need your console application to continue processing, you will need to do that portion on another thread, making sure to synchronize the threads to close the form when the console app closes. You might want to consider moving to a windows application with a notification icon implementation as an alternative as well depending on your needs.

    Public Shared Sub Main()

    Dim f As New Form1

    Console.WriteLine("Before form started")

    Windows.Forms.Application.Run(f)

    Console.WriteLine("After form closed")

    Console.ReadLine()

    End Sub

    Jim Wooley
    http://devauthority.com/blogs/jwooley/default.aspx



  • Usai

    Not really, sorry for the confusing

    What I want to do is when I open the batch it will pass a string back to the console which is part of the windows application

    Then I can use the [ process.start]


  • Console and windows application in vb.net