Ejemplo n.º 1
0
        public void SetCache(string cacheKey, MethodBase methodBase)
        {
            if (!AddInQueue(cacheKey))
            {
                return;
            }
            var cache = Cache;

            ThreadPoolEx.QueueUserWorkItemWithCatch(
                state =>
            {
                var service    = UnityContainer.Resolve(methodBase.DeclaringType);
                var result     = methodBase.Invoke(service, new object[0]);
                var resultType = result.GetType();
                if (resultType.IsGenericType && typeof(IQueryable <>)
                    .MakeGenericType(resultType.GetGenericArguments())
                    .IsAssignableFrom(resultType)
                    )
                {
                    var value = LoadQueryable(result);
                    cache.Insert(cacheKey, value,
                                 null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration);
                }
                RemoveFromQueue(cacheKey);
            });
        }
Ejemplo n.º 2
0
        public Boolean reparse()
        {
            lock (_lock)
            {
                Boolean result = true;

                if (!Constant.RunStatus.STOP.Equals(this.RunStatus))
                {
                    result = false;
                }
                else
                {
                    this.workItemsGroup = ThreadPoolEx.getWorkItemsGroup();

                    this.WaitQueue    = new Hashtable();
                    this.StartQueue   = new Hashtable();
                    this.successQueue = new Hashtable();
                    this.errorQueue   = new Hashtable();

                    this.RunStatus = Constant.RunStatus.RUNING;
                }

                return(result);
            }
        }
Ejemplo n.º 3
0
            public IMethodReturn Invoke(IMethodInvocation input,
                                        GetNextHandlerDelegate getNext)
            {
                if (CachingService.Cache == null)
                {
                    return(getNext()(input, getNext));
                }
                var cacheKey = CacheUtils.GetCacheKey(input.MethodBase);

                var cacheValue = CachingService.Cache[cacheKey];

                if (cacheValue != null)
                {
                    return(input.CreateMethodReturn(cacheValue));
                }
                var currentLock = _locks.GetOrAdd(cacheKey, new object());

                lock (currentLock) {
                    cacheValue = CachingService.Cache[cacheKey];
                    if (cacheValue != null)
                    {
                        return(input.CreateMethodReturn(cacheValue));
                    }
                    if (!NotBlock)
                    {
                        return(AddResultToCache(getNext, input, cacheKey));
                    }

                    ThreadPoolEx.QueueUserWorkItemWithCatch(state =>
                                                            AddResultToCache(getNext, input, cacheKey));
                    var result = input.MethodBase.As <MethodInfo>().ReturnType.Create();
                    AddToCache(result, cacheKey);
                    return(input.CreateMethodReturn(result));
                }
            }
Ejemplo n.º 4
0
        /**
         * 守护进程
         */
        private void daemon_timer_Tick(object sender, EventArgs e)
        {
            try
            {
                tssl_mail_send_status.Text = "待发邮件:" + MailSendService.sendWaitCnt + "; 已发邮件:" + MailSendService.sendSuccessCnt + "; 无法发送数:" + MailSendService.sendErrorCnt;

                //如果队列已经结束,则重跑邮箱解析为0的站点
                if (ckb_reparse.Checked && ThreadPoolEx.IsIdle() && runtimeService.getAll().Count > 0)
                {
                    foreach (DictionaryEntry de in (Hashtable)runtimeService.getAll().Clone())
                    {
                        RuntimeEntity runtime = (RuntimeEntity)de.Value;

                        if (runtime.AllMailCnt == 0 && runtime.reparse())
                        {
                            parseService.add((String)de.Key, tb_type.Text.Trim());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message, ex);
            }
        }
Ejemplo n.º 5
0
        private void Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            foreach (DictionaryEntry de in (Hashtable)runtimeService.getAll().Clone())
            {
                RuntimeEntity runtimeEntity = runtimeService.get((String)de.Key);

                if (runtimeEntity.getWaitQueueCnt() > 0 || runtimeEntity.getStartQueueCnt() > 0)
                {
                    if (MessageBox.Show("还有任务没有完成,是否强制关闭程序?", "提醒", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
                    {
                        e.Cancel = true;
                    }
                    break;
                }
            }

            if (MailSendService.sendWaitCnt > 0 && MessageBox.Show("还有邮件正在发送,是否强制关闭程序?", "提醒", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
            {
                e.Cancel = true;
            }

            if (!e.Cancel)
            {
                ThreadPoolEx.destory();
            }
        }
Ejemplo n.º 6
0
		public SchedulingService() {
			_paused = false;
			_halted = false;
			_threadPool = new ThreadPoolEx(ThreadPoolEx.DefaultIdleTimeout, 10);
			_thread = new Thread(new ThreadStart(Run));
			_thread.Start();
		}
Ejemplo n.º 7
0
        public async Task Test1( )
        {
            bool wasInvoked = false;
            await ThreadPoolEx.InvokeAsync(
                () => { wasInvoked = true; });

            Assert.IsTrue(wasInvoked);
        }
Ejemplo n.º 8
0
 public SchedulingService()
 {
     _paused     = false;
     _halted     = false;
     _threadPool = new ThreadPoolEx(ThreadPoolEx.DefaultIdleTimeout, 10);
     _thread     = new Thread(new ThreadStart(Run));
     _thread.Start();
 }
Ejemplo n.º 9
0
        /// <summary>
        ///
        /// </summary>
        public ExecutionManager(Arbiter arbiter)
        {
            _arbiter    = arbiter;
            _threadPool = new ThreadPoolEx(typeof(ExecutionManager).Name);
            _threadPool.MaximumSimultaniouslyRunningThreadsAllowed = 20;
            _threadPool.MaximumTotalThreadsAllowed = 25;

            _timeOutMonitor.EntityTimedOutEvent += new HandlerDelegate <TimeOutable>(_timeOutMonitor_EntityTimedOutEvent);
        }
Ejemplo n.º 10
0
        private void btn_apply_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dgv_config.Rows)
            {
                ConfigService.update((String)row.Cells["name"].Value, (String)row.Cells["value"].Value);
            }

            ThreadPoolEx.maxThread(Convert.ToInt32(ConfigService.get("thread.maxCnt")));

            MessageBox.Show("配置修改完成~");
        }
Ejemplo n.º 11
0
        public async Task Test2( )
        {
            bool wasInvoked = false;
            int  n          = await ThreadPoolEx.InvokeAsync(
                () => {
                wasInvoked = true;
                return(42);
            });

            Assert.IsTrue(wasInvoked);
            Assert.AreEqual(42, n);
        }
Ejemplo n.º 12
0
        private void btn_start_Click(object sender, EventArgs e)
        {
            try
            {
                if ("".Equals(tb_searchKey.Text.Trim()))
                {
                    XtraMessageBox.Show("请输入搜索引擎检索关键字!");
                    return;
                }

                btn_start.Enabled = false;

                if (workItemsGroup == null)
                {
                    workItemsGroup = ThreadPoolEx.getWorkItemsGroup(5);

                    workItemsGroup.OnIdle += new WorkItemsGroupIdleHandler(delegate(IWorkItemsGroup target)
                    {
                        foreach (String url in engineList[0].AllUrlMap.Values)
                        {
                            parseService.add(url, Main.type);
                        }

                        XtraMessageBox.Show("搜索引擎共抓取出" + engineList[0].AllUrlMap.Count + "个站点提交分析");

                        engineList[0].AllUrlMap.Clear();

                        this.Close();
                    });
                }

                foreach (EngineEntity engine in engineList)
                {
                    if (engine.Selected && "未处理".Equals(engine.Status))
                    {
                        Hashtable param = new Hashtable();
                        param.Add("engine", engine);
                        param.Add("searchKey", tb_searchKey.Text.Trim());

                        engine.Status = "等待处理";

                        ThreadPoolEx.add(workItemsGroup, new WorkItemCallback(EngineService.search), param, WorkItemPriority.Normal);
                    }
                }
            }
            catch { }
            finally { btn_start.Enabled = true; }
        }
Ejemplo n.º 13
0
        public Main()
        {
            ServicePointManager.DefaultConnectionLimit = 256;

            InitializeComponent();


            //非正式版区别
            if (!"FULL".Equals(RegisterService.getRegisterInfo().VersionType))
            {
                //不显示邮箱维护
                tsmi_mail_address.Enabled = false;

                //不显示导入导出
                tsmi_export_import.Enabled = false;

                //不能发送邮件
                btn_send_mail.Enabled = false;
            }

            //初始化线程池
            ThreadPoolEx.init();

            //init urlMap
            {
                urlDataTable = new DataTable();
                urlDataTable.Columns.Add("URL", typeof(string));
                urlDataTable.Columns.Add("等待处理个数", typeof(Int32));
                urlDataTable.Columns.Add("正在处理个数", typeof(Int32));
                urlDataTable.Columns.Add("处理成功个数", typeof(Int32));
                urlDataTable.Columns.Add("请求网页超时数", typeof(Int32));
                urlDataTable.Columns.Add("抓取非重复邮箱数", typeof(Int32));
                urlDataTable.Columns.Add("状态", typeof(string));

                dgv_url.DataSource = urlDataTable;
            }

            //init dgv_mail_send_log
            {
                sendLog = new DataTable();
                sendLog.Columns.Add("信息", typeof(string));
                dgv_mail_send_log.DataSource = sendLog;
            }

            cb_parse_run_status.SelectedIndex = 1;
        }
Ejemplo n.º 14
0
 public void Start()
 {
     try {
         _socket = new Socket(_endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
         _socket.Bind(_endPoint);
         //Set to SOMAXCONN (0x7FFFFFFF), the underlying service provider will set the backlog to a maximum reasonable value
         _socket.Listen(0x7FFFFFFF);
         int loopCount = 0;
         for (int i = 0; i < _acceptCount; i++)
         {
             _socket.BeginAccept(new AsyncCallback(BeginAcceptCallback), this);
             ThreadPoolEx.LoopSleep(ref loopCount);
         }
     } catch (Exception ex) {
         //log.Fatal(__Res.GetString(__Res.SocketServer_ListenerFail), ex);
         HandleError(ex);
     }
 }
Ejemplo n.º 15
0
        public void add(String url, String type)
        {
            url = url.Replace("\r\n", "").Replace(Environment.NewLine, "").Trim();

            RuntimeEntity runtime = runtimeService.get(url);

            if (runtime == null)
            {
                runtime = runtimeService.add(url);
            }

            if (Constant.RunStatus.RUNING.Equals(runtime.RunStatus))
            {
                try
                {
                    if (get(url) == null)
                    {
                        runtime.WaitQueue.Add(url, new ParseEntity(url, type));

                        Hashtable param = new Hashtable();
                        param.Add("url", url);
                        param.Add("type", type);

                        lock (runtime.WorkItemsGroup)
                        {
                            ThreadPoolEx.add(runtime.WorkItemsGroup, new WorkItemCallback(ParseThread.run), param, runtime.Priority);
                        }
                    }
                }
                catch { }

                //等待解析数超过系统设定,则优先级降低
                if (!WorkItemPriority.Lowest.Equals(runtime.Priority) && (runtime.WaitQueue.Count + runtime.StartQueue.Count + runtime.SuccessQueue.Count) > Convert.ToInt32(ConfigService.get("parse.lowParseCnt")))
                {
                    runtime.Priority = WorkItemPriority.Lowest;
                }

                //已解析数超过系统设定,则停止
                if (runtime.WaitQueue.Count + runtime.StartQueue.Count + runtime.SuccessQueue.Count + runtime.ErrorQueue.Count >= Convert.ToInt32(ConfigService.get("parse.rule.maxParseCnt")))
                {
                    runtime.RunStatus = Constant.RunStatus.STOPING;
                }
            }
        }
Ejemplo n.º 16
0
        private void InvokeContinuation()
        {
            Debug.Assert(_continuation != null);
            var continuation = _continuation !;

            switch (_capturedContext)
            {
            case null:
                if (RunContinuationsAsynchronously)
                {
#if TARGETS_NET || TARGETS_NETCORE || GREATERTHAN_NETSTANDARD13
                    if (_executionContext != null)
                    {
                        ThreadPoolEx.QueueUserWorkItem(continuation, _continuationState, true);
                        return;
                    }
#endif
                    ThreadPoolEx.QueueUserWorkItem(continuation, _continuationState, true);
                    return;
                }

                continuation(_continuationState);
                return;

            case SynchronizationContext sc:
                sc.Post(s =>
                {
                    var state = (Tuple <Action <object?>, object?>)s !;
                    state.Item1(state.Item2);
                }, Tuple.Create(continuation, _continuationState));
                return;

            case TaskScheduler ts:
#if NET40
                Task.Factory.StartNew(continuation, _continuationState, CancellationToken.None, TaskCreationOptions.None, ts);
#else
                Task.Factory.StartNew(continuation, _continuationState, CancellationToken.None, TaskCreationOptions.DenyChildAttach, ts);
#endif
                return;

            default:
                break;
            }
        }
Ejemplo n.º 17
0
 public void Start()
 {
     try
     {
         this._socket = new System.Net.Sockets.Socket(this._endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
         this._socket.Bind(this._endPoint);
         this._socket.Listen(0x7fffffff);
         int loopIndex = 0;
         for (int i = 0; i < this._acceptCount; i++)
         {
             this._socket.BeginAccept(new AsyncCallback(this.BeginAcceptCallback), this);
             ThreadPoolEx.LoopSleep(ref loopIndex);
         }
     }
     catch (Exception exception)
     {
         this.HandleError(exception);
     }
 }
Ejemplo n.º 18
0
        /// <summary>Schedules the continuation action for this operation.</summary>
        /// <param name="continuation">The continuation to invoke when the operation has completed.</param>
        /// <param name="state">The state object to pass to <paramref name="continuation"/> when it's invoked.</param>
        /// <param name="token">Opaque value that was provided to the <see cref="ValueTask"/>'s constructor.</param>
        /// <param name="flags">The flags describing the behavior of the continuation.</param>
        public void OnCompleted(Action <object?> continuation, object?state, short token, ValueTaskSourceOnCompletedFlags flags)
        {
            if (continuation == null)
            {
                throw new ArgumentNullException(nameof(continuation));
            }
            ValidateToken(token);

            if ((flags & ValueTaskSourceOnCompletedFlags.FlowExecutionContext) != 0)
            {
#if TARGETS_NET || TARGETS_NETCORE || GREATERTHAN_NETSTANDARD13
                _executionContext = ExecutionContext.Capture();
#endif
            }

            if ((flags & ValueTaskSourceOnCompletedFlags.UseSchedulingContext) != 0)
            {
                var sc = SynchronizationContext.Current;
                if (sc != null && sc.GetType() != typeof(SynchronizationContext))
                {
                    _capturedContext = sc;
                }
                else
                {
                    var ts = TaskScheduler.Current;
                    if (ts != TaskScheduler.Default)
                    {
                        _capturedContext = ts;
                    }
                }
            }

            // We need to set the continuation state before we swap in the delegate, so that
            // if there's a race between this and SetResult/Exception and SetResult/Exception
            // sees the _continuation as non-null, it'll be able to invoke it with the state
            // stored here.  However, this also means that if this is used incorrectly (e.g.
            // awaited twice concurrently), _continuationState might get erroneously overwritten.
            // To minimize the chances of that, we check preemptively whether _continuation
            // is already set to something other than the completion sentinel.

            object?oldContinuation = _continuation;
            if (oldContinuation == null)
            {
                _continuationState = state;
                oldContinuation    = Interlocked.CompareExchange(ref _continuation, continuation, null);
            }

            if (oldContinuation == null)
            {
                return;
            }

            // Operation already completed, so we need to queue the supplied callback.
            if (!ReferenceEquals(oldContinuation, ManualResetValueTaskSourceCoreShared.Sentinel))
            {
                throw new InvalidOperationException();
            }

            switch (_capturedContext)
            {
            case null:
#if TARGETS_NET || TARGETS_NETCORE || GREATERTHAN_NETSTANDARD13
                if (_executionContext != null)
                {
                    ThreadPoolEx.QueueUserWorkItem(continuation, state, true);
                    return;
                }
#endif
                ThreadPoolEx.UnsafeQueueUserWorkItem(continuation, state, true);
                return;

            case SynchronizationContext sc:
                sc.Post(s =>
                {
                    var tuple = (Tuple <Action <object?>, object?>)s !;
                    tuple.Item1(tuple.Item2);
                }, Tuple.Create(continuation, state));
                return;

            case TaskScheduler ts:
#if NET40
                Task.Factory.StartNew(continuation, state, CancellationToken.None, TaskCreationOptions.None, ts);
#else
                Task.Factory.StartNew(continuation, state, CancellationToken.None, TaskCreationOptions.DenyChildAttach, ts);
#endif
                return;
            }
        }