using
System;
using
System.Collections;
using
System.Threading;
public
class
Test
{
public
static
void
Main()
{
Test obj =
new
Test();
Console.WriteLine(Thread.CurrentThread.ManagedThreadId.ToString());
Thread thread =
new
Thread(
new
ThreadStart(obj.Method1));
thread.Start();
System.Timers.Timer t =
new
System.Timers.Timer(100);
t.Elapsed +=
new
System.Timers.ElapsedEventHandler(obj.Method2);
t.AutoReset =
true
;
t.Enabled =
true
;
while
(
true
)
{
Console.WriteLine(
"test_"
+ Thread.CurrentThread.ManagedThreadId.ToString());
Thread.Sleep(100);
}
System.Threading.Timer threadTimer =
new
System.Threading.Timer(
new
System.Threading.TimerCallback(obj.Method3),
null
, 0, 100);
while
(
true
)
{
Console.WriteLine(
"test_"
+ Thread.CurrentThread.ManagedThreadId.ToString());
Thread.Sleep(100);
}
Console.ReadLine();
}
void
Method1()
{
while
(
true
)
{
Console.WriteLine(DateTime.Now.ToString() +
"_"
+ Thread.CurrentThread.ManagedThreadId.ToString());
Thread.CurrentThread.Join(100);
}
}
void
Method2(
object
source, System.Timers.ElapsedEventArgs e)
{
Console.WriteLine(DateTime.Now.ToString() +
"_"
+ Thread.CurrentThread.ManagedThreadId.ToString());
}
void
Method3(Object state)
{
Console.WriteLine(DateTime.Now.ToString() +
"_"
+ Thread.CurrentThread.ManagedThreadId.ToString());
}
}
请发表评论