本文整理匯總了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();
}
}
示例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);
}
}
示例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);
}
示例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);
}
示例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);
}
}
}
示例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);
}
示例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);
}
示例10: Program
static Program()
{
// Запускаем инициализацию в отдельном потоке
var thread = new Thread(Initialize);
thread.Start();
thread.Join();
}
示例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();
}
示例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>());
}
示例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();
}
示例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;
}