Exemple #1
0
        //********************************************************************************************************************

        //线程和窗口进程之间的交互平台(线程与进程安全空间)
        public void worker_Report(Object dataObj)
        {
            //获取线程Report来的数据
            Dictionary <String, Object> argument = dataObj as Dictionary <String, Object>;
            //ILightThreadable me = argument["caller"] as ILightThreadable;//调用者指针
            BackgroundWorker currWorker  = argument["worker"] as BackgroundWorker;
            LightThread      lightThread = argument["LightThread"] as LightThread;
            ILightThreadable caller      = argument["caller"] as ILightThreadable; //调用者指针
            //数据
            Socket socket  = argument["socket"] as Socket;                         //特别传送数据
            String strInfo = argument["str"] as String;                            //特别传送数据

            //调用窗口的其他处理事项
            //对调用窗口的变量和方法的访问和窗口的其他函数一样(线程安全)
            //分析字符串
            CXml xmldoc = new CXml();
            Dictionary <String, String> map = xmldoc.parseStringXml(strInfo);

            if (null == map)
            {
                return;
            }
            if (map.Count <= 0)
            {
                return;
            }

            String strSentence = String.Format("请 {0} 到{1}检查", map["name"], map["room"]);
            //Call TTS to Speak
        }
Exemple #2
0
 public LightThread(ILightThreadable caller, int intervalTime)
 {
     master = caller;
     if (intervalTime > 0)
     {
         intervalMillisecod = intervalTime;
     }
 }
Exemple #3
0
        //线程结束后调用
        public void worker_RunWorkCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //获取启动参数,获得master,worker,this等实例
            Dictionary <String, Object> argument = e.Result as Dictionary <String, Object>;

            BackgroundWorker currWorker = argument["worker"] as BackgroundWorker;
            LightThread      THIS       = argument["LightThread"] as LightThread;
            ILightThreadable caller     = argument["caller"] as ILightThreadable;
            int nReturn = Convert.ToInt16(argument["result"].ToString());

            //调用chaunkou的对应函数,做一些资源释放工作
            //e.Result可以在线程中设置为Object,可以存放多种变量
            caller.worker_Completed(argument);
        }
Exemple #4
0
        //线程结束后调用
        public void worker_Completed(Object dataObj)
        {
            //获取线程Report来的数据
            Dictionary <String, Object> argument = dataObj as Dictionary <String, Object>;
            //ILightThreadable me = argument["caller"] as ILightThreadable;//调用者指针
            BackgroundWorker currWorker  = argument["worker"] as BackgroundWorker;
            LightThread      lightThread = argument["LightThread"] as LightThread;
            ILightThreadable caller      = argument["caller"] as ILightThreadable;//调用者指针
            //结果
            int nReturn = Convert.ToInt16(argument["result"].ToString());

            //做一些资源释放工作
            //e.Result可以在线程中设置为Object,可以存放多种变量
            //MessageBox.Show(String.Format("thread 返回:{0},执行次数:{1}", nReturn, lightThread.loopCount));
            return;
        }
Exemple #5
0
        //namespace引入
        //using GeneralCode;//引入LightThread(自定义库)
        //using System.ComponentModel;//引入BackgroundWorker
        //using System.Threading;

        //线程执行的主体函数(线程空间,不可以直接访问调用者的类变量等,如需要在e.argument中有调用者指针)
        public Object worker_main(Object e)
        {
            //获取启动参数,获得master,worker,this等实例
            Dictionary <String, Object> argument   = e as Dictionary <String, Object>;
            BackgroundWorker            currWorker = argument["worker"] as BackgroundWorker;
            LightThread      lightThread           = argument["LightThread"] as LightThread;
            ILightThreadable caller = argument["caller"] as ILightThreadable;//调用者指针
            //MyClass me = argument["master"] as MyClass;//调用者指针,转换后可访问本进程变量

            //通过LightThread.callerKey获取Socket
            Socket socket = lightThread.callerKey as Socket;

            //处理需要时间和繁杂的任务,不会阻塞前台窗口程序
            //此处为线程空间,尽量避免直接修改其他进程空间的数据
            //do some background work
            //1.接收信息
            String strRecv = "";

            while (strRecv.Length <= 0)
            {
                Thread.Sleep(100);
                strRecv = receive(socket);
            }

            //构建Report参数
            Dictionary <String, Object> map = new Dictionary <string, Object>();

            map.Add("worker", currWorker);
            map.Add("LightThread", lightThread);
            map.Add("caller", caller);

            map.Add("socket", socket);//返回给Report的数据
            map.Add("str", strRecv);

            return(map);//提供给LightThread主线程发送Report
        }
Exemple #6
0
        //线程循环函数
        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            //对线程执行体进行整体的TRY-CATCH封装
            try
            {
                //获取启动参数,获得master,worker,this等实例
                Dictionary <String, Object> argument = e.Argument as Dictionary <String, Object>;

                BackgroundWorker currWorker = argument["worker"] as BackgroundWorker;
                LightThread      THIS       = argument["LightThread"] as LightThread;
                ILightThreadable caller     = argument["caller"] as ILightThreadable;

                while (true)
                {
                    loopCount++;//循环计数
                    if (loopCount > 60000)
                    {
                        loopCount = 0;                   //reset
                    }
                    //执行线程主函数时线程同步
                    //每次循环调用一次caller的线程主体函数
                    //窗口主体函数运行后返回需要Report的数据做参数
                    object result = null;
                    result = caller.worker_main(argument);

                    //报告进度
                    THIS.n_lock = 1;//Lock
                    //System.Windows.Forms.MessageBox.Show("worker_DoWork:线程空间");//不会阻塞主界面
                    currWorker.ReportProgress(loopCount, result);
                    Thread.Sleep(100);//暂停100ms


                    //如果ReportProcess需要等待,则在此死循环
                    while (THIS.n_lock > 0)
                    {
                        Thread.Sleep(100);//停止100ms
                    }

                    //设置每次查询的间隔时间
                    Thread.Sleep(THIS.intervalMillisecod);

                    //判断是否需要终止执行
                    //判断是否窗口发出终止执行命令
                    if (currWorker.CancellationPending)
                    {
                        //线程返回值
                        //e.Result为Object类型,可以构建复杂参数作为返回值
                        //其值将在worker_RunWorkCompleted()
                        //中被访问
                        argument.Add("result", 1);
                        e.Result = argument;
                        return;//终止执行
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Dictionary <String, Object> argument = e.Argument as Dictionary <String, Object>;
                argument.Add("result", -1);//非常规结束,返回-1
                e.Result = argument;

                return;//终止执行
            }
        }
Exemple #7
0
 //构造函数
 public LightThread(ILightThreadable caller)
 {
     master = caller;
 }