Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by lowbloodsugar 1 · Aug 10, 2010 at 11:48 PM · external-application

Determine if an external application is running (Windows Standalone)

Hello,

The function that you would normally use to determine if an external application is running is:

System.Diagnostics.Process.GetProcessesByName("ProgramName")

This compiles, but throws an exception. I am guessing that it is some issue with windows7 security. If you get all of the processes and look at each one individually you would see some processes, but other would throw exceptions if you try to list their names.

The same code works properly when compiled by visual studio c#.

This is a test harness that will show you what i am talking about. Notice that not all of the processes are listed. Some processes that are even launched by you under your own account will generate an execption:

using UnityEngine; using System.Collections;

public class test: MonoBehaviour {

 // Use this for initialization
 void Start () {
     try
     {
         foreach (System.Diagnostics.Process p 
                     in 
                 System.Diagnostics.Process.GetProcesses())
         {
             try
             {
                 print(p.ToString());
             }catch(System.Exception e)
             {
                 print("Mini exception when trying to list the name " + e);
             }
         }
     }catch(System.Exception e)
     {
         print("Exception caught "  + e);
     }
 }

 // Update is called once per frame
 void Update () {

 }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cyb3rManiak · Aug 11, 2010 at 12:44 AM 0
Share

Can't test it out at the moment... Just a thought though - have you tried doing it using P-Invoke? $$anonymous$$ight be interesting to see if you get better results.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by thelackey3326 · Jan 31, 2012 at 12:48 AM

This question is old, but I ran into the same problem today while testing before an alpha deployment. GetProcessesByName() works just fine on Windows XP (my development platform), but throws an InvalidOperationException in Windows 7 Pro.

Then I found this: Mono Bugzilla: Bug 596779

And it was reportedly fixed here: Release Notes Mono 2.6.7

The reported bug is similar to the described problem, and may be related. At any rate, we found today that we can reproduce this issue using Unity v3.4 and v3.5 RC1. So, this may be fixed when Unity catches up to Mono 2.6.7 (if it hasn't already). The answer at the moment seems to be to iterate through the whole list of processes, but catch the InvalidOperationException inside the loop body, similar to the way lowbloodsugar 1 has done. Here's an example of killing all running instances of Notepad:

void KillRunningInstances()
{
    //NOTE: GetProcessByName() doesn't seem to work on Win7
    //Process[] running = Process.GetProcessesByName("notepad");
    Process[] running = Process.GetProcesses();
    foreach (Process process in running)
    {
        try
        {
            if (!process.HasExited && process.ProcessName == "notepad")
            {
                process.Kill();
                process.WaitForExit(1000);
            }
        }
        catch (System.InvalidOperationException)
        {
            //do nothing
            UnityEngine.Debug.Log("***** InvalidOperationException was caught!");
        }
    }
}

It would be a good idea to make sure that the process you are trying to find is not causing the exception to throw, otherwise it will be ignored.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ben Blaut · Jul 23, 2013 at 07:35 PM 0
Share

I'm running into the same problem, however this answer does the same exact thing as GetProcessesByName().

avatar image
0

Answer by miker88 · Oct 29, 2013 at 06:42 AM

The methods in System.Diagnostics.Process work only after you built your app and does not work while in Editor mode. I've tested this in Win8.1 using Unity Pro 4.2.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image adamtuliper · Sep 12, 2015 at 05:59 AM 0
Share

just verified in unity 5.2, running System.Diagnostics.Process.GetProcessesByName("Notepad"); in the editor works fine

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

In build - Process cannot find file 1 Answer

[SOLVED] Unable To Import Fonts? Unable to open assets/fonts check external settings 2 Answers

Integrate Unity inside a QT Application 0 Answers

How to launch another program when using or interacting with an object? 1 Answer

Embed and communicate with Unity inside another app 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges