.net - Limit the maximum process-instances by counting the amount of Mutexes? -


i've written next code prevents multi-instancing via mutex, extend funcionality allowing set maximum number of allowed instances, should decide whether want allow single instance or multi-instace of 3 instances (for example).

i've tried figure out how count amount of mutexes of process, didn't find about.

by other hand i've found lot of posts in stackoverflow explaining how count amount of processes it's filename, i'm not interested in of 'cause i've choosed mutex detection me more secure.

someone me in whim?.

this have done:

namespace      partial friend class myapplication  #region " properties "          ''' <summary>         ''' gets current process mutex identifier.         ''' </summary>         ''' <value>the current process mutex identifier.</value>         ''' <exception cref="system.formatexception">the specified value not valid guid format.</exception>         private readonly property mutexid string                             ' define golabl unique identifier name mutex.                 dim id string = "b045ce40-2863-4ce7-a7df-8afca8214454"                  if guid.tryparse(input:=id, result:=new guid)                     return id                 else                     throw new formatexception("the specified value not in valid guid format.")                 end if              end         end property          ''' <summary>         ''' gets maximum instances allowed process.         ''' </summary>         ''' <value>the maximum instances allowed process.</value>         private readonly property maxinstances integer                             return 2             end         end property  #end region  #region " private methods "          ''' <summary>         ''' determines whether unique instance running process.         ''' </summary>         ''' <returns><c>true</c> if unique instance; otherwise, <c>false</c>.</returns>         private function isuniqueinstance() boolean              dim mtx threading.mutex = nothing              try                 mtx = threading.mutex.openexisting(name:=me.mutexid)                 mtx.close()                 mtx = nothing             catch                 mtx = new threading.mutex(initiallyowned:=true, name:=me.mutexid)             end try              return mtx isnot nothing          end function  #end region  #region " event-handlers "          ''' <summary>         ''' occurs when application starts, before startup form created.         ''' </summary>         ''' <param name="sender">the source of event.</param>         ''' <param name="e">the <see cref="applicationservices.startupeventargs"/> instance containing event data.</param>         private sub myapplication_startup(byval sender object, byval e applicationservices.startupeventargs) _         handles me.startup              ' if there more 1 instance running of process same mutex then...             if not me.isuniqueinstance ' prevent multi-instancing.                  messagebox.show("this limited demo, run multiple instances please purchase program.",                                application.info.productname, messageboxbuttons.ok, messageboxicon.error)                  ' cancel application execution.                 e.cancel = true              end if          end sub  #end region      end class ' myapplication  end namespace 

update

this code i'm trying, i've tried in application events, in form constructor, in load , shown event, multiinstances not detected.

    private sub test() handles me.startup          using semaphore new semaphore(3i, 3i, "something")              if not semaphore.waitone(100i)                 msgbox("already max instances running")                 end             else                 msgbox("test")             end if          end using      end sub 

mutex mutual exclusion. atmost allow 1 thread proceed. if need allow number of threads(process in case) proceed need semaphore rather mutex.

c# sample:

private static void main() {     using (semaphore semaphore = new semaphore(3, 3, assembly.getexecutingassembly().getname().fullname))     {         if (!semaphore.waitone(100))         {             messagebox.show("already max instances running");             return;         }          //start application here         application.enablevisualstyles();         application.setcompatibletextrenderingdefault(false);         application.run(new form1());     } } 

vb.net sample:

private shared sub main()     using semaphore new semaphore(3, 3, assembly.getexecutingassembly().getname().fullname)         if not semaphore.waitone(100)             messagebox.show("already max instances running")             return         end if         'start application here         application.enablevisualstyles()         application.setcompatibletextrenderingdefault(false)         application.run(new form1())     end using end sub 

p.s: code converted http://converter.telerik.com/ c# vb


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -