Quantcast
Channel: Answers by "thelackey3326"
Viewing all articles
Browse latest Browse all 30

Answer by thelackey3326

$
0
0

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.


Viewing all articles
Browse latest Browse all 30

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>