CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2006
    Location
    18° 32' N / 73° 52' E
    Posts
    416

    Question How to know when externally called program ends ?

    Hi all...

    I am calling one console application from my application and passing some parameters for its execution; after the console program runs successfully, it outputs a text file. and then closes itself automatically. (Takes some time to output text file.)

    I want to know when does this program ends. so that i can read the completly created text file.

    Is there any way to know this ?

    Thanks
    MMH.
    Regards,
    MMH
    Rate my post if you find it usefull.

  2. #2
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: How to know when externally called program ends ?

    It would be easier to make some suggestions if you post some of your code (specifically, the part that opens the console app and the method within this that writes to the textfile)

    but hey, maybe someone here has enough guru power to help you without it
    It's not a bug, it's a feature!

  3. #3
    Join Date
    Jan 2006
    Location
    18° 32' N / 73° 52' E
    Posts
    416

    Re: How to know when externally called program ends ?

    Basically, this is done using 'process.start();'. I am just calling the console executable file with passing some parameters like name + path of the file to output, some commands, etc. Rest of the job is done by the console exe. it will take in the parameters and start working on them. and will output the file. This takes some time. and after creation of the output file, it closes itself.
    Now the question is. When do i read the output file ? when will it finish writing it so that i read it completely without missing any information.

    I hope this explains in some depth.

    thanks
    MMH
    Regards,
    MMH
    Rate my post if you find it usefull.

  4. #4
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: How to know when externally called program ends ?

    Declare and initialize backgroundWorker control
    Code:
    System.ComponentModel.BackgroundWorker BackgroundWorker1;
    
    private void Form1_Load(object sender, System.EventArgs e)
    {
        BackgroundWorker1 = new System.ComponentModel.BackgroundWorker();
     
        BackgroundWorker1.DoWork += BackgroundWorker1_DoWork;
        BackgroundWorker1.RunWorkerCompleted += BackgroundWorker1_RunWorkerCompleted;
    }
    Start the BackGroundWorker and disable the btnFileRead
    Code:
    private void Button1_Click(object sender, System.EventArgs e)
    {
        BackgroundWorker1.RunWorkerAsync();
     
        TxtStatus.Text = "File Write in Progress...";
    
        btnFileRead.Enabled = false;
    }
    BackGroundWorker starts the process.
    Waits for it to complete.
    Enable btnFileRead upon completion.
    Code:
    private void BackgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
    {
        Process p = Process.Start(sAppPath);
    
        p.WaitForExit();
    }
    
    private void BackgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
    {
        TxtStatus.Text = "File Write Completed";
    
        btnFileRead.Enabled = true;
    }
    Last edited by aniskhan; December 21st, 2007 at 11:49 AM.

  5. #5
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: How to know when externally called program ends ?

    or you could use also the "Exit" event of the process object.
    Busy

  6. #6
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: How to know when externally called program ends ?

    Quote Originally Posted by Thread1
    or you could use also the "Exit" event of the process object.
    x2

    you beat me to it...

    the wait for exit is also an excellent method.

    I've wrapped quite a few command line applications as a windows app this way.

    good luck.

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: How to know when externally called program ends ?

    Try to open the textfile with a writelock. You can't until it's written fully.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: How to know when externally called program ends ?

    Quote Originally Posted by MadHatter
    x2

    you beat me to it...
    hehe happy holidays!
    Busy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured