private void Work()
        {
            WorkerTask currentTask = null;

            while (_running)
            {
                currentTask = null;
                bool hasTasks = false;

                using (_writeLock.LockRead()) // faster because multiple threads can read at the same time.
                    hasTasks = _tasks.Count > 0;

                if (hasTasks)
                {
                    using (_writeLock.LockWrite())
                        if (_tasks.Count > 0)
                        {
                            currentTask = _tasks.Dequeue();
                        }

                    if (currentTask == null)
                    {
                        Thread.Sleep(1);
                        continue;
                    }

#if DEBUG
                    Logger.LogTrace($"Dequeued Job in WorkerThread: {currentTask.Task.Method.Name} ({_tasks.Count} tasks left.)");
#endif

                    try
                    {
                        currentTask.State = ETaskState.Executing;

                        object returnedValue = currentTask.Task.DynamicInvoke(currentTask.Parameters);

                        currentTask.ReturnedValue = returnedValue;
                        currentTask.State         = ETaskState.Done;
                    }
                    catch (Exception e)
                    {
                        currentTask.ExceptionThrown = e;
                        currentTask.State           = ETaskState.ExceptionThrown;

                        Logger.LogError("Exception in WorkerTask '" + currentTask.Task.Method.Name + "'.\n" + e);
                    }
                }
                else
                {
                    Thread.Sleep(1);
                }
            }

            Memory.FlushableMemoryPool.Destroy();
        }
示例#2
0
        private void Read(bool a, bool b, bool c)
        {
            if (a)
            {
                using (wla.LockRead())
                {
                    Assert.IsFalse(ba);

                    string s = " | " + (ba ? "1" : "0");

                    using (console.LockWrite())
                    {
                        Console.WriteLine("a is " + ia + s);
                    }

                    Assert.IsFalse(ba);
                }
            }
            else if (b)
            {
                using (UsableWriteLock.LockRead(wla, wlb))
                {
                    Assert.IsFalse(ba);
                    Assert.IsFalse(bb);

                    string s = " | " + (ba ? "1" : "0") + " | " + (bb ? "1" : "0");

                    using (console.LockWrite())
                    {
                        Console.WriteLine("a is " + ia + ", b is " + ib + s);
                    }

                    Assert.IsFalse(ba);
                    Assert.IsFalse(bb);
                }
            }
            else if (c)
            {
                using (UsableWriteLock.LockRead(wla, wlb, wlc))
                {
                    Assert.IsFalse(ba);
                    Assert.IsFalse(bb);
                    Assert.IsFalse(bc);

                    string s = " | " + (ba ? "1":"0") + " | " + (bb ? "1" : "0") + " | " + (bc ? "1" : "0");

                    using (console.LockWrite())
                    {
                        Console.WriteLine("a is " + ia + ", b is " + ib + ", c is " + ic + s);
                    }

                    Assert.IsFalse(ba);
                    Assert.IsFalse(bb);
                    Assert.IsFalse(bc);
                }
            }
        }
示例#3
0
        /// <summary>
        /// Notify all connected clients.
        /// </summary>
        /// <param name="notification">the notification to send</param>
        public void Notify(Notification notification)
        {
            Thread t = new Thread(() =>
            {
                string notificationText = notification.GetNotification();

                using (_listWriteLock.LockRead())
                    _proxies.ForEach(proxy => proxy.Respond(notificationText));
            });

            t.Start();
        }
示例#4
0
        /// <summary>
        /// Retrieves a local WebService of a specified Type.
        /// </summary>
        /// <param name="type">The type to generate a local Object of.</param>
        /// <returns>Returns an instance of the local WebService implementation.</returns>
        public object GetLocalService(Type type)
        {
            if (!type.GetInterfaces().Contains(typeof(IWebService)))
            {
                throw new IncompatibleTypeException($"Type '{type}' is not compatible with {nameof(WebServiceHandler)}: Does not implement '{nameof(IWebService)}'.");
            }

            if (type.GetConstructor(new Type[0]) == null)
            {
                throw new IncompatibleTypeException($"Type '{type}' is not compatible with {nameof(WebServiceHandler)}: No empty constructor available.");
            }

            if (type.IsAbstract || type.IsInterface || !type.IsPublic || type.IsSealed)
            {
                throw new IncompatibleTypeException("Only public non-abstract non-sealed Types of classes can be WebServices.");
            }

            bool contained = false;

            using (_listLock.LockRead())
                contained = LocalWebServiceVariants.ContainsKey(type);

            if (contained)
            {
                return(LocalWebServiceVariants[type]);
            }
            else
            {
                object ret = WebServiceImplementationGenerator.GetWebServiceLocalImplementation(type, this);

                using (_listLock.LockWrite())
                    LocalWebServiceVariants.Add(type, ret);

                return(ret);
            }
        }