读书人

哪位能帮忙看看多线程的类并帮忙修改

发布时间: 2012-04-05 12:42:40 作者: rapoo

哪位能帮忙看看多线程的类,并帮忙修改一下
需要用多线程执行一组任务,自己对这些十分不熟悉,努力拼凑的代码应该很不正确,请有时间的兄台帮忙给修改整理下,十分感谢
另:1、要执行的任务应该很快,但我弄的代码,总要等待很长时间才执行完
2、还有,某一个线程执行完一个任务后,怎么再取下一个任务来自动执行?

C# code
using System.Threading;using System.Collections;using System.Diagnostics;using System;using System.ComponentModel;using System.Collections.Generic;namespace TaskThreadClass.task{    public class taskThread    {        private int threadNums = 10;        //线程列表        public List<Thread> threadlist = new List<Thread>();        //任务列表        public List<task.taskModel> tasklist = new List<task.taskModel>();        // 创建一个委托,返回类型为void,两个参数        public delegate void changeState(object sender, threadEventArgs e);        // 将创建的委托和特定事件关联,在这里特定的事件为OnKeyDown        public event changeState onStateChange;        public taskThread()        {        }        /// <summary>        /// 前期准备,分配线程        /// </summary>        /// <returns></returns>        public void prepare(List<task.taskModel> tml)        {            this.tasklist = tml;        }        /// <summary>        /// 得到一个等待中得任务,并分配线程(分配的是线程序号)        /// </summary>        /// <returns></returns>        public task.taskModel getFreeTaskList(int thIndex)        {            Monitor.Enter(tasklist);            task.taskModel rtm = null;            for (int i = 0; i < tasklist.Count; i++)            {                if (tasklist[i].state == 0)                {                    rtm = tasklist[i];                    rtm.threadIndex = thIndex;                    rtm.state = 3;//1 任务完成,2任务失败,3标记为正在执行                    break;                }            }            this.onStateChange(this, new threadEventArgs(rtm));            Monitor.Exit(tasklist);            return rtm;        }                //执行任务        private void workfunc(object v)        {            //线程序号            int thindex = (int)v;            taskModel tm = getFreeTaskList(thindex);            if (tm == null)            {                threadlist[thindex].Abort();            }            else            {                this.SyncRemoteWeb(tm);            }        }        /// <summary>        /// 线程开始入口        /// </summary>        public void SyncRemoteWebInit()        {            this.enterThreadList();        }        /// <summary>        /// 初始化 开启 threadNums个线程        /// </summary>        /// <param name="tasksssss"></param>        public void enterThreadList()        {            for (int i = 0; i < threadNums; i++)            {                getNewThread(i,true);            }        }        public void getNewThread(int thindex)        {            getNewThread(thindex,false);        }        //创建一个新线程 执行新任务        //thindex 线程序号,新的线程占用了原来线程的位置        public void getNewThread(int thindex,bool isinit){            Thread th = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(workfunc));            th.Name = "线程" + thindex.ToString();            th.IsBackground = true;            th.Start(thindex);            if (isinit)            {                threadlist.Add(th);            }            else            {                threadlist[thindex] = th;            }        }        //返回任务列表        public List<taskModel> getTaskList()        {            return this.tasklist;        }            /// <summary>        /// 任务处理,具体方法未实现,暂时直接设置了 任务实例的 状态        /// </summary>        /// <param name="tms"></param>        public void SyncRemoteWeb(task.taskModel tms)        {            bool result = doSomeThing();            //失败            if (result == false)            {                tms.state = 2;//标记为失败            }            else            {                tms.state = 1;            }                        //给界面发消息,标记任务状态            this.onStateChange(this, new threadEventArgs(tms));            //执行下一个任务(对吗,这样做)            getNewThread(tms.threadIndex);        }        //取消所有线程        public void closeAllThread()        {            foreach (Thread th in threadlist)            {                th.Abort();            }        }    }}


[解决办法]
http://blog.163.com/zhb123@126/blog/static/62515850201062714121186/
[解决办法]
任务用队列,要加锁
[解决办法]
不要用tasklist来做锁,另外建一个对象 private static object lock = new object();
然后Monitor对lock操作。
[解决办法]
用并行库:
tasklist.AsParallel().WithDegreeOfParallelism(10).ForAll(tms => SyncRemoteWeb(tms));

读书人网 >C#

热点推荐