Esempio n. 1
0
        private static uint StartThread(IntPtr parameter)
        {
            GCHandle      threadHandle = (GCHandle)parameter;
            RuntimeThread thread       = (RuntimeThread)threadHandle.Target;
            Delegate      threadStart  = thread._threadStart;
            // Get the value before clearing the ThreadState.Unstarted bit
            object threadStartArg = thread._threadStartArg;

            try
            {
                t_currentThread = thread;
                System.Threading.ManagedThreadId.SetForCurrentThread(thread._managedThreadId);
            }
            catch (OutOfMemoryException)
            {
#if PLATFORM_UNIX
                // This should go away once OnThreadExit stops using t_currentThread to signal
                // shutdown of the thread on Unix.
                thread._stopped.Set();
#endif
                // Terminate the current thread. The creator thread will throw a ThreadStartException.
                return(0);
            }

            // Report success to the creator thread, which will free threadHandle and _threadStartArg
            thread.ClearThreadStateBit(ThreadState.Unstarted);

            try
            {
                // The Thread cannot be started more than once, so we may clean up the delegate
                thread._threadStart = null;

#if ENABLE_WINRT
                // If this call fails, COM and WinRT calls on this thread will fail with CO_E_NOTINITIALIZED.
                // We may continue and fail on the actual call.
                Interop.WinRT.RoInitialize(Interop.WinRT.RO_INIT_TYPE.RO_INIT_MULTITHREADED);
#endif

                ParameterizedThreadStart paramThreadStart = threadStart as ParameterizedThreadStart;
                if (paramThreadStart != null)
                {
                    paramThreadStart(threadStartArg);
                }
                else
                {
                    ((ThreadStart)threadStart)();
                }
            }
            finally
            {
                thread.SetThreadStateBit(ThreadState.Stopped);
            }
            return(0);
        }
Esempio n. 2
0
        private static void StartThread(IntPtr parameter)
        {
            GCHandle      threadHandle = (GCHandle)parameter;
            RuntimeThread thread       = (RuntimeThread)threadHandle.Target;
            Delegate      threadStart  = thread._threadStart;
            // Get the value before clearing the ThreadState.Unstarted bit
            object threadStartArg = thread._threadStartArg;

            try
            {
                t_currentThread = thread;
                System.Threading.ManagedThreadId.SetForCurrentThread(thread._managedThreadId);
                thread.InitializeComOnNewThread();
            }
            catch (OutOfMemoryException)
            {
#if PLATFORM_UNIX
                // This should go away once OnThreadExit stops using t_currentThread to signal
                // shutdown of the thread on Unix.
                thread._stopped.Set();
#endif
                // Terminate the current thread. The creator thread will throw a ThreadStartException.
                return;
            }

            // Report success to the creator thread, which will free threadHandle and _threadStartArg
            thread.ClearThreadStateBit(ThreadState.Unstarted);

            try
            {
                // The Thread cannot be started more than once, so we may clean up the delegate
                thread._threadStart = null;

                ParameterizedThreadStart paramThreadStart = threadStart as ParameterizedThreadStart;
                if (paramThreadStart != null)
                {
                    paramThreadStart(threadStartArg);
                }
                else
                {
                    ((ThreadStart)threadStart)();
                }
            }
            finally
            {
                thread.SetThreadStateBit(ThreadState.Stopped);
            }
        }
Esempio n. 3
0
        private static uint StartThread(IntPtr parameter)
        {
            GCHandle      threadHandle = (GCHandle)parameter;
            RuntimeThread thread       = (RuntimeThread)threadHandle.Target;

            // TODO: OOM hardening
            t_currentThread = thread;
            System.Threading.ManagedThreadId.SetForCurrentThread(thread._managedThreadId);
            threadHandle.Free();

            thread.ClearThreadStateBit(ThreadState.Unstarted);

            try
            {
                Delegate threadStart    = thread._threadStart;
                object   threadStartArg = thread._threadStartArg;
                thread._threadStart    = null;
                thread._threadStartArg = null;

#if ENABLE_WINRT
                Interop.WinRT.RoInitialize(Interop.WinRT.RO_INIT_TYPE.RO_INIT_MULTITHREADED);
#endif

                ParameterizedThreadStart paramThreadStart = threadStart as ParameterizedThreadStart;
                if (paramThreadStart != null)
                {
                    paramThreadStart(threadStartArg);
                }
                else
                {
                    ((ThreadStart)threadStart)();
                }
            }
            finally
            {
                thread.SetThreadStateBit(ThreadState.Stopped);
            }
            return(0);
        }