当前位置: 首页>>代码示例>>C#>>正文


C# Thread.Join方法代码示例

本文整理汇总了C#中System.Threading.Thread.Join方法的典型用法代码示例。如果您正苦于以下问题:C# Thread.Join方法的具体用法?C# Thread.Join怎么用?C# Thread.Join使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Threading.Thread的用法示例。


在下文中一共展示了Thread.Join方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: DifferentThreads_DifferentObjects_RegisterInterfaceWithDependencyPropertyAndDependencyMethod_Success

        public void DifferentThreads_DifferentObjects_RegisterInterfaceWithDependencyPropertyAndDependencyMethod_Success()
        {
            var c = new Container();
            c.RegisterType<IEmptyClass, EmptyClass>().AsPerThread();
            c.RegisterType<ISampleClassWithInterfaceDependencyPropertyAndDependencyMethodWithSameType, SampleClassWithInterfaceDependencyPropertyAndDependencyMethodWithSameType>();
            ISampleClassWithInterfaceDependencyPropertyAndDependencyMethodWithSameType sampleClass1 = null;
            ISampleClassWithInterfaceDependencyPropertyAndDependencyMethodWithSameType sampleClass2 = null;

            var thread1 = new Thread(() => { sampleClass1 = c.Resolve<ISampleClassWithInterfaceDependencyPropertyAndDependencyMethodWithSameType>(ResolveKind.PartialEmitFunction); });
            thread1.Start();
            thread1.Join();
            var thread2 = new Thread(() => { sampleClass2 = c.Resolve<ISampleClassWithInterfaceDependencyPropertyAndDependencyMethodWithSameType>(ResolveKind.PartialEmitFunction); });
            thread2.Start();
            thread2.Join();

            Assert.IsNotNull(sampleClass1.EmptyClassFromDependencyProperty);
            Assert.IsNotNull(sampleClass1.EmptyClassFromDependencyMethod);
            Assert.AreEqual(sampleClass1.EmptyClassFromDependencyProperty, sampleClass1.EmptyClassFromDependencyMethod);
            Assert.IsNotNull(sampleClass2.EmptyClassFromDependencyProperty);
            Assert.IsNotNull(sampleClass2.EmptyClassFromDependencyMethod);
            Assert.AreEqual(sampleClass2.EmptyClassFromDependencyProperty, sampleClass2.EmptyClassFromDependencyMethod);
            Assert.AreNotEqual(sampleClass1, sampleClass2);
            Assert.AreNotEqual(sampleClass1.EmptyClassFromDependencyProperty, sampleClass2.EmptyClassFromDependencyProperty);
            Assert.AreNotEqual(sampleClass1.EmptyClassFromDependencyMethod, sampleClass2.EmptyClassFromDependencyMethod);
        }
开发者ID:amularczyk,项目名称:NiquIoC,代码行数:25,代码来源:RegisterInterfaceWithDependencyPropertyAndDependencyMethodTests.cs

示例2: Main

        private static void Main(string[] args)
        {
            var n = 10;

            var chickenFarm = new ChickenFarm();
            var token = chickenFarm.GetToken();
            var chickenFarmer = new Thread(chickenFarm.FarmSomeChickens) {Name = "TheChickenFarmer"};
            var chickenStore = new Retailer(token);
            chickenFarm.PriceCut += chickenStore.OnPriceCut;
            var retailerThreads = new Thread[n];
            for (var index = 0; index < retailerThreads.Length; index++)
            {
                retailerThreads[index] = new Thread(chickenStore.RunStore) {Name = "Retailer" + (index + 1)};
                retailerThreads[index].Start();
                while (!retailerThreads[index].IsAlive)
                {
                    ;
                }
            }
            chickenFarmer.Start();
            chickenFarmer.Join();
            foreach (var retailerThread in retailerThreads)
            {
                retailerThread.Join();
            }
        }
开发者ID:asu-cse445-cornholios,项目名称:Project2,代码行数:26,代码来源:Program.cs

示例3: MultiThread

        public void MultiThread()
        {
            int threadCount = 5;
             _threadedMessageCount = 100;
             int totalMessageCount = threadCount * _threadedMessageCount;

             List<Thread> threads = new List<Thread>();
             for (int thread = 0; thread < threadCount; thread++)
             {
            Thread t = new Thread(new ThreadStart(SendMessageThread));

            threads.Add(t);

            t.Start();
             }

             foreach (Thread t in threads)
             {
            t.Join();
             }

             POP3ClientSimulator.AssertMessageCount(_account.Address, "test", totalMessageCount);

             for (int i = 0; i < totalMessageCount; i++)
             {
            string content = POP3ClientSimulator.AssertGetFirstMessageText(_account.Address, "test");

            Assert.IsTrue(content.Contains("X-Spam-Status"), content);
             }
        }
开发者ID:digitalsoft,项目名称:hmailserver,代码行数:30,代码来源:SpamAssassin.cs

示例4: Start_WhenExecutedInMultipleThreads_ShouldBeThreadSafeAndNotExecuteSameTaskTwice

        public void Start_WhenExecutedInMultipleThreads_ShouldBeThreadSafeAndNotExecuteSameTaskTwice()
        {
            const string resultId = "result/1";

            Enumerable.Range(1, 100)
                .ToList()
                .ForEach(i =>
                         	{
                         		CreateRaceConditionTask(resultId);
                         		var thread1 = new Thread(() =>
                         		                         	{
                         		                         		var taskExecutor =
                         		                         			MasterResolve<ITaskExecutor>();
                         		                         		taskExecutor.Start();
                         		                         	});
                         		var thread2 = new Thread(() =>
                         		                         	{
                         		                         		var taskExecutor =
                         		                         			MasterResolve<ITaskExecutor>();
                         		                         		taskExecutor.Start();
                         		                         	});

                         		thread1.Start();
                         		thread2.Start();

                         		thread1.Join();
                         		thread2.Join();
                         	});

            var result = Store.Load<ComputationResult<int>>(resultId);

            result.Result.Should().Be(100);
        }
开发者ID:mamluka,项目名称:SpeedyMailer,代码行数:33,代码来源:TaskExecutorTests.cs

示例5: ClassReRegisteredFromClassToObjectFactory_Success

        public void ClassReRegisteredFromClassToObjectFactory_Success()
        {
            var c = new Container();
            c.RegisterType<EmptyClass>().AsPerThread();
            EmptyClass emptyClass1 = null;
            EmptyClass emptyClass2 = null;
            EmptyClass emptyClass3 = null;
            EmptyClass emptyClass4 = null;

            var thread = new Thread(() =>
            {
                emptyClass1 = c.Resolve<EmptyClass>(ResolveKind.PartialEmitFunction);
                emptyClass2 = c.Resolve<EmptyClass>(ResolveKind.PartialEmitFunction);

                c.RegisterType<EmptyClass>(() => new EmptyClass()).AsPerThread();
                emptyClass3 = c.Resolve<EmptyClass>(ResolveKind.PartialEmitFunction);
                emptyClass4 = c.Resolve<EmptyClass>(ResolveKind.PartialEmitFunction);
            });
            thread.Start();
            thread.Join();

            Assert.AreEqual(emptyClass1, emptyClass2);
            Assert.AreEqual(emptyClass3, emptyClass4);
            Assert.AreNotEqual(emptyClass1, emptyClass3);
        }
开发者ID:amularczyk,项目名称:NiquIoC,代码行数:25,代码来源:ReRegistereClassTests.cs

示例6: CreateShaderProgramSequential

        public void CreateShaderProgramSequential()
        {
            using (var thread0Window = Device.CreateWindow(1, 1))
            using (var thread1Window = Device.CreateWindow(1, 1))
            using (var window = Device.CreateWindow(1, 1))
            using (ShaderProgramFactory factory0 = new ShaderProgramFactory(thread0Window.Context, ShaderSources.PassThroughVertexShader(), ShaderSources.PassThroughFragmentShader()))
            using (ShaderProgramFactory factory1 = new ShaderProgramFactory(thread1Window.Context, ShaderSources.PassThroughVertexShader(), ShaderSources.PassThroughFragmentShader()))
            {
                Thread t0 = new Thread(factory0.Create);
                t0.Start();
                t0.Join();

                Thread t1 = new Thread(factory1.Create);
                t1.Start();
                t1.Join();

                using (Framebuffer framebuffer = TestUtility.CreateFramebuffer(window.Context))
                using (VertexArray va = TestUtility.CreateVertexArray(window.Context, factory0.ShaderProgram.VertexAttributes["position"].Location))
                {
                    window.Context.Framebuffer = framebuffer;
                    window.Context.Draw(PrimitiveType.Points, 0, 1, new DrawState(TestUtility.CreateRenderStateWithoutDepthTest(), factory0.ShaderProgram, va), new SceneState());
                    TestUtility.ValidateColor(framebuffer.ColorAttachments[0], 255, 0, 0);

                    window.Context.Clear(new ClearState());
                    window.Context.Draw(PrimitiveType.Points, 0, 1, new DrawState(TestUtility.CreateRenderStateWithoutDepthTest(), factory1.ShaderProgram, va), new SceneState());
                    TestUtility.ValidateColor(framebuffer.ColorAttachments[0], 255, 0, 0);
                }
            }
        }
开发者ID:jpespartero,项目名称:OpenGlobe,代码行数:29,代码来源:ShaderProgramMultiThreadingTests.cs

示例7: InterfaceReRegisteredFromClassToInstance_Success

        public void InterfaceReRegisteredFromClassToInstance_Success()
        {
            var c = new Container();
            IEmptyClass emptyClass = new EmptyClass();
            c.RegisterType<IEmptyClass, EmptyClass>().AsPerThread();
            IEmptyClass emptyClass1 = null;
            IEmptyClass emptyClass2 = null;
            IEmptyClass emptyClass3 = null;
            IEmptyClass emptyClass4 = null;

            var thread = new Thread(() =>
            {
                emptyClass1 = c.Resolve<IEmptyClass>(ResolveKind.FullEmitFunction);
                emptyClass2 = c.Resolve<IEmptyClass>(ResolveKind.FullEmitFunction);

                c.RegisterInstance(emptyClass).AsPerThread();
                emptyClass3 = c.Resolve<IEmptyClass>(ResolveKind.FullEmitFunction);
                emptyClass4 = c.Resolve<IEmptyClass>(ResolveKind.FullEmitFunction);
            });
            thread.Start();
            thread.Join();

            Assert.AreEqual(emptyClass1, emptyClass2);
            Assert.AreEqual(emptyClass, emptyClass3);
            Assert.AreEqual(emptyClass3, emptyClass4);
            Assert.AreNotEqual(emptyClass1, emptyClass3);
        }
开发者ID:amularczyk,项目名称:NiquIoC,代码行数:27,代码来源:ReRegistereInterfaceTests.cs

示例8: Container

        public void DifferentThreads_DifferentObjects_RegisterClassWithDependencyPropertyAndDependencyMethodWithDifferentTypes_Success()
        {
            var c = new Container();
            c.RegisterType<EmptyClass>().AsPerThread();
            c.RegisterType<SampleClass>().AsPerThread();
            c.RegisterType<SampleClassWithClassDependencyPropertyAndDependencyMethodWithDifferentTypes>();
            SampleClassWithClassDependencyPropertyAndDependencyMethodWithDifferentTypes sampleClass1 = null;
            SampleClassWithClassDependencyPropertyAndDependencyMethodWithDifferentTypes sampleClass2 = null;

            var thread1 = new Thread(() => { sampleClass1 = c.Resolve<SampleClassWithClassDependencyPropertyAndDependencyMethodWithDifferentTypes>(ResolveKind.PartialEmitFunction); });
            thread1.Start();
            thread1.Join();
            var thread2 = new Thread(() => { sampleClass2 = c.Resolve<SampleClassWithClassDependencyPropertyAndDependencyMethodWithDifferentTypes>(ResolveKind.PartialEmitFunction); });
            thread2.Start();
            thread2.Join();

            Assert.IsNotNull(sampleClass1.SampleClass);
            Assert.IsNotNull(sampleClass1.EmptyClass);
            Assert.AreNotEqual(sampleClass1.SampleClass, sampleClass1.EmptyClass);
            Assert.AreEqual(sampleClass1.SampleClass.EmptyClass, sampleClass1.EmptyClass);
            Assert.IsNotNull(sampleClass2.SampleClass);
            Assert.IsNotNull(sampleClass2.EmptyClass);
            Assert.AreNotEqual(sampleClass2.SampleClass, sampleClass2.EmptyClass);
            Assert.AreEqual(sampleClass2.SampleClass.EmptyClass, sampleClass2.EmptyClass);
            Assert.AreNotEqual(sampleClass1, sampleClass2);
            Assert.AreNotEqual(sampleClass1.SampleClass, sampleClass2.SampleClass);
            Assert.AreNotEqual(sampleClass1.EmptyClass, sampleClass2.EmptyClass);
        }
开发者ID:amularczyk,项目名称:NiquIoC,代码行数:28,代码来源:RegisterClassWithDependencyPropertyAndDependencyMethodTests.cs

示例9: QuickOpen

        /// <summary>
        /// Quickly open a sqlconnection and if it doesn't respond in the timeout time, throw a ConnectionError with the errorMessage
        /// </summary>
        /// <param name="conn">The sqlconnection that you are about to connect to</param>
        /// <param name="timeout">The timeout time in ms</param>
        /// <param name="errorMessage">The errormessage that the ConnectionException will have</param>
        public static void QuickOpen(this SqlConnection conn, int timeout = 5000, string errorMessage = "Timed out while trying to connect.")
        {
            // We'll use a Stopwatch here for simplicity. A comparison to a stored DateTime.Now value could also be used
            Stopwatch sw = new Stopwatch();
            bool connectSuccess = false;

            // Try to open the connection, if anything goes wrong, make sure we set connectSuccess = false
            Thread t = new Thread(delegate()
            {
                try
                {
                    sw.Start();
                    conn.Open();
                    connectSuccess = true;
                }
                catch { }
            });

            // Make sure it's marked as a background thread so it'll get cleaned up automatically
            t.IsBackground = true;
            t.Start();

            // Keep trying to join the thread until we either succeed or the timeout value has been exceeded
            while (timeout > sw.ElapsedMilliseconds)
                if (t.Join(1))
                    break;

            // If we didn't connect successfully, throw an exception
            if (!connectSuccess)
                throw new ConnectionException(errorMessage);
        }
开发者ID:CCHolmgren,项目名称:vt14-3-1-individuellt-arbete,代码行数:37,代码来源:SqlExtensions.cs

示例10: Program

 static Program()
 {
     // Запускаем инициализацию в отдельном потоке
     var thread = new Thread(Initialize);
     thread.Start();
     thread.Join();
 }
开发者ID:bazile,项目名称:Training,代码行数:7,代码来源:Program.cs

示例11: PerformanceCounterTest

        public void PerformanceCounterTest()
        {
            var stop = false;
            var t = new Thread(
                () =>
                    {
                        while (!stop)
                        {
                        }
                    });
            t.Start();
            for (int i = 0; i < 60; i++)
            {
                if ((i+1) % 10 == 0)
                {
                    stop = !stop;
                }

                var value = 0;// PerformanceMonitor.GetProcessorTimeCounterValue();
                Debug.WriteLine(value);
            }

            stop = true;
            t.Join();
        }
开发者ID:pbazydlo,项目名称:NetReduce,代码行数:25,代码来源:PerformanceMonitorTests.cs

示例12: ShouldRemoveExecutionContextInstanceOnly

        public void ShouldRemoveExecutionContextInstanceOnly()
        {
            var instance1 = new TestClass();
            var instance2 = new TestClass();

            var container = new Container();
            container.Inject<TestInterface>(instance1, Lifecycle.HttpContextOrExecutionContextLocal);

            TestInterface[] thread2ResolvedTestClasses = null;
            bool thread2HasRegistration = true;
            ExecutionContext.SuppressFlow();
            var thread2 = new Thread(() =>
            {
                container.Inject<TestInterface>(instance2, Lifecycle.HttpContextOrExecutionContextLocal);

                thread2ResolvedTestClasses = container.ResolveAll<TestInterface>().ToArray();

                container.RemoveInstancesOf<TestInterface>(Lifecycle.HttpContextOrExecutionContextLocal);

                thread2HasRegistration = container.HasRegistrationFor<TestInterface>();
            });

            thread2.Start();
            thread2.Join(1000);
            ExecutionContext.RestoreFlow();
            Assert.IsFalse(thread2HasRegistration);
            Assert.AreEqual(1, thread2ResolvedTestClasses.Length);

            Assert.AreEqual(instance1, container.Resolve<TestInterface>());
        }
开发者ID:trullock,项目名称:NanoIoC,代码行数:30,代码来源:RemovingInstances.cs

示例13: Execute

        public void Execute()
        {
            //
            // .NET 4.0より、Threadクラスに以下のメソッドが追加された。
            //   ・Yieldメソッド
            //
            // Yieldメソッドは、別のスレッドにタイムスライスを引き渡す為のメソッド。
            // 今までは、Thread.Sleepを利用したりして、タイムスライスを切り替えるよう
            // にしていたが、今後はこのメソッドを利用することが推奨される。
            //
            // 戻り値は、タイムスライスの引き渡しが成功したか否かが返ってくる。
            //

            //
            // テスト用にスレッドを2つ起動する.
            //
            var t1 = new Thread(ThreadProc);
            var t2 = new Thread(ThreadProc);

            t1.Start("T1");
            t2.Start("T2");

            t1.Join();
            t2.Join();
        }
开发者ID:devlights,项目名称:Sazare,代码行数:25,代码来源:ThreadingNamespaceSamples04.cs

示例14: DifferentThreads_DifferentObjects_RegisterClassWithDependencyPropertyAndDependencyMethod_Success

        public void DifferentThreads_DifferentObjects_RegisterClassWithDependencyPropertyAndDependencyMethod_Success()
        {
            var c = new Container();
            c.RegisterType<EmptyClass>().AsPerThread();
            var sampleClass1 = new SampleClassWithClassDependencyPropertyAndDependencyMethodWithSameType();
            var sampleClass2 = new SampleClassWithClassDependencyPropertyAndDependencyMethodWithSameType();

            var thread1 = new Thread(() =>
            {
                c.BuildUp(sampleClass1, ResolveKind.PartialEmitFunction);
            });
            thread1.Start();
            thread1.Join();
            var thread2 = new Thread(() =>
            {
                c.BuildUp(sampleClass2, ResolveKind.PartialEmitFunction);
            });
            thread2.Start();
            thread2.Join();

            Assert.IsNotNull(sampleClass1.EmptyClassFromDependencyProperty);
            Assert.IsNotNull(sampleClass1.EmptyClassFromDependencyMethod);
            Assert.AreEqual(sampleClass1.EmptyClassFromDependencyProperty, sampleClass1.EmptyClassFromDependencyMethod);
            Assert.IsNotNull(sampleClass2.EmptyClassFromDependencyProperty);
            Assert.IsNotNull(sampleClass2.EmptyClassFromDependencyMethod);
            Assert.AreEqual(sampleClass2.EmptyClassFromDependencyProperty, sampleClass2.EmptyClassFromDependencyMethod);
            Assert.AreNotEqual(sampleClass1, sampleClass2);
            Assert.AreNotEqual(sampleClass1.EmptyClassFromDependencyProperty, sampleClass2.EmptyClassFromDependencyMethod);
            Assert.AreNotEqual(sampleClass1.EmptyClassFromDependencyMethod, sampleClass2.EmptyClassFromDependencyMethod);
        }
开发者ID:amularczyk,项目名称:NiquIoC,代码行数:30,代码来源:BuildUpForClassWithDependencyPropertyAndDependencyMethodTests.cs

示例15: Process

        public TypedData Process(TypedData data, IMutableProgressTracker progress, CancellationToken cancelToken)
        {
            progress.Status = "Taking a crop shot";

            CropShotWindow cropShotWindow = null;

            var thread = new Thread(() => {
                cropShotWindow = new CropShotWindow {
                    SourceImage = (Image) data.Data,
                    DataName = data.Name
                };

                cropShotWindow.ShowDialog();
            });

            // UI objects want to be in STA; make sure we're in STA
            thread.SetApartmentState(ApartmentState.STA);

            thread.Start();
            thread.Join();

            if (cropShotWindow.Data == null) {
                throw new CommandCanceledException(this);
            }

            progress.Progress = 1;

            return cropShotWindow.Data;
        }
开发者ID:strager,项目名称:NoCap,代码行数:29,代码来源:CropShot.cs


注:本文中的System.Threading.Thread.Join方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。