Mono.Unix.FileMutex.Wait C# (CSharp) Method

Wait() public method

public Wait ( int millisecondsTimeout ) : bool
millisecondsTimeout int
return bool
        public bool Wait(int millisecondsTimeout)
        {
            lock (typeof(FileMutex))
            {
                if (handle == 0)
                    Open();

                bool result = false;

                ThreadStart placeLock = delegate()
                {
                    // a write (exclusive) lock
                    wl.l_type = LockType.F_WRLCK;
                    int res = Syscall.fcntl(handle, FcntlCommand.F_SETLKW, ref wl);

                    if (res == 0 && Syscall.GetLastError() != Errno.EAGAIN)
                        result = true;
                };

                if (millisecondsTimeout == -1)
                {
                    // Console.WriteLine("waiting in the calling thread");
                    placeLock();
                }
                else
                {
                    // Console.WriteLine("waiting in the separate thread");
                    Thread t = new Thread(placeLock);
                    t.IsBackground = true;
                    t.Start();
                    if (!t.Join(millisecondsTimeout))
                    {
                        //timeout
                        t.Abort();
                    }
                }

                return result;
            }
        }

Usage Example

Esempio n. 1
0
        static void __Main(string[] args)
        {
            string file = @"/home/user/Desktop/lock2";

            FileMutex mutex = new FileMutex(file);

            Console.WriteLine("Trying to obtain exclusive lock...");

            mutex.Wait(1000 * 5);
            Console.WriteLine("exclusive lock is obtained...");

            Console.WriteLine("Press 'Enter' to release lock.");
            Console.ReadLine();

            mutex.Release();
            Console.WriteLine("Lock is released.");

            Console.ReadLine();
            Console.WriteLine("Press 'Enter' to exit.");
        }
All Usage Examples Of Mono.Unix.FileMutex::Wait