Start a process in two different ways

T

Tony Johansson

Hi!

I would say that when you start notepad using alternative 1 or 2 is
identical.
So in this case it's no point to use alternative 2 because alternative 1 is
doing the exact same thing and with much less code.
Does anyone agree with me ?

1. Process myProcess2 = Process.Start("notepad.exe");

2. Process myProcess = new Process();
2. ProcessStartInfo myProcessStartInfo = new
ProcessStartInfo("notepad.exe");
2. myProcess.StartInfo = myProcessStartInfo;
2. myProcess.Start();

//Tony
 
P

Peter Duniho

Tony said:
Hi!

I would say that when you start notepad using alternative 1 or 2 is
identical.
So in this case it's no point to use alternative 2 because alternative 1 is
doing the exact same thing and with much less code.
Does anyone agree with me ?

Given the contrived example, yes…there's no point in using the second
approach.

Noting, of course, that in your contrivance you have added an extra line
of code that is itself unnecessary:

Process myProcess = new Process();
myProcess.StartInfo = new
ProcessStartInfo("notepad.exe");
myProcess.Start();

And if you want to get really fancy:

new Process()
{
StartInfo = new ProcessStartInfo("notepad.exe")
}.Start();

That's still more wordy than, say, this:

Process.Start("notepad.exe");

But you can see that it's not as bad as your example suggests, and it
could be a convenient way to initialize a Process that needs other
settings than just the executable name. For example:

new Process()
{
StartInfo = new ProcessStartInfo("notepad.exe")
{
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Maximized
}
}.Start();

(Which is itself a bit contrived, since the WindowStyle property doesn't
affect how notepad.exe actually starts up…but hopefully you get the idea).

Pete
 
T

Tony Johansson

Peter Duniho said:
Given the contrived example, yes…there's no point in using the second
approach.

Noting, of course, that in your contrivance you have added an extra line
of code that is itself unnecessary:

Process myProcess = new Process();
myProcess.StartInfo = new
ProcessStartInfo("notepad.exe");
myProcess.Start();

And if you want to get really fancy:

new Process()
{
StartInfo = new ProcessStartInfo("notepad.exe")
}.Start();

That's still more wordy than, say, this:

Process.Start("notepad.exe");

But you can see that it's not as bad as your example suggests, and it
could be a convenient way to initialize a Process that needs other
settings than just the executable name. For example:

new Process()
{
StartInfo = new ProcessStartInfo("notepad.exe")
{
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Maximized
}
}.Start();

(Which is itself a bit contrived, since the WindowStyle property doesn't
affect how notepad.exe actually starts up…but hopefully you get the idea).

Pete

using this way of writing is not supported by VS 2005(2.0).
What version must you at least have to support this construction ?
new Process()
{
StartInfo = new ProcessStartInfo("notepad.exe")
}.Start();

Is it the same if I write it in this way putting in the Start within the
code block{}
new Process()
{
StartInfo = new ProcessStartInfo("notepad.exe")
.Start();
}

//Tony

//Tony
 
F

Family Tree Mike

using this way of writing is not supported by VS 2005(2.0).
What version must you at least have to support this construction ?
new Process()
{
StartInfo = new ProcessStartInfo("notepad.exe")
}.Start();

Is it the same if I write it in this way putting in the Start within the
code block{}
new Process()
{
StartInfo = new ProcessStartInfo("notepad.exe")
.Start();
}

//Tony

//Tony

You need at least VS 2008 (Express is fine).
 
P

Peter Duniho

Tony said:
using this way of writing is not supported by VS 2005(2.0).

That's correct.
What version must you at least have to support this construction ?

Property initializers are supported in C# 3.0 and later. So, Visual
Studio 2008 and later.
new Process()
{
StartInfo = new ProcessStartInfo("notepad.exe")
}.Start();

Is it the same if I write it in this way putting in the Start within the
code block{}
new Process()
{
StartInfo = new ProcessStartInfo("notepad.exe")
.Start();
}

You can't do the second example. It's not legal code. The only thing
that can go in the block following the constructor are property
initializers.

The compiler rewrites them as plain assignments to the properties (i.e.
calls to the setters) on the newly created object, executed before any
assignment of the reference to the new object to any variable, method
argument, etc.

So, for example, this:

new Process()
{
StartInfo = new ProcessStartInfo("notepad.exe")
{
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Maximized
}
}.Start();

…is simply rewritten as something like this:

Process processTemp = new Process();
ProcessStartInfo psiTemp = new ProcessStartInfo("notepad.exe");

psiTemp.UseShellExecute = false;
psiTemp.WindowStyle = ProcessWindowStyle.Maximized;
processTemp.StartInfo = psiTemp;

processTemp.Start();

You can see this more explicitly if you actually compile the example and
then examine the output in ILDASM or Reflector.

Pete
 
M

Mihai N.

The onle benefit of 2 is if you want to fine-tune a bit more the
ProcessStartInfo (stuff like redirect standard input/output/error,
mess with the environment, working directory, etc.)
See the ProcessStartInfo Members

As used in your post, there is no benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top