using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ThreadEag
{
class Program
{
static void Main(string[] args)
{
//调用线程方法
ThreadPro();
//让控制台停窗体停下来,便于观察
Console.ReadKey();
}
//主方法,包括开启线程方法
public static void ThreadPro()
{
Console.WriteLine("Main Thread:Start a second work !");
//声明一个线程,并指明调用方法:ThreadWork
Thread t = new Thread(new ThreadStart(ThreadWork));
//开启一个线程
t.Start();
for (int i = 0; i < 4;i++ )
{
Console.WriteLine("Main Thread:Do some work!");
//线程休眠时间
Thread.Sleep(0);
}
Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
t.Join();
Console.WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.");
}
//被线程调用方法
public static void ThreadWork()
{
//要干的具体事,可以在这个方法里面实现
Console.WriteLine("ThreadWork start work!");
for (int i = 0; i < 10; i++)
{
Console.WriteLine("ThreadWork:{0}",i);
Thread.Sleep(0);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Threading;
namespace ThreadEag
{
class WorkPace
{
//泛型创建一个People集合,
private static List<People> list;
public static List<People> List
{
get { return list; }
set { list = value; }
}
public void ThreadWork()
{
//初始化三个people事例
People a = new People();
People b = new People();
People c = new People();
//分别给他们的状态赋值
a.State = "A";
b.State = "B";
c.State = "C";
//初始化集合类对象
list = new List<People>();
//将人员添加的集合里面
list.Add(a);
list.Add(b);
list.Add(c);
//初始化三个线程对象
Thread ta = new Thread(new ThreadStart(a.Work));
Thread tb = new Thread(new ThreadStart(b.Work));
Thread tc = new Thread(new ThreadStart(c.Work));
//开启三个线程
ta.Start();
tb.Start();
tc.Start();
}
}
//人员的类
public class People
{
private string name;
public string Name
{
get{ return this.name;}
set { this.name = value; }
}
public string state;
public string State
{
get { return this.state; }
set { this.state = value; }
}
//工作
public void Work()
{
if (state == "A")
{
WorkPace.List[0].state = "干活中";
Console.WriteLine("A干活的时候,B,C休息!");
//干活完毕
WorkPace.List[0].state = "完毕!";
Console.WriteLine("A的工作完成!");
}
if (state =="B")
{
while(WorkPace.List[0].state != "完毕!")
{
Thread.Sleep(1000);
}
Console.WriteLine("B开始干活!");
}
if (state == "C")
{
while(WorkPace.List[0].state != "完毕!")
{
Thread.Sleep(1000);
}
Console.WriteLine("C开始干活!");
}
}
}
}
static void Main(string[] args)
{
WorkPace workPace = new WorkPace();
workPace.ThreadWork();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
namespace ThreadEag
{
public class LogMessage
{
/// <summary>
/// 写入日志 by crazycoder
/// </summary>
/// <param name="fileName">日志文件名,不需要带路径</param>
/// <param name="content">日志内容,单位为行</param>
public static void WriteLog(string fileName,string content)
{
//获取日志文件物理路径
string path = CrazyCoderGetMapPath(fileName);
try
{
if (!System.IO.File.Exists(path))
{
StreamWriter sw = System.IO.File.CreateText(path);
sw.WriteLine("-----日志开始----"+DateTime.Now.ToString());
sw.Flush();
sw.Close();
}
StreamWriter sw2 = System.IO.File.AppendText(path);
//每个日志写一行
sw2.WriteLine("---"+ DateTime.Now.ToString() +"---"+content);
sw2.Flush();
sw2.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
private static string CrazyCoderGetMapPath(string strPath)
{
if (HttpContext.Current != null)
{
//如果是Web
return HttpContext.Current.Server.MapPath(strPath);
} //非web程序引用
else
{
if(strPath.StartsWith("~/"))
{
strPath = strPath.Substring(2);
}
else if(strPath.StartsWith("/"))
{
strPath = strPath.Substring(1);
}
return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,strPath);
}
}
}
}