コード例 #1
0
        public void BasicChecks()
        {
            ThreadLocalVariable <int> threadVar = new ThreadLocalVariable <int>(123);
            Thread parent = Thread.CurrentThread;
            bool   eventOccurred = false;
            bool   valueOk = true, eventOk = true;
            bool   stop    = false;
            bool   started = false;

            ThreadEx t = new ThreadEx(delegate(object o)
            {
                started = true;
                try
                {
                    if ((int)o != 123 || threadVar.Value != 123)
                    {
                        valueOk = false;
                    }
                }
                catch
                {
                    valueOk = false;
                }
                while (!stop)
                {
                    GC.KeepAlive("");                     // Waste time
                }
                started = false;
            });

            EventHandler <ThreadStartEventArgs> eh = null;

            ThreadEx.ThreadStarting += (eh = delegate(object o, ThreadStartEventArgs e)
            {
                eventOccurred = true;
                if (e.ChildThread != t || e.ParentThread != parent)
                {
                    eventOk = false;
                }
                ThreadEx.ThreadStarting -= eh;
            });

            Assert.IsFalse(t.IsAlive);
            Assert.AreEqual(System.Threading.ThreadState.Unstarted, t.ThreadState);
            t.Start(123);
            Assert.IsTrue(t.IsAlive);
            Assert.IsTrue(eventOccurred);
            Assert.IsTrue(eventOk);
            while (!started)
            {
                ThreadEx.Sleep(0);
            }
            Assert.AreEqual(System.Threading.ThreadState.Running, t.ThreadState);
            stop = true;
            Assert.IsTrue(t.Join(5000));
            Assert.IsTrue(valueOk);
            Assert.IsFalse(started);
        }
コード例 #2
0
ファイル: ThreadEx.cs プロジェクト: dadhi/ecsharp
 /// <summary>Constructs a ThreadLocalVariable.</summary>
 /// <param name="initialValue">Initial value on the current thread.
 /// Does not affect other threads that are already running.</param>
 /// <param name="fallbackValue">Value to use when a given thread
 /// doesn't have an associated value.</param>
 /// <param name="propagator">A function that copies (and possibly
 /// modifies) the Value from a parent thread when starting a new
 /// thread.</param>
 public ThreadLocalVariable(T initialValue, T fallbackValue, Func <T, T> propagator)
 {
     _fallbackValue = fallbackValue;
     Value          = initialValue;
     if (propagator != null)
     {
         _propagator = propagator;
     }
     ThreadEx.RegisterTLV(this);
 }
コード例 #3
0
ファイル: ThreadEx.cs プロジェクト: dadhi/ecsharp
 public ThreadLocalVariable()
 {
     ThreadEx.RegisterTLV(this);
 }
コード例 #4
0
ファイル: ThreadEx.cs プロジェクト: dadhi/ecsharp
 public ThreadStartEventArgs(Thread parent, ThreadEx child)
 {
     ParentThread = parent; ChildThread = child;
 }