Click here to Skip to main content
15,902,189 members
Home / Discussions / C#
   

C#

 
GeneralRe: Consuming webservices using SOAP Pin
sk84059-Oct-07 3:56
sk84059-Oct-07 3:56 
QuestionMutex.ReleaseMutex() asynchronous ? Pin
Stevo Z9-Oct-07 3:26
Stevo Z9-Oct-07 3:26 
AnswerRe: Mutex.ReleaseMutex() asynchronous ? Pin
led mike9-Oct-07 5:34
led mike9-Oct-07 5:34 
GeneralRe: Mutex.ReleaseMutex() asynchronous ? Pin
Stevo Z9-Oct-07 6:05
Stevo Z9-Oct-07 6:05 
GeneralRe: Mutex.ReleaseMutex() asynchronous ? Pin
led mike9-Oct-07 6:13
led mike9-Oct-07 6:13 
GeneralRe: Mutex.ReleaseMutex() asynchronous ? Pin
Stevo Z9-Oct-07 7:06
Stevo Z9-Oct-07 7:06 
GeneralRe: Mutex.ReleaseMutex() asynchronous ? Pin
led mike9-Oct-07 7:20
led mike9-Oct-07 7:20 
GeneralRe: Mutex.ReleaseMutex() asynchronous ? Pin
Stevo Z9-Oct-07 22:47
Stevo Z9-Oct-07 22:47 
Try this one:

<br />
<br />
public class SingleProgramInstance : IDisposable<br />
{<br />
       //private members <br />
        private Mutex __mutex;<br />
        private bool __firstInstance = false;<br />
        private string __uniqueName = String.Empty;<br />
       <br />
        private Mutex Mutex<br />
        {<br />
            get { return __mutex; }<br />
            private set { __mutex = value; }<br />
        }<br />
<br />
        public string UniqueName<br />
        {<br />
            get { return __uniqueName; }<br />
            private set { __uniqueName = value; }<br />
        }<br />
 <br />
        public SingleProgramInstance()<br />
            : this(String.Empty)<br />
        {      }<br />
        <br />
        public SingleProgramInstance(string identifier)<br />
        {<br />
            //Initialize a named mutex and attempt to<br />
            // get ownership immediately.<br />
            //Use an addtional identifier to lower<br />
            // our chances of another process creating<br />
            // a mutex with the same name.<br />
<br />
            this.UniqueName =  Assembly.GetExecutingAssembly().GetName().Name + identifier;<br />
<br />
            this.Mutex = new Mutex(true, this.UniqueName, out __firstInstance);<br />
        }<br />
<br />
        // Disposing resources<br />
        ~SingleProgramInstance()<br />
        {            <br />
            this.Dispose();<br />
        }<br />
      <br />
        public bool IsFirstInstance<br />
        {<br />
            get { return __firstInstance; }<br />
        } <br />
        <br />
        public void Dispose()<br />
        {<br />
            if (this.IsFirstInstance && this.Mutex != null)<br />
            {<br />
                this.Mutex.ReleaseMutex();<br />
                this.Mutex = null;<br />
<br />
                GC.SuppressFinalize(this);<br />
            }            <br />
        }       <br />
    }<br />
<br />
class Program<br />
    {<br />
        static void Main(string[] args)<br />
        {<br />
            using (SingleProgramInstance spi = new SingleProgramInstance("zxy123"))<br />
            {<br />
                if (spi.IsFirstInstance)            <br />
                    Console.WriteLine("First Instance");               <br />
<br />
                else                <br />
                    Console.WriteLine("!!!!! NOT First Instance !!!!!!");              <br />
<br />
                spi.Dispose();<br />
            }<br />
<br />
            Console.ReadKey();<br />
<br />
            Process.Start("test73_Mutex.exe");<br />
        }<br />
    }<br />
<br />


this fails from time to time


zilo
GeneralRe: Mutex.ReleaseMutex() asynchronous ? Pin
led mike10-Oct-07 4:45
led mike10-Oct-07 4:45 
GeneralRe: Mutex.ReleaseMutex() asynchronous ? Pin
Stevo Z10-Oct-07 5:24
Stevo Z10-Oct-07 5:24 
GeneralRe: Mutex.ReleaseMutex() asynchronous ? Pin
led mike10-Oct-07 5:58
led mike10-Oct-07 5:58 
AnswerRe: Mutex.ReleaseMutex() asynchronous ? Pin
Stevo Z10-Oct-07 8:36
Stevo Z10-Oct-07 8:36 
GeneralRe: Mutex.ReleaseMutex() asynchronous ? Pin
led mike10-Oct-07 8:51
led mike10-Oct-07 8:51 
GeneralRe: Mutex.ReleaseMutex() asynchronous ? Pin
Stevo Z10-Oct-07 9:32
Stevo Z10-Oct-07 9:32 
GeneralRe: Mutex.ReleaseMutex() asynchronous ? Pin
led mike10-Oct-07 10:13
led mike10-Oct-07 10:13 
GeneralRe: Mutex.ReleaseMutex() asynchronous ? Pin
Stevo Z10-Oct-07 10:45
Stevo Z10-Oct-07 10:45 
GeneralRe: Mutex.ReleaseMutex() asynchronous ? Pin
led mike10-Oct-07 11:03
led mike10-Oct-07 11:03 
GeneralRe: Mutex.ReleaseMutex() asynchronous ? Pin
Stevo Z10-Oct-07 11:14
Stevo Z10-Oct-07 11:14 
GeneralRe: Mutex.ReleaseMutex() asynchronous ? Pin
led mike10-Oct-07 11:36
led mike10-Oct-07 11:36 
GeneralRe: Mutex.ReleaseMutex() asynchronous ? Pin
Stevo Z10-Oct-07 11:44
Stevo Z10-Oct-07 11:44 
GeneralRe: Mutex.ReleaseMutex() asynchronous ? Pin
Dave Kreskowiak10-Oct-07 14:05
mveDave Kreskowiak10-Oct-07 14:05 
GeneralRe: Mutex.ReleaseMutex() asynchronous ? Pin
Stevo Z10-Oct-07 20:25
Stevo Z10-Oct-07 20:25 
GeneralRe: Mutex.ReleaseMutex() asynchronous ? Pin
Stevo Z10-Oct-07 22:01
Stevo Z10-Oct-07 22:01 
GeneralRe: Mutex.ReleaseMutex() asynchronous ? Pin
Dave Kreskowiak11-Oct-07 1:56
mveDave Kreskowiak11-Oct-07 1:56 
QuestionWeb browser control (Print() functionality) - Printing in landscape from code Pin
madhusri9-Oct-07 3:25
madhusri9-Oct-07 3:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.