⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 taskmanager.cs

📁 ajax patterns 这是关于ajax设计模式方面的原代码
💻 CS
字号:
/*
    Copyright 2006 Christian Gross http://www.devspace.com
    From the book Ajax Patterns and Best Practices

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Collections;
using System.Threading;
using Devspace.Commons.Tracer;

namespace Devspace.Ajax.InfiniteData {
    #region Base Interfaces
    public interface IData {
        ITask InstantiateTask();
    }
    public interface ITask {
        string TransactionIdentifier { get; set;}
        void Execute( ITaskManager taskManager);
    }
    public interface IResult {
        string Result {
            get;
        }
        string TransactionIdentifier {
            get;
        }
    }
    public interface ITaskManager {
        void AddResult( IResult result);
    }
    #endregion
    
    public class TaskManagerImpl : ITaskManager {
        #region Data Members
        private Queue<ITask> _tasks = new Queue< ITask>();
        private Queue<IResult> _results = new Queue< IResult>();
        private Thread _thread = null;
        private int _taskCount;
        private object _objTaskCount = new Object();
        #endregion
        
        public TaskManagerImpl( ) {
            _taskCount = 0;
        }
        
        #region Task Management
        private ITask GetTask() {
            ITask task = null;
            lock( _tasks) {
                if( _tasks.Count > 0) {
                    task = _tasks.Dequeue();
                }
            }
            return task;
        }
        public void ProcessTasks() {
            ITask task = null;
            while( true) {
                task = GetTask();
                if( task != null) {
                    task.Execute( this);
                    lock( _objTaskCount) {
                        _taskCount --;
                    }
                }
                else {
                    lock( this) {
                        task = GetTask();
                        if( task == null) {
                            _thread = null;
                            break;
                        }
                    }
                }
            }
        }
        public bool AnyOutstandingTasks() {
            lock( _objTaskCount) {
                return _taskCount != 0;
            }
        }
        public void RunThreadedTasks() {
            lock( this) {
                if( _thread == null) {
                    _thread = new Thread( new ThreadStart( this.ProcessTasks));
                    _thread.Start();
                }
            }
        }
        public void AddTask( ITask task) {
            lock( _tasks) {
                _tasks.Enqueue( task);
                lock( _objTaskCount) {
                    _taskCount ++;
                }
            }
        }
        
        #endregion
        
        #region Results
        public void AddResult(IResult result) {
            GenerateOutput.Write("TaskManagerImpl.AddResult", "Generated result (" + result.ToString() + ")");
            Monitor.Enter( _results);
            _results.Enqueue( result);
            Monitor.Pulse( _results);
            Monitor.Exit( _results);
        }
        private IResult GetSingleResult() {
            IResult result = null;
            if( _results.Count > 0) {
                result = _results.Dequeue();
                GenerateOutput.Write("TaskManagerImpl.GetSingleResult", "Retrieving result (" + result.ToString() + ")");
            }
            return result;
        }
        public IResult GetResult() {
            Monitor.Enter( _results);
            IResult result = GetSingleResult();
            Monitor.Exit( _results);
            return result;
        }
        public IResult GetResultWait( int timeout) {
            IResult result = null;
            Monitor.Enter( _results);
            result = GetSingleResult();
            if( result == null) {
                Monitor.Wait( _results, timeout * 1000);
                result = GetSingleResult();
            }
            Monitor.Exit( _results);
            return result;
        }
        #endregion
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -