/// <summary> /// 创建线程 /// </summary> /// <param name="name"></param> /// <param name="task"></param> /// <param name="isBackground"></param> /// <param name="apartmentState"></param> public SmartThread(Action task, string name = null, bool isBackground = true, ApartmentState apartmentState = ApartmentState.MTA) { if (task == null) throw new ArgumentNullException("task"); InnerThread = new System.Threading.Thread(()=>task()) { Name = name, IsBackground = isBackground }; InnerThread.SetApartmentState(apartmentState); mutex = new ResetEvent(false); State = ThreadState.NotStarted; }
/// <summary> /// 创建线程 /// </summary> /// <param name="name"></param> /// <param name="task"></param> /// <param name="isBackground"></param> /// <param name="apartmentState"></param> public SmartThread(Action task, string name = null, bool isBackground = true, ApartmentState apartmentState = ApartmentState.MTA) { if (task == null) { throw new ArgumentNullException("task"); } InnerThread = new System.Threading.Thread(() => task()) { Name = name, IsBackground = isBackground }; InnerThread.SetApartmentState(apartmentState); mutex = new ResetEvent(false); State = ThreadState.NotStarted; }
public void ThreadLifestyleTest() { ServiceRegistry.Current.Register<IPerson, Person>("person", LifestyleFlags.Thread); IPerson person = null, person2 = null, person3 = null, person4 = null; PopulatePerThreadLifestyle(ref person, ref person2); var mre = new ResetEvent(false); ThreadPool.QueueUserWorkItem((s) => { PopulatePerThreadLifestyle(ref person3, ref person4); mre.Set(); }); mre.Wait(); Assert.AreSame(person, person2); Assert.AreSame(person3, person4); Assert.AreNotSame(person, person3); }
public void GenericThreadLifestyleTest() { ServiceRegistry.Current.Register(new ComponentInfo(null, typeof(IList<>), typeof(List<>), LifestyleFlags.Thread)); IList<int> coll = null, coll2 = null, coll3 = null, coll4 = null; PopulatePerThreadLifestyle(ref coll, ref coll2); var mre = new ResetEvent(false); ThreadPool.QueueUserWorkItem((s) => { PopulatePerThreadLifestyle(ref coll3, ref coll4); mre.Set(); }); mre.Wait(); Assert.AreSame(coll, coll2); Assert.AreSame(coll3, coll4); Assert.AreNotSame(coll, coll3); }