Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello !!
i am new to coding and i am writing code in visual studio using c#.
I run a process there (basically double clicking a file which internally runs and give us output as text file).
In next line i wrote a code for getting that text file created as a result of process.
The problem is process running and giving text file as output take some seconds and mean while the next file which is getting text file says null. Because creation of text file need some seconds. Is there any method i can put a break or something so that when process.run is completed then next line of code should run.

What I have tried:

C#
Process.Start(@"D:\example.gms");
List<string> lines = File.ReadAllLines(@"D:\My.txt").ToList();
Posted
Updated 6-Feb-20 5:47am
v2

1 solution

Processes are independant threads, and they do not run in the same "space" as the code that "kicks them off" - so almost immediately you call Process.Start, the next line of code will probably be executed.

You can prevent this, by waiting for the process you start to complete:
C#
Process process = Process.Start(@"D:\example.gms");
process.WaitForExit();
List<string> lines = File.ReadAllLines(@"D:\My.txt").ToList();
But ... that will prevent the thread that started the new process from doing anything until the new process exits - which means that if you call this on your UI thread (the one that starts your app running) your app will stop and will not respond at all to user input while the new process is running. If it's likely to take a while, your user may become frustrated, so you will need to be careful (or code a lot more to get round it by moving the Process starting and waiting code into a BackgroundWorker thread).
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900