本文整理汇总了C#中Thread.Join方法的典型用法代码示例。如果您正苦于以下问题:C# Thread.Join方法的具体用法?C# Thread.Join怎么用?C# Thread.Join使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Thread
的用法示例。
在下文中一共展示了Thread.Join方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Execute
public void Execute(string[] args)
{
Options options = new Options(args);
int threadsCount = options.ThreadsCount > 0
? options.ThreadsCount
: Environment.ProcessorCount;
_loopsPerThread = options.MegaLoops * 1000000L;
if (threadsCount == 1)
{
Burn();
}
else
{
_loopsPerThread /= threadsCount;
_gateEvent = new ManualResetEvent(false);
Thread[] threads = new Thread[threadsCount];
for (int i = 0; i < threadsCount; i++)
{
var thread = new Thread(Burn);
thread.IsBackground = true;
thread.Start();
threads[i] = thread;
}
_gateEvent.Set();
foreach (var thread in threads)
thread.Join();
}
}
示例2: Main
static void Main(string[] args)
{
int [] mas = {5,15};
ThreadManipulator Manipulator = new ThreadManipulator();
Thread Thread_AddingOne1 = new Thread(Manipulator.AddingOne);
Thread Thread_AddingOne2 = new Thread(Manipulator.AddingOne);
Thread Thread_AddingCustomValue = new Thread(Manipulator.AddingCustomValue);
Thread Thread_Stop = new Thread(Manipulator.Stop);
Thread_Stop.IsBackground = true;
Console.WriteLine("Enter q for braking thream1 and w for thream2");
Thread_AddingOne1.Start(10);
Thread_AddingOne2.Start(20);
Thread_AddingCustomValue.Start(mas);
Thread_Stop.Start();
Thread_AddingOne1.Join();
Thread_AddingOne2.Join();
Thread_AddingCustomValue.Join();
Thread_Stop.Join();
Console.ReadKey();
}
示例3: Main
public static int Main()
{
Thread
t1
=
new
Thread(new
ThreadStart
(MultiThreadExceptionTest.ThreadStart1));
t1.Name
=
"Thread 1";
Thread.Sleep
(100);
t1.Start();
Thread.Sleep
(200);
t1.Abort
("STATETEST");
t1.Join
();
Console.WriteLine
("Result: "
+
result);
if
(result
!=
27)
return
1;
return
0;
}
示例4: TestConcurrentQueueDeclare
public void TestConcurrentQueueDeclare()
{
string x = GenerateExchangeName();
Random rnd = new Random();
List<Thread> ts = new List<Thread>();
System.NotSupportedException nse = null;
for(int i = 0; i < 256; i++)
{
Thread t = new Thread(() =>
{
try
{
// sleep for a random amount of time to increase the chances
// of thread interleaving. MK.
Thread.Sleep(rnd.Next(5, 500));
Model.ExchangeDeclare(x, "fanout", false, false, null);
} catch (System.NotSupportedException e)
{
nse = e;
}
});
ts.Add(t);
t.Start();
}
foreach (Thread t in ts)
{
t.Join();
}
Assert.IsNotNull(nse);
Model.ExchangeDelete(x);
}
示例5: UpdateDatabaseThreaded
/*
* Reads all INFATI data and fills out all possible fields in the database
*/
static void UpdateDatabaseThreaded()
{
List<Worker> workerPool = new List<Worker>();
for (Int16 i = 1; i <= 1; i++) {
workerPool.Add(new Worker(1, i));
}
/*
for (Int16 i = 12; i <= 20; i++) {
workerPool.Add(new Worker(2, i));
}
*/
List<Thread> threads = new List<Thread>();
foreach (Worker worker in workerPool) {
Thread thread = new Thread(new ThreadStart(worker.Start));
thread.Start();
threads.Add(thread);
}
foreach (var thread in threads) {
thread.Join();
}
Console.WriteLine("All threads ended");
}
示例6: RunWithTimeout
static void RunWithTimeout(Action entryPoint, int timeout)
{
Thread thread = null;
try
{
thread = new Thread(() => entryPoint()) { IsBackground = true };
if (thread != null)
{
thread.Start();
if(thread.IsAlive)
thread.Join(timeout);
}
}
catch (Exception ex) { ddbs.WriteErrorMessage(ddbs.ConnectionString, 0, null, "ошибка запуска задания " + ex.Message,-1); }
finally
{
if (thread != null)
thread.Abort();
if (thread != null)
thread.Join(3000);
if (thread != null)
thread.Interrupt();
if (thread != null)
GC.SuppressFinalize(thread);
};
thread = null;
}
示例7: FolderFinishedHandler
public override void FolderFinishedHandler(Item item)
{
try
{
m_thread = new Thread(this.RetreiveData);
m_thread.Name = "Tree Thread";
m_thread.Start();
m_thread.Join();
m_tree.Dispatcher.Invoke(() =>
{
if (m_itemsStack.Peek().Header.ToString() == item.Name)
{
m_itemsStack.Pop();
}
else
{
throw new Exception(string.Format("invalid element structure {0} != {1}", m_itemsStack.Peek(), item.Id));
}
});
}
catch (Exception exc)
{
m_thread.Abort();
m_thread.Join();
OnExceptionOccured(exc.Message);
}
}
示例8: Main
static void Main()
{
var clock = new Thread(() =>
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
Thread.Sleep(500);
}
}) { IsBackground = true };
// --- BACKGROUND THREAD EXPLANATION ---
// Case 1 -- START
clock.Start();
// Result: It never prints anything, i.e. Background threads can't keep a
// process alive until it exits.
// Case 1 -- END
// Case 2 -- START
clock.Start();
Thread.Sleep(2000);
// Result: Prints four times, i.e. 0\n1\n2\n3
// Case 2 -- END
// Case 3 -- START
clock.Start();
clock.Join();
// Result: Prints all values, because the foreground thread (in Main method)
// waits for the end of the execution of the clock thread with a help of
// 'Join' method
// Case 3 -- END
// --- BACKGROUND THREAD EXPLANATION END ---
// --- FOREGROUND THREAD EXPLANATION ---
clock.IsBackground = false;
// Case 1 -- START
clock.Start();
// Result: Prints all values, because the foreground thread can keep a
// process alive until it exits. All new threads are foreground by default
// Case 1 -- END
// Case 2 -- START
clock.Start();
Thread.Sleep(2000);
// Result: Prints all values.
// Case 2 -- END
// Case 3 -- START
clock.Start();
clock.Join();
// Result: Prints all values.
// Case 3 -- END
// --- FOREGROUND THREAD EXPLANATION END ---
}
示例9: should_wait_for_completion
public void should_wait_for_completion()
{
var thread = new Thread(() => _defaultCompletionCallback.WaitForCompletion());
thread.Start();
Assert.False(thread.Join(300));
_defaultCompletionCallback.ExecuteCallback(new CompletionAcknowledgementMessage(Guid.NewGuid(),"test", true, null));
Assert.IsTrue(thread.Join(300));
}
示例10: AttemptToTerminate
/// <summary>
/// Attempts to terminate the passed in thread
/// </summary>
/// <param name="workerThread">The worker thread.</param>
/// <param name="timeout">The timeout.</param>
/// <returns></returns>
public bool AttemptToTerminate(Thread workerThread, TimeSpan? timeout)
{
if (workerThread == null || !workerThread.IsAlive)
return true; //if the thread is null or not alive, its terminated
if (timeout.HasValue)
{
return workerThread.Join(timeout.Value);
}
workerThread.Join();
return true;
}
示例11: CabinetMultithread
public void CabinetMultithread()
{
this.multithreadExceptions = new List<Exception>();
const int threadCount = 10;
IList<Thread> threads = new List<Thread>(threadCount);
for (int i = 0; i < threadCount; i++)
{
Thread thread = new Thread(new ThreadStart(this.CabinetMultithreadWorker));
thread.Name = "CabinetMultithreadWorker_" + i;
threads.Add(thread);
}
foreach (Thread thread in threads)
{
thread.Start();
}
foreach (Thread thread in threads)
{
thread.Join();
}
foreach (Exception ex in this.multithreadExceptions)
{
Console.WriteLine();
Console.WriteLine(ex);
}
Assert.AreEqual<int>(0, this.multithreadExceptions.Count);
}
示例12: rellenaArrays_Click
private void rellenaArrays_Click(object sender, EventArgs e)
{
Thread t1 = new Thread(rellena);
Thread t2 = new Thread(rellena);
Thread t3 = new Thread(rellena);
Thread t4 = new Thread(rellena);
Thread t5 = new Thread(rellena);
t1.Start();
t2.Start();
t3.Start();
t4.Start();
t5.Start();
//Thread.Sleep(10000);
//Esperar fin ejecución de todos los threads
t1.Join();
t2.Join();
t3.Join();
t4.Join();
t5.Join();
//ver array
for (int i = 0; i < 1000; i++)
{
Console.WriteLine(i + ".- " + numeros[i]);
}
}
示例13: btnArrays_Click
private void btnArrays_Click(object sender, EventArgs e)
{
Thread t1 = new Thread(rellenaArr1);
Thread t2 = new Thread(rellenaArr2);
t1.Start();
t2.Start();
t1.Join();
t2.Join();
Thread t3 = new Thread(circularArray);
t3.Priority = ThreadPriority.AboveNormal;
t3.Start();
poolThread[0] = new Thread(intercambiarImpar);
poolThread[1] = new Thread(intercambiarImpar);
poolThread[2] = new Thread(intercambiarImpar);
poolThread[0].Start();
poolThread[1].Start();
poolThread[2].Start();
}
示例14: AllocationOfConnectedDomains_recursively
// Выделение связных областей рекурсивным алгоритмом
unsafe private void AllocationOfConnectedDomains_recursively()
{
int maxStackSize = 100000000;
Bitmap tmp = bitmap.Clone(new Rectangle(0, 0, bitmap.Width, bitmap.Height), bitmap.PixelFormat);
Thread t = new Thread(() =>
{
UnsafeBitmap src = new UnsafeBitmap(tmp);
Byte* pBaseSrc = src.LockBitmap();
int L = 1;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
FillLabels(pBaseSrc, x, y, L++);
}
}
src.UnlockBitmap();
}, maxStackSize);
t.Start();
t.Join();
objectsCount = RenumberDomains();
}
示例15: ShouldCallExceptionHandlerOnUncaughtException
public void ShouldCallExceptionHandlerOnUncaughtException()
{
var ex = new Exception();
var exceptionHandlerMock = new Mock<IExceptionHandler<object>>();
_batchEventProcessor.SetExceptionHandler(exceptionHandlerMock.Object);
_batchHandlerMock.Setup(bh => bh.OnEvent(_ringBuffer[0], 0, true))
.Throws(ex); // OnNext raises an expcetion
exceptionHandlerMock.Setup(bh => bh.HandleEventException(ex, 0, _ringBuffer[0]))
.Callback(() => _countDownEvent.Signal()); // Exception should be handled here and signal the CDE
var thread = new Thread(_batchEventProcessor.Run);
thread.Start();
_ringBuffer.Publish(_ringBuffer.Next());
_countDownEvent.Wait(50);
_batchEventProcessor.Halt();
thread.Join();
_batchHandlerMock.VerifyAll();
exceptionHandlerMock.VerifyAll();
}