C#中创建多线程应用程序域
受到国家税务总局周三晚间宣布将证券交易印花税税率,由现行的3‰调整为1‰影响,沪深基金指数今日高开高走,展开强势反弹行情,基金指数双双创出历史最大单日涨幅。 这对我这个被股票套了20%的穷人来讲是个再好不过的消息了!
哈哈,转机来了,希望大盘这次能冲到5500+,6K也不介意,这样偶才能小赚点……
-------------------------------------
回到正题,这次的内容是关于多线程操作的,比较简单,用到了一点同步知识,希望能对有这方面需要的朋友有所帮助。
l 场景
项目文件及需求我就不传了。有需要的朋友可以在回复中留下邮箱。
哈哈,转机来了,希望大盘这次能冲到5500+,6K也不介意,这样偶才能小赚点……
-------------------------------------
回到正题,这次的内容是关于多线程操作的,比较简单,用到了一点同步知识,希望能对有这方面需要的朋友有所帮助。
l 场景
假设你是一家软件开发公司的开发人员。公司要开发一个多线程项目管理应用程序的原型。该应用程序将使用Microsoft Windows Server 2003来承载。你必须使用控制台应用程序来开发一个项目管理应用程序框架的原型。所创建的应用程序框架将用做商务外观层,用户界面层将构造在它之上。
最终,该应用程序框架将能够成为多个数据库后端的接口;但是,当开发该原型时,你将该数据存储在哈希表中。应用程序框架必须非常好地响应用户请求。此外,设计该应用程序框架,以允许单个项目管理者修改数据,而许多其他应用程序用户可以读取项目信息(与输入的信息一致)。
应该将一个项写入该应用程序事件日志,以记录输入到该应用程序中的每个项目。当将项目记录到该应用程序日志中时,该项目应该反射。
必须完成该控制台应用程序。你将开始向该控制台应用程序添加代码,以衍生多个线程。应该创建一个线程来模拟单个编写器线程,并应该至少创建3个其他线程来模拟用户工作站来读取项目信息。还应该创建一个用于写入该应用程序事件日志的线程。
本任务的主要步骤是衍生多个线程。
主要代码如下:
static void ShowMenu()
{
string userSelection ;
//Thread newThread;
Console.WriteLine();
Console.WriteLine("=================================");
Console.WriteLine(" PROJECT MANAGEMENT MENU");
Console.WriteLine("=================================");
Console.WriteLine();
Console.WriteLine("Enter 1 to Add a new project");
Console.WriteLine("Enter 2 to View a project");
Console.WriteLine("Enter 3 to Exit");
Console.WriteLine();
Console.WriteLine("=================================");
Console.WriteLine();
Console.Write("Enter the choice :- ");
userSelection = Console.ReadLine();
while (char.IsNumber(userSelection, 0) == false)
{
Console.WriteLine("Invalid Number Format ");
Console.Write("Enter choice in valid number format :- ");
userSelection = Console.ReadLine();
}
int uSelection = Int32.Parse(userSelection);
Console.Clear();
switch (uSelection)
{
case 1:
Thread writeThread = new Thread(new ThreadStart(WriterMethod));
writeThread.Start();
break;
case 2:
Thread readThread = new Thread(new ThreadStart(ReaderMethod));
readThread.Start();
break;
default:
break;
}
}
{
string userSelection ;
//Thread newThread;
Console.WriteLine();
Console.WriteLine("=================================");
Console.WriteLine(" PROJECT MANAGEMENT MENU");
Console.WriteLine("=================================");
Console.WriteLine();
Console.WriteLine("Enter 1 to Add a new project");
Console.WriteLine("Enter 2 to View a project");
Console.WriteLine("Enter 3 to Exit");
Console.WriteLine();
Console.WriteLine("=================================");
Console.WriteLine();
Console.Write("Enter the choice :- ");
userSelection = Console.ReadLine();
while (char.IsNumber(userSelection, 0) == false)
{
Console.WriteLine("Invalid Number Format ");
Console.Write("Enter choice in valid number format :- ");
userSelection = Console.ReadLine();
}
int uSelection = Int32.Parse(userSelection);
Console.Clear();
switch (uSelection)
{
case 1:
Thread writeThread = new Thread(new ThreadStart(WriterMethod));
writeThread.Start();
break;
case 2:
Thread readThread = new Thread(new ThreadStart(ReaderMethod));
readThread.Start();
break;
default:
break;
}
}
l 场景
在本练习中,你将继续使用上述应用程序。你将向该应用程序添加一个哈希表,以用做应用程序数据存储区。将通过编写器线程来写入该哈希表,并通过读取器线程来读该哈希表。因为它属于共享资源,必须使用ReaderWriterLock同步线程对该哈希表的访问。
本任务的主要步骤是对共享资源进行同步static void WriterMethod()
{
ProjectInfo proInfo = new ProjectInfo();
Console.Write("Please enter name:");
proInfo.Name = Console.ReadLine();
Console.Write("Description:");
proInfo.Description = Console.ReadLine();
Console.Write("StartDate:");
DateTime startTime =DateTime.Parse(Console.ReadLine());
Console.Write("EndDate:");
DateTime endTime = DateTime.Parse(Console.ReadLine());
proInfo.StartDate = startTime;
proInfo.EndDate = endTime;
Console.Write("BillableHours:");
proInfo.BillableHours = int.Parse(Console.ReadLine());
Console.Write("LoggedDate:");
DateTime logDate = DateTime.Parse(Console.ReadLine());
proInfo.LoggedDate = logDate;
Console.WriteLine(string.Empty.PadRight(30, '-'));
Console.Write("Ok,Please enter project name:");
string name = Console.ReadLine();
try
{
//加写锁
rwLock = new ReaderWriterLock();
rwLock.AcquireWriterLock(1000);
//对HashTable加锁
lock (projectTable)
{
projectTable.Add(name, proInfo);
}
rwLock.ReleaseWriterLock();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.Read();
}
ShowMenu();
}
static void ReaderMethod()
{
Console.WriteLine(string.Empty.PadRight(30, '*'));
Console.Write("Please enter project name:");
string name = Console.ReadLine();
try
{
//加读锁
rwLock = new ReaderWriterLock();
rwLock.AcquireReaderLock(1000);
if (projectTable.Contains(name))
{
DisplayMember(name);
}
else
{
Console.WriteLine("Sorry,can't find result!");
Console.Read();
}
rwLock.ReleaseReaderLock();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.Read();
}
ShowMenu();
}
static void DisplayMember(string name)
{
Console.WriteLine("name Description StartDate EndDate BillableHours LoggedDate");
foreach(DictionaryEntry key in projectTable)
{
if (key.Key.ToString() == name)
{
Console.WriteLine(((ProjectInfo)key.Value).Name + " " + ((ProjectInfo)key.Value).Description
+ " " + ((ProjectInfo)key.Value).StartDate.ToShortDateString()
+ " " + ((ProjectInfo)key.Value).EndDate.ToShortTimeString()
+ " " + ((ProjectInfo)key.Value).BillableHours
+ " " + ((ProjectInfo)key.Value).LoggedDate.ToShortDateString());
}
}
}
{
ProjectInfo proInfo = new ProjectInfo();
Console.Write("Please enter name:");
proInfo.Name = Console.ReadLine();
Console.Write("Description:");
proInfo.Description = Console.ReadLine();
Console.Write("StartDate:");
DateTime startTime =DateTime.Parse(Console.ReadLine());
Console.Write("EndDate:");
DateTime endTime = DateTime.Parse(Console.ReadLine());
proInfo.StartDate = startTime;
proInfo.EndDate = endTime;
Console.Write("BillableHours:");
proInfo.BillableHours = int.Parse(Console.ReadLine());
Console.Write("LoggedDate:");
DateTime logDate = DateTime.Parse(Console.ReadLine());
proInfo.LoggedDate = logDate;
Console.WriteLine(string.Empty.PadRight(30, '-'));
Console.Write("Ok,Please enter project name:");
string name = Console.ReadLine();
try
{
//加写锁
rwLock = new ReaderWriterLock();
rwLock.AcquireWriterLock(1000);
//对HashTable加锁
lock (projectTable)
{
projectTable.Add(name, proInfo);
}
rwLock.ReleaseWriterLock();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.Read();
}
ShowMenu();
}
static void ReaderMethod()
{
Console.WriteLine(string.Empty.PadRight(30, '*'));
Console.Write("Please enter project name:");
string name = Console.ReadLine();
try
{
//加读锁
rwLock = new ReaderWriterLock();
rwLock.AcquireReaderLock(1000);
if (projectTable.Contains(name))
{
DisplayMember(name);
}
else
{
Console.WriteLine("Sorry,can't find result!");
Console.Read();
}
rwLock.ReleaseReaderLock();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.Read();
}
ShowMenu();
}
static void DisplayMember(string name)
{
Console.WriteLine("name Description StartDate EndDate BillableHours LoggedDate");
foreach(DictionaryEntry key in projectTable)
{
if (key.Key.ToString() == name)
{
Console.WriteLine(((ProjectInfo)key.Value).Name + " " + ((ProjectInfo)key.Value).Description
+ " " + ((ProjectInfo)key.Value).StartDate.ToShortDateString()
+ " " + ((ProjectInfo)key.Value).EndDate.ToShortTimeString()
+ " " + ((ProjectInfo)key.Value).BillableHours
+ " " + ((ProjectInfo)key.Value).LoggedDate.ToShortDateString());
}
}
}
项目文件及需求我就不传了。有需要的朋友可以在回复中留下邮箱。
SIGNATRUE-----------------------------------
龟看上去很慢很慢,而且还有些憨,虽然没有兔子跑的快,但是只要有坚持不懈的毅力,就一定会到达成功的比彼岸.如果自己是龟,就不要试图把自己变成兔子,我就是那只憨龟。
龟看上去很慢很慢,而且还有些憨,虽然没有兔子跑的快,但是只要有坚持不懈的毅力,就一定会到达成功的比彼岸.如果自己是龟,就不要试图把自己变成兔子,我就是那只憨龟。