x


Type 'System.Diagnostics.Process' does not contain a definition for 'CloseMainWindow'

Hello!

I am writing an application in Unity that requires another application to be running. I have been able to successfully start another program from Unity, but I have not been able to close it. I am

using System.Diagnostics
...

Process p = new Process();
p.StartInfo.FileName = @"C:\path\to\program.exe";
p.Start();  // works up to this point
p.CloseMainWindow(); // throws a compiler error

When I try to run this code, I get a compile error:

Type 'System.Diagnostics.Process' does not contain a definition for 'CloseMainWindow' and no extension method 'CloseMainWindow' of type 'System.Diagnostics.Process' could be found (are you missing a using directive or an assembly reference?)

I get a similar error for the Kill() method.

Both the Kill() and CloseMainWindow() methods are suggested by intellisense in the Monodevelop IDE, and documented in the C# .NET

My theory is that Unity has blocked some features of the .NET framework to prevent a Unity process from shutting down other processes. I have tried using System.Reflection to look inside the Process class, but it isn't working for some mysterious reason (this being my first time using reflection in C#).

How can I close this extra process when the user closes Unity?

Thanks!

more ▼

asked Dec 22, 2011 at 10:09 PM

JoeProgram gravatar image

JoeProgram
46 1 2 3

I have not figured out what is going on, but I have figured out a workaround. Replace:

p.CloseMainWindow()

with:

p.GetType().GetMethod( "CloseMainWindow" ).Invoke( p, new object[]{} );

Using reflection shows that CloseMainWindow and Kill are both still defined, so it seems like a Unity-specific compiler issue.

Another gotcha: If the new process steals focus from Unity, Unity will pause its execution and will not run the CloseMainWindow method until you click back on it.

Dec 22, 2011 at 07:08 PM JoeProgram

Hmm, that sounds strange. According to the MonoCompatibility page this method is supported for standalone builds.

The focus problem is related to your runInBackground - setting which can be configured in the Player settings.

If you're sure that it won't compile in Unity you could create a small sample project which reproduces the problem and file a bugreport so the Unity staff know about the problem.

Dec 23, 2011 at 01:22 AM Bunny83

I started to do this process, but then in my debug project CloseMainWindow() worked perfectly. Something must be obscuring the method in my original project.

Dec 23, 2011 at 05:38 PM JoeProgram
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x241
x75
x42
x3

asked: Dec 22, 2011 at 10:09 PM

Seen: 2176 times

Last Updated: Dec 23, 2011 at 05:38 PM