Exemplo n.º 1
0
        public void asyncRequest(AsyncPathRequest request)
        {
            // Get the worker
            WorkerThread thread = findSuitableWorker();

            // Push the request
            thread.asyncRequest(request);
        }
Exemplo n.º 2
0
 public void asyncRequest(AsyncPathRequest request)
 {
     // Lock the queue
     lock (incoming)
     {
         // Push the request
         incoming.Enqueue(request);
     }
 }
Exemplo n.º 3
0
        private void threadMain()
        {
            // Set the flag
            isRunning = true;

            // Used to calcualte the average
            Stopwatch timer = new Stopwatch();

            try
            {
                // Loop forever
                while (isRunning == true)
                {
                    // Get a request
                    AsyncPathRequest request = null;

                    // Lock the input queue
                    lock (incoming)
                    {
                        // Calcualte the current thread load
                        calcualteLoad();

                        // Get a request
                        if (incoming.Count > 0)
                        {
                            request = incoming.Dequeue();
                        }
                    }

                    // Check for a request
                    if (request == null)
                    {
                        idleFrames++;

                        // Take a long sleep - no load
                        Thread.Sleep((int)targetTime);
                        continue;
                    }

                    // Reset the idle frames
                    idleFrames = 0;

                    // Begin timing
                    timer.Reset();
                    timer.Start();

                    // Lock the grid while we search
                    lock (request.Grid)
                    {
                        // Execute the request
                        request.Grid.findPath(request.Start, request.End, request.Diagonal, (Path path, PathRequestStatus status) =>
                        {
                            // Create a result
                            AsyncPathResult result = new AsyncPathResult(request, path, status);

                            // Push the result to the outgoing queue
                            lock (outgoing)
                            {
                                // Add result
                                outgoing.Enqueue(result);
                            }
                        });
                    }

                    // Stop timing and calculate the average time
                    timer.Stop();
                    calculateAverageTime(timer.ElapsedMilliseconds);

                    // Calculate the amount of rest time based on the thread load
                    int sleepDuration = (int)((1 - threadLoad) * targetTime);

                    // Sleep based on the current thread load
                    Thread.Sleep(sleepDuration);
                } // End while
            }
            catch (System.Exception e)
            {
                Debug.WriteLine(e);
            }
        }
Exemplo n.º 4
0
 // Constructor
 public AsyncPathResult(AsyncPathRequest request, Path result, PathRequestStatus status)
 {
     this.request = request;
     this.result  = result;
     this.status  = status;
 }