zoukankan      html  css  js  c++  java
  • Thread中带参方法无法使用之解决方案


    using System;
    using System.Collections.Generic;
    using System.Collections;
    using System.Threading;
    namespace ThreadHelperLib
    {
        public class ThreadHelper
        {
            public ThreadHelper()
            {

            }
            ~ThreadHelper() { }

            #region 私有变量
            /// <summary>
            /// 当前线程       
            /// </summary>
            private Thread thrWork = null;
            /// <summary>
            /// 参数集合
            /// </summary>
            private List<object> objParams = new List<object>();
            /// <summary>
            /// 方法
            /// </summary>
            private Delegate Method = null ;

            /// <summary>
            /// 线程结束标识
            /// </summary>
            private bool overFlag = false;

            private string Error = string.Empty;

            #endregion

            #region 公共方法

            /// <summary>
            /// 设置参数
            /// </summary>
            /// <param name="o"></param>
            public void SetParams(object o)
            {
                objParams.Add(o);
            }
            /// <summary>
            /// 设置方法
            /// </summary>
            /// <param name="pMethod"></param>
            public void SetMethod(Delegate pMethod)
            {
                Method = pMethod;
            }
            public bool StartThread()
            {
                try
                {
                    ThreadStart myThreadStart = new ThreadStart(this.ThreadProc);
                    thrWork = new Thread(myThreadStart);
                    thrWork.Start();
                    return true;
                }
                catch (ThreadStartException e)
                {
                    Error = e.ToString();
                    thrWork.Abort();
                    return false;
                }
            }
            public void EndStart()
            {
                if (this.overFlag)
                {
                    this.thrWork.Abort();
                }
            }
            /// <summary>
            /// 错误消息
            /// </summary>
            /// <returns></returns>
            public  string outError()
            {
                return this.Error;
            }
            #endregion

            #region 私有方法

          
            private void ThreadProc()
            {
                try
                {
                    System.Collections.IEnumerator m_Enum = objParams.GetEnumerator();
                    while (m_Enum.MoveNext())
                    {
                        Method.DynamicInvoke(m_Enum.Current);
                    }
                }
                catch (ThreadStateException err)
                {
                    this.Error = err.ToString();
                }
                finally
                {
                    this.overFlag = true;
                }
            }
            #endregion
        }

    }



    using System;
    using System.Collections.Generic;
    using System.Text;

    using ThreadHelperLib;
    namespace helloWorld
    {
      
        class Program
        {
           private delegate void TestCall(string str);
            static void Main()
            {
               string state = "123";
               for (int i = 0; i < 3000; i++)
               {
                   ThreadHelper thr = new ThreadHelper();
                   thr.SetMethod(new TestCall(test));
                   thr.SetParams(i.ToString());
                   thr.StartThread();
                   thr.EndStart();
               }
             

            
                Console.ReadLine();
               
            }
            private static void test(string state)
            {
                Console.WriteLine(state);
            }
          

            static void c_OnTestHandler()
            {
                Console.WriteLine("sadfasdfsadf");
            }
          
        }
    }

  • 相关阅读:
    jdbc代码
    openwrt vsftp
    openwrt 配置samba && ubuntu 配置samba
    如何学习开源项目
    Makefile 笔记
    Samba 学习笔记
    quilt-补丁工具
    to-do-list
    新增feeds模块
    linux命令
  • 原文地址:https://www.cnblogs.com/liping13599168/p/598140.html
Copyright © 2011-2022 走看看