/// <summary> /// 获取监听接口实例 /// </summary> /// <returns>返回实例对象</returns> public IThreadListen CreateThreadListen() { lock (obj) { if (threadListens.Any()) { return(threadListens.FirstOrDefault().Value); } if (isInit) { return(null); } try { isInit = true; Type threadListenType = typeof(IThreadListen); var types = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(a => a.GetTypes().Where(t => t.GetInterfaces().Contains(threadListenType))).ToArray(); if (types != null && types.Count() > 0) { foreach (var item in types) { IThreadListen threadListen = (IThreadListen)Activator.CreateInstance(item); threadListens.Add(item.FullName, threadListen); return(threadListen); } } } catch { isInit = false; } return(null); } }
public ThreadExtend(int index) { locks = new AutoResetEvent(false); work = true; Key = Guid.NewGuid().ToString() + index;//线程唯一标识 //初始化线程 thread_obj = new Thread(Work); thread_obj.IsBackground = true; thread_obj.Start(); //初始化监听对象 threadListen = ThreadListenProvider.Current.CreateThreadListen(); }