线程扩展
1 public class ThreadExtend 2 { 3 public ThreadExtend() 4 { 5 } 6 7 /// <summary> 8 /// 线程对象 9 /// </summary> 10 protected System.Threading.Thread Th; 11 12 13 public System.Threading.Thread Thread { 14 get { return Th; } } 15 16 /// <summary> 17 /// 开始线程 执行委托 18 /// </summary> 19 /// <typeparam name="T1"></typeparam> 20 /// <typeparam name="T2"></typeparam> 21 /// <typeparam name="T3"></typeparam> 22 /// <typeparam name="T4"></typeparam> 23 /// <param name="start"></param> 24 /// <param name="args"></param> 25 /// <returns></returns> 26 public ThreadExtend Start<T1, T2, T3, T4>(Func<T1, T2, T3, T4, object> start, params object[] args) 27 { 28 Th = new System.Threading.Thread(() => 29 { 30 Result = start( 31 args.Length > 0 ? (T1)args[0] : default(T1), 32 args.Length > 1 ? (T2)args[1] : default(T2), 33 args.Length > 2 ? (T3)args[2] : default(T3), 34 args.Length > 3 ? (T4)args[3] : default(T4)); 35 }) { IsBackground = true }; 36 //开启线程 37 Th.Start(); 38 39 return this; 40 } 41 42 43 44 45 /// <summary> 46 /// 开启线程 47 /// </summary> 48 public static ThreadExtend CreateFunc<T1, T2, T3, T4>(Func<T1, T2, T3, T4, object> start, params object[] args) 49 { 50 return new ThreadExtend().Start(start, args); 51 } 52 53 /// <summary> 54 /// 开启线程 55 /// </summary> 56 public static ThreadExtend CreateFunc<T1, T2, T3>(Func<T1, T2, T3, object> start, params object[] args) 57 { 58 Func<T1, T2, T3, object, object> st = (a, b, c, d) => start(a, b, c); 59 return new ThreadExtend().Start(st, args); 60 } 61 62 /// <summary> 63 /// 开启线程 64 /// </summary> 65 public static ThreadExtend CreateFunc<T1, T2>(Func<T1, T2, object> start, params object[] args) 66 { 67 Func<T1, T2, object, object, object> st = (a, b, c, d) => start(a, b); 68 return new ThreadExtend().Start(st, args); 69 } 70 71 /// <summary> 72 /// 开启线程 73 /// </summary> 74 public static ThreadExtend CreateFunc<T1>(Func<T1, object> start, params object[] args) 75 { 76 Func<T1, object, object, object, object> st = (a, b, c, d) => start(a); 77 return new ThreadExtend().Start(st, args); 78 } 79 80 /// <summary> 81 /// 开启线程 82 /// </summary> 83 public static ThreadExtend CreateFunc(Func<object> start) 84 { 85 Func<object, object, object, object, object> st = (a, b, c, d) => start(); 86 return new ThreadExtend().Start(st, new object[0]); 87 } 88 89 /// <summary> 90 /// 开启线程 91 /// </summary> 92 public static ThreadExtend CreateAction<T1, T2, T3, T4>(Action<T1, T2, T3, T4> start, params object[] args) 93 { 94 Func<T1, T2, T3, T4, object> st = (a, b, c, d) => { start(a, b, c, d); return null; }; 95 return new ThreadExtend().Start(st, args); 96 } 97 98 /// <summary> 99 /// 开启线程 100 /// </summary> 101 public static ThreadExtend CreateAction<T1, T2, T3>(Action<T1, T2, T3> start, params object[] args) 102 { 103 Func<T1, T2, T3, object, object> st = (a, b, c, d) => { start(a, b, c); return null; }; 104 return new ThreadExtend().Start(st, args); 105 } 106 107 /// <summary> 108 /// 开启线程 109 /// </summary> 110 public static ThreadExtend CreateAction<T1, T2>(Action<T1, T2> start, params object[] args) 111 { 112 Func<T1, T2, object, object, object> st = (a, b, c, d) => { start(a, b); return null; }; 113 return new ThreadExtend().Start(st, args); 114 } 115 116 /// <summary> 117 /// 开启线程 118 /// </summary> 119 public static ThreadExtend CreateAction<T1>(Action<T1> start, params object[] args) 120 { 121 Func<T1, object, object, object, object> st = (a, b, c, d) => { start(a); return null; }; 122 return new ThreadExtend().Start(st, args); 123 } 124 125 /// <summary> 126 /// 开启线程 j=>... 127 /// </summary> 128 public static ThreadExtend CreateAction(Action start) 129 { 130 Func<object, object, object, object, object> st = (a, b, c, d) => { start(); return null; }; 131 return new ThreadExtend().Start(st, new object[0]); 132 } 133 134 /// <summary> 135 /// 开启线程 j=>... 136 /// </summary> 137 public static ThreadExtend Create<T>(Action<T> start, T arg = default(T)) 138 { 139 return CreateAction(start, arg); 140 //Func<T, object, object, object, object> st = (a, b, c, d) => { start(arg); return null; }; 141 //return new ThreadExtend().Start(st, new object[0]); 142 } 143 144 /// <summary> 145 /// 开启线程 j=>... 146 /// </summary> 147 public static ThreadExtend Create(Action start) 148 { 149 return CreateAction(start); 150 //Func<object, object, object, object, object> st = (a, b, c, d) => { start(); return null; }; 151 //return new ThreadEx().Start(st, new object[0]); 152 } 153 154 /// <summary> 强行终止线程 155 /// </summary> 156 /// <param name="timeout">等待时间</param> 157 public void Abort(int timeout = int.MaxValue) 158 { 159 if (timeout > 0) 160 { 161 if (!Wait(timeout)) 162 { 163 Th.Abort(); 164 } 165 } 166 else 167 { 168 throw "Timeout 必须 > 0".ToException(); 169 } 170 } 171 172 /// <summary> 173 /// 等待指定时间 返回线程是否正常运行结束. 174 /// </summary> 175 /// <param name="timeout"> </param> 176 /// <returns></returns> 177 public bool Wait(int timeout = int.MaxValue) 178 { 179 return Th.Join(timeout); 180 } 181 182 /// <summary> 183 /// 返回结果 184 /// </summary> 185 public object Result { get; set; } 186 187 /// <summary> 188 /// 当前线程执行状态 189 /// </summary> 190 public bool IsAlive 191 { 192 get { return Th.IsAlive; } 193 } 194 }