[Mono-list] Problem enumerating all mono processes on the system.

Brett Senior brett_senior at yahoo.com.au
Sun Aug 17 01:18:35 EDT 2008


The following is some code that I have written to determine if the currently running program is already running.  The way that I tackled it was to search for all mono processes and then within the process module collection to see if any of them match the name of the program (minus the directory part of the filename) then I increment a counter.  If the counter is greater than one then the program returns true, otherwise false.

Problem is that even if I have multiple instances of program 'xxx' running using mono, only one mono process is returned.  Within this process then only one instance of 'xxx' is found in the process module collection.  So the below is unable to detect multiple instances of the program.

Can someone look at the below and point out what I am doing wrong ?
 

       internal bool Is_Application_Already_Running()
        {
            int pc;
            
            string pn1;
            string pn2;
            
            string [] cla;
            
            System.Diagnostics.Process [] pra;
            System.Diagnostics.ProcessModuleCollection pmc;
            
            //
            //Now using the command line (and its arguments), determine the name of the .exe
            //that was run.
            //
            cla = System.Environment.GetCommandLineArgs();
            
            pn1 = cla[0];
            pn2 = pn1.Substring(pn1.LastIndexOf(System.IO.Path.DirectorySeparatorChar) + 1);
            
            //
            //Now obtain all processes that are running this program.  Note: As these programs are
            //run under MONO, we must find all processes running this and then determine via the
            //name whether we should terminate them.
            //
            pc = 0;
            pra = System.Diagnostics.Process.GetProcessesByName("mono");
            
            //
            //Now loop and kill each of the above processes - but not ourself.
            //
            for (int ix = 0; ix <= pra.GetUpperBound(0); ++ix)
            {
                //
                //First obtain the modules associated with the process so we can
                //then determine if it is running PN2.
                //
                pmc = pra[ix].Modules;
                
                //
                //Now loop and check the filename associated with the process module
                //and if we find a match then count up the number of instances.
                //
                for (int jx = 0; jx < pmc.Count; ++jx)
                {
                    if (pmc[jx].FileName.EndsWith(pn2))
                    {
                        pc++;
                    }
                }
            }
            
            //
            //Now return true to indicate that the process is running elsewhere if the
            //process count is greater than one.
            //
            return (pc > 1);
        }


Thanks,
Brett Senior.



      Win a MacBook Air or iPod touch with Yahoo!7. http://au.docs.yahoo.com/homepageset
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-list/attachments/20080816/2ede7521/attachment-0001.html 


More information about the Mono-list mailing list