當前位置: 首頁>>代碼示例>>C#>>正文


C# Threading.Thread類代碼示例

本文整理匯總了C#中System.Threading.Thread的典型用法代碼示例。如果您正苦於以下問題:C# Thread類的具體用法?C# Thread怎麽用?C# Thread使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


Thread類屬於System.Threading命名空間,在下文中一共展示了Thread類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Initialize

		// Initialize Parallel class's instance creating required number of threads
		// and synchronization objects
		private void Initialize( )
		{
			threadsCount = System.Environment.ProcessorCount;
			
			//No point starting new threads for a single core computer
			if (threadsCount <= 1) {
				return;
			}
			
			// array of events, which signal about available job
			jobAvailable = new AutoResetEvent[threadsCount];
			// array of events, which signal about available thread
			threadIdle = new ManualResetEvent[threadsCount];
			// array of threads
			threads = new Thread[threadsCount];
		
			for ( int i = 0; i < threadsCount; i++ )
			{
				jobAvailable[i] = new AutoResetEvent( false );
				threadIdle[i]   = new ManualResetEvent( true );
		
				threads[i] = new Thread( new ParameterizedThreadStart( WorkerThread ) );
				threads[i].IsBackground = false;
				threads[i].Start( i );
			}
		}
開發者ID:JustSAT,項目名稱:Tower-Defence,代碼行數:28,代碼來源:AstarParallel.cs

示例2: Main

        static void Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.WriteLine("Usage:");
                Console.WriteLine("'extractor.exe output_folder input.xef' to extract a .xef file.");
                return;
            }

            string myPhotos = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
            string subFolderPath = System.IO.Path.Combine(myPhotos, args[0]);
            Directory.CreateDirectory(subFolderPath);

            using (Extractor extractor = new Extractor(subFolderPath))
            {
                extractor.ListenForFrames();

                string xefFileName = args[1];
                Console.WriteLine("Extracting frames from .xef file: {0}", xefFileName);

                Player player = new Player(xefFileName);
                Thread playerThread;
                playerThread = new System.Threading.Thread(new ThreadStart(player.PlayXef));
                playerThread.Start();
                playerThread.Join();

                extractor.Stop();
            }
        }
開發者ID:isalento,項目名稱:XefFrameExtractor,代碼行數:29,代碼來源: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: 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

示例5: SetClipboard

 public static void SetClipboard(string result)
 {
     var thread = new Thread(() => Clipboard.SetText(result));
     thread.SetApartmentState(ApartmentState.STA);
     thread.Start();
     thread.Join();
 }
開發者ID:gitter-badger,項目名稱:GitReleaseManager,代碼行數:7,代碼來源:ClipBoardHelper.cs

示例6: SetUp

        public void SetUp()
        {
            runner = new TestRunner(x => x.AddFixture<SlowFixture>());
            test = new Test("slow test").With(Section.For<SlowFixture>().WithStep("GoSlow"));

            var reset = new ManualResetEvent(false);
            var running = new ManualResetEvent(false);

            var thread = new Thread(() =>
            {
                running.Set();
                Debug.WriteLine("Starting to run");
                test.LastResult = runner.RunTest(new TestExecutionRequest()
                {
                    Test = test,
                    TimeoutInSeconds = 60
                });

                test.LastResult.ShouldNotBeNull();
                Debug.WriteLine("finished running");
                reset.Set();
            });

            thread.Start();
            running.WaitOne();
            Thread.Sleep(1000);

            Debug.WriteLine("Aborting now!");
            runner.Abort();
            Debug.WriteLine("Done aborting");

            reset.WaitOne(5000);
            test.LastResult.ShouldNotBeNull();
            Debug.WriteLine("completely done");
        }
開發者ID:wbinford,項目名稱:storyteller,代碼行數:35,代碼來源:AbortTestSmokeTester.cs

示例7: StartService

 public static void StartService()
 {
     Thread thread = new Thread(OrderProcessingService.Run);
     thread.Name = "Order Processing Thread";
     thread.IsBackground = true;
     thread.Start();
 }
開發者ID:michaellperry,項目名稱:DuraCore,代碼行數:7,代碼來源:ServiceConfig.cs

示例8: RelayPort

 public RelayPort(Cpu.Pin pin, bool initialState, bool glitchFilter, Port.ResistorMode resistor, int timeout)
     : base(pin, initialState, glitchFilter, resistor)
 {
     currentstate = initialState;
     relayThread = new Thread(new ThreadStart(RelayLoop));
     relayThread.Start();
 }
開發者ID:mbaldini,項目名稱:NovaOS,代碼行數:7,代碼來源:RelayPort.cs

示例9: Service

 public Service()
 {
     InitializeComponent();
     _server = new ConnectsterServer();
     var job = new ThreadStart(_server.Start);
     _thread = new Thread(job);
 }
開發者ID:shopster,項目名稱:NconnectSter,代碼行數:7,代碼來源:Service.cs

示例10: mainForm

 //UI Init and Main Thread Creation
 public mainForm()
 {
     InitializeComponent();
     AcceptButton = buttonSend;
     tmain = new System.Threading.Thread(main);
     tmain.Start();
 }
開發者ID:Foltik,項目名稱:SugoiRC,代碼行數:8,代碼來源:mainForm.cs

示例11: SetupPrimaryDisplayForm

        /// <summary>
        /// Setups the primary display form.
        /// </summary>
        public static void SetupPrimaryDisplayForm()
        {
            PrimaryDisplayForm = new DisplayForm(800, 600);

            Thread thread = new Thread(new ThreadStart(CreatePrimaryDisplayForm));
            thread.Start();
        }
開發者ID:GeroL,項目名稱:MOSA-Project,代碼行數:10,代碼來源:Setup.cs

示例12: Start

 public void Start()
 {
     Stop();
     m_bThreadStop = false;
     m_thread = new System.Threading.Thread(Main);
     m_thread.Start();
 }
開發者ID:arsaccol,項目名稱:SLED,代碼行數:7,代碼來源:TaskQueue.cs

示例13: 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

示例14: setupRequestResponseBackgroundThead

 private void setupRequestResponseBackgroundThead()
 {
     requestResponseThread = new Thread(new ThreadStart(RequestResponseBackground_Thread));
     requestResponseThread.IsBackground = true;
     multicastRequestThread = new Thread(new ThreadStart(MulticastRequestBackgroud_Thread));
     multicastRequestThread.IsBackground = true;
 }
開發者ID:bdr27,項目名稱:c-,代碼行數:7,代碼來源:App.xaml.cs

示例15: RunInNewThread

 [System.Security.SecurityCritical]  // auto-generated
 private int RunInNewThread () {
     Thread th = new Thread(new ThreadStart(NewThreadRunner));
     th.SetApartmentState(m_apt);
     th.Start();
     th.Join();
     return m_runResult;
 }
開發者ID:nlh774,項目名稱:DotNetReferenceSource,代碼行數:8,代碼來源:ApplicationActivator.cs


注:本文中的System.Threading.Thread類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。