ch7_05.cs
来自「《c#技术内幕代码》」· CS 代码 · 共 62 行
CS
62 行
using System;
using System.IO;
using System.Threading;
public class CH7_5
{
Thread[] threads;
string[] sFileNames;
int[] iThreadIDs;
public void RunThread()
{
string sFile = "";
int id = Thread.CurrentThread.GetHashCode();
// Find it in the list
for ( int i=0; i<iThreadIDs.Length; ++i )
if ( id == iThreadIDs[i] )
sFile = sFileNames[i];
// If we found a filename for this thread, count the
// letters in it
StreamReader sr = File.OpenText(sFile);
int nCount = 0;
while (sr.Peek()!=-1)
{
int ch = sr.Read();
nCount ++;
}
sr.Close();
Console.WriteLine("There were {0} characters in file {1}",
nCount, sFile );
}
public void StartThreads(String[] args)
{
threads = new Thread[args.Length];
sFileNames = new string[args.Length];
iThreadIDs = new int[args.Length];
for ( int i=0; i<args.Length; ++i )
{
sFileNames[i] = args[i];
threads[i] = new Thread( new ThreadStart(RunThread) );
iThreadIDs[i] = threads[i].GetHashCode();
Console.WriteLine("Thread ID {0} is with file {1}",
iThreadIDs[i],
sFileNames[i] );
threads[i].Start();
}
}
public static void Main(String[] args)
{
CH7_5 app = new CH7_5();
app.StartThreads(args);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?