示例#1
0
        void CreateThreads()
        {
            for (int n = 1; ; n++)
            {
                MapObjectAttachedObject startObject = GetFirstAttachedObjectByAlias(string.Format("thread{0}Start", n));
                MapObjectAttachedObject endObject   = GetFirstAttachedObjectByAlias(string.Format("thread{0}End", n));
                if (startObject == null || endObject == null)
                {
                    break;
                }

                MeshObject meshObject = SceneManager.Instance.CreateMeshObject("Base\\Simple Models\\Box.mesh");
                if (meshObject == null)
                {
                    break;
                }

                meshObject.SetMaterialNameForAllSubObjects("Black");
                meshObject.CastShadows = true;

                ThreadItem item = new ThreadItem();
                item.startObject = startObject;
                item.endObject   = endObject;
                item.meshObject  = meshObject;
                item.sceneNode   = new SceneNode();
                item.sceneNode.Attach(item.meshObject);

                MapObject.AssociateSceneNodeWithMapObject(item.sceneNode, this);

                threads.Add(item);
            }

            UpdateThreads();
        }
        private void proc(object state)
        {
            ThreadItem <TResult> item = state as ThreadItem <TResult>;

            item.StartTime = DateTime.Now;
            while (true)
            {
                if (item.IsCanceled)
                {
                    break;
                }

                TEntity entity = Queue.Dequeue();
                if (entity == null)
                {
                    break;
                }

                TResult r = Processor(entity);
                item.Result.Add(r);

                item.ProcessedCount++;
            }
            item.EndTime = DateTime.Now;
            item.waiter.Set();
        }
        public List <ThreadItem> Start()
        {
            actualThreadCount = ThreadCount;
            if (actualThreadCount > Queue.Count)
            {
                actualThreadCount = Queue.Count;
            }

            threadList = new List <ThreadItem>();

            for (int i = 0; i < actualThreadCount; i++)
            {
                ThreadItem item = new ThreadItem();
                item.Index  = i;
                item.waiter = new ManualResetEvent(false);
                Thread thread = new Thread(proc);
                thread.IsBackground = true;
                thread.Name         = "处理线程_" + i.ToString();
                item.Thread         = thread;
                item.IsCanceled     = false;

                threadList.Add(item);
            }

            for (int i = 0; i < actualThreadCount; i++)
            {
                threadList[i].Thread.Start(threadList[i]);
            }

            WaitHandle.WaitAll(threadList.Select(x => x.waiter).ToArray());

            return(threadList.ToList());
        }
示例#4
0
        private void ProcessJobQueue()
        {
            if (m_NewJobsAdded)
            {
                lock (m_NewJobs)
                {
                    while (m_NewJobs.Count > 0)
                    {
                        AddJob(m_NewJobs.Dequeue());
                    }

                    m_NewJobsAdded = false;
                }
            }
            while (m_Jobs.Count > 0 && m_Threads.Count > 0)
            {
                var job = m_Jobs.Dequeue();
                if (!job.IsAborted)
                {
                    var thread = m_Threads.Pop();
                    thread.StartJob(job);
                    // add thread to the linked list of active threads
                    thread.NextActive = m_Active;
                    m_Active          = thread;
                }
            }
        }
示例#5
0
 public void Post(SendOrPostCallback callback, object state)
 {
     lock (sync)
     {
         ThreadItem item = new ThreadItem(callback, state, null);
         Items.Enqueue(item);
     }
 }
示例#6
0
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            var uri    = "https://adnmb2.com/api/thread/" + (string)e.Parameter;
            var result = GetPostContent(uri);

            _threaditems = JsonConvert.DeserializeObject <ThreadItem>(result);
            contentListView.ItemsSource = _threaditems.replys;
        }
示例#7
0
 public PostViewModel(ThreadItem thread)
 {
     Console.Write("postviewmodel load\n");
     Thread          = thread;
     AgoraService    = new AgoraService(thread);
     Title           = thread.Topic;
     Posts           = new ObservableCollection <PostItem>();
     LoadPostCommand = new Command(async() => await ExecuteLoadPostCommand());
     SendPostCommand = new Command <String>(async(UserPost) => await ExecuteSubmitPostAsync(UserPost));
 }
示例#8
0
        private void CheckActiveJobs()
        {
            ThreadItem thread = m_Active;
            ThreadItem last   = null;

            while (thread != null)
            {
                ThreadItem next = thread.NextActive;
                T          job  = thread.Data;
                if (job.IsAborted)
                {
                    if (last == null)
                    {
                        m_Active = next;
                    }
                    else
                    {
                        last.NextActive = next;
                    }

                    thread.NextActive = null;

                    thread.Reset();
                    m_Threads.Push(thread);
                }
                else if (thread.Data.IsDataReady)
                {
                    job.OnFinished();
                    if (OnJobFinished != null)
                    {
                        OnJobFinished(job);
                    }

                    if (last == null)
                    {
                        m_Active = next;
                    }
                    else
                    {
                        last.NextActive = next;
                    }

                    thread.NextActive = null;

                    thread.Reset();
                    m_Threads.Push(thread);
                }
                else
                {
                    last = thread;
                }

                thread = next;
            }
        }
示例#9
0
 public void Execute()
 {
     lock (sync)
     {
         var oCount = Items.Count;
         for (int i = 0; i < oCount; i++)
         {
             ThreadItem item = Items.Dequeue();
             item.Invoke();
         }
     }
 }
示例#10
0
        private void StopServiceWorker(object param)
        {
            ThreadItem item = param as ThreadItem;

            if (workers.Contains(item.Worker))
            {
                workers.Remove(item.Worker);
            }

            ServiceController service = item.Service;

            this.Invoke(new StringInvoker(delegate(string serviceName)
            {
                statusBox.Text = String.Format(
                    translator.GetString("StoppingMsg"),
                    serviceName
                    );
            }),
                        service.ServiceName);

            service.Stop();

            try
            {
                service.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 15));
            }
            catch (System.ServiceProcess.TimeoutException)
            {
                //MessageBox.Show(
                //    translator.GetString("StopTimeoutMsg"),
                //    translator.GetString("TimeoutTitle"),
                //    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception exc)
            {
                ExceptionDialog.ShowException(exc);
            }

            if (refreshTimer != null)
            {
                DiscoverServices();
            }

            this.Invoke(new MethodInvoker(delegate
            {
                statusBox.Text = String.Empty;
            }));
        }
示例#11
0
        public void AddTask(Delegate task, object[] @params)
        {
            lock (addlock)
            {
                ThreadItem item = new ThreadItem(task, @params);

                if (IsFull)
                {
                    _queue.Enqueue(item);
                }
                else
                {
                    StartThread(item);
                }
            }
        }
示例#12
0
        async Task ExecuteCreateThreadCommand(String threadName)
        {
            if (IsBusy)
            {
                return;
            }
            IsBusy = true;
            try
            {
                var uuid = await AgoraService.CreateThreadAsync(threadName);

                lastThread = await AgoraService.GetThreadAsync(uuid);
            }
            finally { IsBusy = false; }
            CreateThreadCommand.ChangeCanExecute();
        }
示例#13
0
 public void ShutdownQueue()
 {
     for (var thread = m_Active; thread != null; thread = thread.NextActive)
     {
         thread.Abort();
     }
     while (m_Threads.Count > 0)
     {
         m_Threads.Pop().Abort();
     }
     while (m_Jobs.Count > 0)
     {
         m_Jobs.Dequeue().AbortJob();
     }
     m_Jobs    = null;
     m_Active  = null;
     m_Threads = null;
 }
示例#14
0
        private void StopService(object sender, System.EventArgs e)
        {
            if (serviceView.SelectedIndices.Count > 0)
            {
                SelectService(ServiceControllerStatus.ContinuePending);

                Thread worker = new Thread(new ParameterizedThreadStart(StopServiceWorker));
                workers.Add(worker);
                ThreadItem item = new ThreadItem();
                item.Service = services.Values[serviceView.SelectedIndices[0]].Controller;
                item.Worker  = worker;
                worker.Start(item);

                serviceView.SelectedItems[0].ImageIndex = 1;

                serviceView.Focus();
                SelectService(ServiceControllerStatus.Stopped);
            }
        }
示例#15
0
 public void Send(SendOrPostCallback callback, object state)
 {
     if (ThreadID == Thread.CurrentThread.ManagedThreadId)
     {
         callback(state);
     }
     else
     {
         using (ManualResetEvent waitHandle = new ManualResetEvent(false))
         {
             lock (sync)
             {
                 ThreadItem oWorker = new ThreadItem(callback, state, waitHandle);
                 Items.Enqueue(oWorker);
             }
             waitHandle.WaitOne();
         }
     }
 }
示例#16
0
        private void StartThread(ThreadItem task)
        {
            _threads.Add(task);
            BackgroundWorker thread = new BackgroundWorker();

            thread.DoWork += delegate
            {
                task.Invoke();
            };
            thread.RunWorkerCompleted += delegate
            {
                _threads.Remove(task);

                CheckQueue();

                if (_queue.Count == 0 && _threads.Count == 0 && RaiseCompleteEventIfQueueEmpty)
                {
                    OnCompleted();
                }
            };
            thread.RunWorkerAsync();
        }
示例#17
0
        private System.Drawing.Color GetThreadColor(ThreadItem titem)
        {
            if (Settings.Instance.UseColorSuspended && titem.WaitReason == KWaitReason.Suspended)
                return Settings.Instance.ColorSuspended;
            else if (Settings.Instance.UseColorGuiThreads && titem.IsGuiThread)
                return Settings.Instance.ColorGuiThreads;

            return System.Drawing.SystemColors.Window;
        }
示例#18
0
        private System.Drawing.Color GetThreadColor(ThreadItem titem)
        {
            if (Properties.Settings.Default.UseColorSuspended && titem.WaitReason == KWaitReason.Suspended)
                return Properties.Settings.Default.ColorSuspended;
            else if (Properties.Settings.Default.UseColorGuiThreads && titem.IsGuiThread)
                return Properties.Settings.Default.ColorGuiThreads;

            return System.Drawing.SystemColors.Window;
        }
示例#19
0
 protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
 {
     _threaditems.Dispose();
     _threaditems = null;
 }
示例#20
0
        void CreateThreads()
        {
            for( int n = 1; ; n++ )
            {
                MapObjectAttachedObject startObject = GetAttachedObjectByAlias(
                    string.Format( "thread{0}Start", n ) );
                MapObjectAttachedObject endObject = GetAttachedObjectByAlias(
                    string.Format( "thread{0}End", n ) );
                if( startObject == null || endObject == null )
                    break;

                MeshObject meshObject = SceneManager.Instance.CreateMeshObject(
                    "Models\\DefaultBox\\DefaultBox.mesh" );
                if( meshObject == null )
                    break;

                meshObject.SetMaterialNameForAllSubObjects( "Black" );
                meshObject.CastShadows = true;

                ThreadItem item = new ThreadItem();
                item.startObject = startObject;
                item.endObject = endObject;
                item.meshObject = meshObject;
                item.sceneNode = new SceneNode();
                item.sceneNode.Attach( item.meshObject );

                MapObject.AssociateSceneNodeWithMapObject( item.sceneNode, this );

                threads.Add( item );
            }

            UpdateThreads();
        }
示例#21
0
 public static void Add(ThreadItem action)
 {
     WorkItems.Add(action);
 }
示例#22
0
        private void provider_DictionaryAdded(ThreadItem item)
        {
            HighlightedListViewItem litem = new HighlightedListViewItem(_highlightingContext,
                item.RunId > 0 && _runCount > 0);

            litem.Name = item.Tid.ToString();
            litem.Text = item.Tid.ToString();
            litem.SubItems.Add(new ListViewItem.ListViewSubItem(litem, ""));
            litem.SubItems.Add(new ListViewItem.ListViewSubItem(litem, item.StartAddress));
            litem.SubItems.Add(new ListViewItem.ListViewSubItem(litem, item.Priority));
            litem.Tag = item;
            litem.NormalColor = GetThreadColor(item);

            lock (_needsAdd)
                _needsAdd.Add(litem);
        }
示例#23
0
        private void provider_DictionaryModified(ThreadItem oldItem, ThreadItem newItem)
        {
            this.BeginInvoke(new MethodInvoker(() =>
                {
                    lock (listThreads)
                    {
                        ListViewItem litem = listThreads.Items[newItem.Tid.ToString()];

                        if (litem == null)
                            return;

                        if (!_useCycleTime)
                        {
                            if (newItem.ContextSwitchesDelta == 0)
                                litem.SubItems[1].Text = "";
                            else
                                litem.SubItems[1].Text = newItem.ContextSwitchesDelta.ToString("N0");
                        }
                        else
                        {
                            if (newItem.CyclesDelta == 0)
                                litem.SubItems[1].Text = "";
                            else
                                litem.SubItems[1].Text = newItem.CyclesDelta.ToString("N0");
                        }

                        litem.SubItems[2].Text = newItem.StartAddress;
                        litem.SubItems[3].Text = newItem.Priority;
                        litem.Tag = newItem;

                        (litem as HighlightedListViewItem).NormalColor = GetThreadColor(newItem);
                        _needsSort = true;
                    }
                }));
        }
示例#24
0
 private void provider_DictionaryRemoved(ThreadItem item)
 {
     this.BeginInvoke(new MethodInvoker(() =>
         {
             lock (listThreads)
             {
                 if (listThreads.Items.ContainsKey(item.Tid.ToString()))
                     listThreads.Items[item.Tid.ToString()].Remove();
             }
         }));
 }
示例#25
0
    public static void RunEvents()
    {
        lock (lck)//we do not want more than one thread to be querying the database at a time
        {
            ThreadPool.Clean();
            //figure out what needs to be run
            var events_to_run = db.EventsToRun();

            var current_message_id = "";
            Dictionary<string,string> values=null;

            //send to consumers
            foreach (DataRow dr in events_to_run.Rows)
            {
                string consumer_id = Convert.ToString(dr["consumer_id"]);
                string message_id = Convert.ToString(dr["message_id"]);
                if (current_message_id != message_id)
                {
                    values = db.getValues(message_id);
                    current_message_id = message_id;
                }
                var t = new ThreadItem();
                t.tag = consumer_id;

                //if the current consumer isn't busy, process all the events in this group
                //if (ThreadPool.GetByTag(consumer_id) == null)
                //{

                    t.thread = new Thread(() => TryCallConsumer(consumer_id, message_id, values));
                            ThreadPool.Items.Add(t);
                            t.thread.Start();
                //}
                //else
                //{
                    //if thread for this consumer is busy end; it will be handled later
                //}

            }
            Thread.Sleep(1000);//wait a second before querying the database again
        }
    }
示例#26
0
 public AgoraService(ThreadItem thread)
 {
     tr = thread;
 }