Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 637699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:39:09+00:00 2026-05-13T20:39:09+00:00

I write two windows console application as below class ProgramA { static void Main(string[]

  • 0

I write two windows console application as below

class ProgramA
{
    static void Main(string[] args)
    {

        FileStream fs = File.Open(@"E:\console.txt", FileMode.OpenOrCreate);
        StreamWriter sw = new StreamWriter(fs);
        TextWriter old = Console.Out;
        Console.SetOut(sw);
        Console.WriteLine("Process A Started " + DateTime.Now.ToString());
        sw.Flush();
        Process p = new Process();
        ProcessStartInfo pi = new ProcessStartInfo();
        pi.FileName = @"D:\ProgramB.exe";
        p.StartInfo = pi;
        pi.UseShellExecute = true;
        pi.RedirectStandardOutput = false;
        bool result = p.Start();

        Console.WriteLine("Process A Ended " + DateTime.Now.ToString());
        sw.Flush();
    }
}

And

class ProgramB
{
    static void Main(string[] args)
    {
        try
        {
            FileStream fs = File.Open(@"E:\console.txt", FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(fs);
            TextWriter old = Console.Out;
            Console.SetOut(sw);
            Console.WriteLine("Process B output " + DateTime.Now.ToString());
            sw.Flush();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
}

They are totally different exe files. When I debug ProgramA step by step with F5, the result in E:\console.txt is

Process A Started 2/26/2010 13:43:59
Process A Ended 2/26/2010 13:44:03

But if I just run ProgramA with Ctrl+F5, the result in E:\console.txt is

Process B output 2/26/2010 13:48:50
Process A Ended 2/26/2010 13:48:50

I am confused. Originally, I want to have the 2 process both write to the same file. But after some reflection, I thought since Process A has taken control of the console.txt first, Process B shouldn’t be able to have access to console.txt, and the result should be similar to below:

Process A Started 2/26/2010 13:43:59
Process A Ended 2/26/2010 13:44:03

with Process B throw an access denied exception. But it’s not. Why? Where is the “Process A Started” message?


I change my code to the below:

class ProgramA
{
    static void Main(string[] args)
    {

        FileStream fs = File.Open(@"E:\console.txt", FileMode.OpenOrCreate);
        StreamWriter sw = new StreamWriter(fs);
        TextWriter old = Console.Out;
        Console.SetOut(sw);
        Console.Write("aaaaa");
        Console.Write("AAAAA");
        sw.Flush();
        Process p = new Process();
        ProcessStartInfo pi = new ProcessStartInfo();
        pi.FileName = @"D:\ProgramB.exe";
        p.StartInfo = pi;
        pi.UseShellExecute = true;
        pi.RedirectStandardOutput = false;
        bool result = p.Start();
        Console.Write("aaaaa");
        sw.Flush();
    }

And

class ProgramB
{
    static void Main(string[] args)
    {
        try
        {
            FileStream fs = File.Open(@"E:\console.txt", FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(fs);
            TextWriter old = Console.Out;
            Console.SetOut(sw);
            Console.Write("bbb");
            sw.Flush();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
}

And the content of console.txt is

bbbaaAAAAAaaaaa

Maybe this could explain something. I am figuring it out.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:39:09+00:00Added an answer on May 13, 2026 at 8:39 pm

    Two things:

    • first, since you have not used FileShare.None, both exes will have access to the file, so Process B can still access the file.

    • second, since you have opened the file with OpenOrCreate (and nothing else), the file will be opened and writes you do will be from the start of the file. Try doing a FileStream.Seek to seek to the end of the file before writing. Opening the file in FileMode.Append won’t really help (if you run them in parallel), I think, as both processes will still trample on each other.

    Better yet, try doing a FileStream.Lock to ensure the two processes don’t trample on each other.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 312k
  • Answers 313k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In your controller, you can access ViewHelpers through $this->view->gravatar($email) Your… May 13, 2026 at 10:43 pm
  • Editorial Team
    Editorial Team added an answer If you want "simple" then use SimpleXML, not DOM. Note… May 13, 2026 at 10:43 pm
  • Editorial Team
    Editorial Team added an answer Basically, the ASP.NET membership service creates a GUID per User,… May 13, 2026 at 10:43 pm

Related Questions

Possible Duplicate: Locking main() thread Below you'll find my code, Main calls two threads,
I have a project that is deployed to production as a windows service. However
I’m well aware of the Microsoft support base article stating that it’s not supported
I know it sounds stupid but: I've found this application written on Mono and
Given a file tree - a directory with directories in it etc, how would

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.