本文整理汇总了C#中Thread.Interrupt方法的典型用法代码示例。如果您正苦于以下问题:C# Thread.Interrupt方法的具体用法?C# Thread.Interrupt怎么用?C# Thread.Interrupt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Thread
的用法示例。
在下文中一共展示了Thread.Interrupt方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static int Main ()
{
lock (_lock2) {
Thread t1;
lock (_lock1) {
t1 = new Thread (new ThreadStart(T1));
t1.Start ();
Monitor.Wait (_lock1);
}
Thread.Sleep (100);
_actions.Add ("Main: interrupting T1");
t1.Interrupt ();
}
#if NET_2_0
if (_actions.Count != 3) {
#else
if (_actions.Count != 3) {
#endif
Console.WriteLine ("#1: " + _actions.Count);
return 1;
}
if ((string) _actions [0] != "T1: started") {
Console.WriteLine ("#2: " + (string) _actions [0]);
return 2;
}
if ((string) _actions [1] != "T1: trying Lock1") {
Console.WriteLine ("#3: " + (string) _actions [1]);
return 3;
}
if ((string) _actions [2] != "Main: interrupting T1") {
Console.WriteLine ("#4: " + (string) _actions [2]);
return 4;
}
return 0;
}
static void T1 ()
{
_actions.Add ("T1: started");
lock (_lock1) {
Monitor.Pulse (_lock1);
}
try {
_actions.Add ("T1: trying Lock1");
lock (_lock2) {
_actions.Add ("T1: got Lock1");
}
} catch (Exception) {
_actions.Add ("T1: interrupted");
}
}
}
示例2: TestFairness
private static bool TestFairness()
{
const int RUN_TIME = 15000;
const int SETUP_TIME = 20;
const int READERS = 50;
const int WRITERS = 10;
Thread[] rdthrs = new Thread[READERS];
Thread[] wrthrs = new Thread[WRITERS];
int[] readersCounters = new int[READERS];
int[] writersCounters = new int[WRITERS];
int[] interruptCounters = new int[READERS + WRITERS];
int sentInterrupts = 0;
ManualResetEventSlim startEvent = new ManualResetEventSlim(false);
ReadersWriters_ rwLock = new ReadersWriters_();
for (int i = 0; i < READERS; i++) {
int tid = i;
rdthrs[i] = new Thread(() => {
// Wait the start for all threads
startEvent.Wait();
int endTime = Environment.TickCount + RUN_TIME;
do {
do {
try {
rwLock.StartRead();
break;
} catch (ThreadInterruptedException) {
interruptCounters[tid]++;
}
} while (true);
Thread.Yield();
rwLock.EndRead();
if ((++readersCounters[tid] % 1000) == 0) {
Console.Write("[#r{0}]", tid);
}
} while (Environment.TickCount < endTime);
try {
Thread.Sleep(0);
} catch (ThreadInterruptedException) {
interruptCounters[tid]++;
}
});
rdthrs[i].Start();
}
for (int i = 0; i < WRITERS; i++) {
int tid = i;
wrthrs[i] = new Thread(() => {
// Wait the start for all threads
startEvent.Wait();
int endTime = Environment.TickCount + RUN_TIME;
do {
do {
try {
rwLock.StartWrite();
break;
} catch (ThreadInterruptedException) {
interruptCounters[tid + READERS]++;
}
} while (true);
Thread.Yield();
rwLock.EndWrite();
if ((++writersCounters[tid] % 1000) == 0) {
Console.Write("[#w{0}]", tid);
}
} while (Environment.TickCount < endTime);
try {
Thread.Sleep(0);
} catch (ThreadInterruptedException) {
interruptCounters[tid + READERS]++;
}
});
wrthrs[i].Start();
}
// Create the and start the bomber thread...
Thread bomber = new Thread(() => {
const int MIN_TIME = 5;
const int MAX_TIME = 50;
Random rnd = new Random(Environment.TickCount);
// Wait the start for all threads
startEvent.Wait();
do {
try {
Thread.Sleep(rnd.Next(MIN_TIME, MAX_TIME));
} catch (ThreadInterruptedException) {
break;
}
int target = rnd.Next(0, READERS + WRITERS);
if (target < READERS) {
//.........这里部分代码省略.........
示例3: Main
static void Main()
{
// First, set the name of the current thread so that it's easier
// to view in the debugger.
Thread mainThread = Thread.CurrentThread;
mainThread.Name = "Main Thread";
// Create an instance of the ThreadedClass.
ThreadedClass tc1 = new ThreadedClass("Thread1");
// Set the ThreadedClass's boolean wait forever flag to false.
tc1.WaitIndefinitely = false;
// Create the thread giving it the delegate with the
// method that should be called when the thread starts.
Console.WriteLine("Main - Creating first worker thread.");
Thread t1 = new Thread(tc1.ThreadMethod);
t1.Name = "First Worker Thread";
// Start the thread
t1.Start();
Console.WriteLine ("Main - First worker thread started.");
// Sleep for a few seconds.
Console.WriteLine("Main - Sleeping for 5 seconds.");
Thread.Sleep (5000);
// Start another thread
ThreadedClass tc2 = new ThreadedClass("Thread2");
tc2.WaitIndefinitely = true;
Console.WriteLine ("Main - Creating second worker thread.");
Thread t2 = new Thread(tc2.ThreadMethod);
t2.Name = "Second Worker Thread";
t2.Start();
Console.WriteLine ("Main - Second worker thread started.");
// Sleep for a few seconds.
Console.WriteLine("Main - Sleeping for 5 seconds.");
Thread.Sleep(5000);
// Now wake the second worker thread.
Console.WriteLine("Main - Interrupting Thread2.");
t2.Interrupt();
// Sleep for a few more seconds to give the thread time to finish up.
Console.WriteLine("Main - Sleeping for 10 seconds.");
Thread.Sleep(10000);
Console.Write ("Main - Press <ENTER> to end: ");
Console.ReadLine();
}
示例4: testMonitorInterruptSleep
public String testMonitorInterruptSleep()
{
Thread thread;
lock (this)
{
thread = new Thread(new ThreadStart(threadFunc));
thread.Start();
Monitor.Wait(this);
}
thread.Interrupt();
this.seen = true;
Thread.MemoryBarrier();
thread.Join();
return this.result;
}
示例5: testMonitorInterruptDuringSleep
public String testMonitorInterruptDuringSleep()
{
Thread thread;
thread = new Thread(new ThreadStart(threadFunc));
thread.Start();
while ((thread.ThreadState & ThreadState.WaitSleepJoin) == 0)
continue;
thread.Interrupt();
thread.Join();
return this.result;
}
示例6: testMonitorInterruptDuringWait
public String testMonitorInterruptDuringWait()
{
Thread thread;
lock (this)
{
thread = new Thread(new ThreadStart(threadFunc));
thread.Start();
this.seen = false;
Monitor.Wait(this);
}
lock (this.o)
{
thread.Interrupt();
Thread.Sleep(800);
this.seen = true;
}
thread.Join();
return this.result;
}