⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch7_05.cs

📁 《c#技术内幕代码》
💻 CS
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -