解决方案 »

  1.   

    看到一个地方,
    t_div = new Thread(new ParameterizedThreadStart(div));div(Object ostr) 函数带参数,参考:
    Thread线程调用带参数的方法  
      

  2.   

    sorry, LZ 这种写法也是可以的
      

  3.   

    各位大神,不好意思,现在我发一下完整的代码,还是上面说的那个问题,希望大神们帮忙看看。我读入的需要被拆词的txt文件有130MB,问题还是程序在执行完多线程拆词以后,根本不执行combineresult函数。求求各位大神指导指导using System;
    using ZZHDivideChinese;
    using System.Runtime.InteropServices;
    using System.IO;
    using System.Collections;
    using System.Text;
    using System.Threading;namespace calldlltest
    {

    class Program
    {
    public static void Main(string[] args)
    {
    Ini configini = new Ini(System.Environment.CurrentDirectory+"\\Config.ini");
    string n_Thread = Environment.ProcessorCount.ToString();
    string ImputFile = configini.ReadValue("General","ImputFile");
    string SplitFile = configini.ReadValue("General","SplitFile");
    string OutputFile = configini.ReadValue("General","OutputFile");
    string DictionaryPath = configini.ReadValue("General","DictionaryPath");
    StreamReader sr = new StreamReader(ImputFile,Encoding.GetEncoding("GBK"));
    string sline;
    ArrayList a_txt = new ArrayList();
    ArrayList temp = new ArrayList();
    menu:
    hehe begin = new hehe();
    Console.WriteLine("Task Menu: ");
    Console.WriteLine("1. Split text file");
    Console.WriteLine("2. Divide Chinese");
    Console.WriteLine("3. Exit");
    Console.Write("Enter the sequence number of the task: ");
    string keyImprot = Console.ReadLine();
    if (keyImprot=="1")
    {
    Console.Write("Enter how many need split into ,or pass 0 to back: ");
    try
    {
    int fn = int.Parse(Console.ReadLine());
    if(fn == 0)
    {
    Console.Clear();
    goto menu;
    }
    sline = "";
    while (sline!=null)
    {
    sline = sr.ReadLine();
    if(sline==null||sline=="") break;
    a_txt.Add(sline);
    }
    for(int i=0;i<=fn-1;i++)
    {
    FileStream fs1 = new FileStream(SplitFile.Substring(0,SplitFile.Length-4)+" "+i+ImputFile.Substring(ImputFile.Length-4,4),FileMode.Create);
    temp.Add(fs1);
    }
    for(int i=0;i<=fn-1;i++)
    {   
    Console.WriteLine(SplitFile.Substring(0,SplitFile.Length-4)+" "+i+ImputFile.Substring(ImputFile.Length-4,4)+" Done.");
    StreamWriter sw1 = new StreamWriter((FileStream)temp[i],Encoding.GetEncoding("GB2312"));
    for(int j=0+i*a_txt.Count/fn;j<(1+i)*a_txt.Count/fn;j++)
    {
    sw1.WriteLine((String)a_txt[j]);
    }
    if(i==fn-1&&a_txt.Count%fn>0)
    {
    for(int k=0;k<a_txt.Count%fn;k++)
    {
    sw1.WriteLine((String)a_txt[k+a_txt.Count-a_txt.Count%fn]);
    }
    }
    sw1.Flush();
    sw1.Close();
    }
    temp.Clear();
    a_txt.Clear();
    Console.WriteLine("Split text file complite.");
    Console.Write("Press any key to continue . . . ");
    Console.ReadKey(true);

    catch
    {
    temp.Clear();
    a_txt.Clear();
    Console.Clear();
    Console.WriteLine("Error,please try again: ");
    goto menu;
    }
    }
    else if (keyImprot=="2")
    {
    sline = "";
    try
    {
    Console.WriteLine("Loading file . . . ");
    while (sline!=null)
    {
    sline = sr.ReadLine();
    if(sline==null||sline=="") break;
    a_txt.Add(sline);

    }
    Console.SetCursorPosition(30,Console.CursorTop-1);
    Console.WriteLine(a_txt.Count);
    }
    catch(OutOfMemoryException e)
    {
    Console.WriteLine(a_txt.Count);
    Console.WriteLine(e.Message);
    Console.Write("Press any key to continue . . . ");
    Console.ReadKey(true);
    }
    divChinese dc = new divChinese(a_txt,DictionaryPath,int.Parse(n_Thread),OutputFile);
    }
    else if (keyImprot=="3")
    {
    Environment.Exit(0);
    }
    else
    {
    Console.Write("Error,please try again: ");
    Console.Clear();
    goto menu;
    }
    }
    }
    public class divChinese
    {
    [DllImport("ZZHDivideChinese.dll")]
    public static extern string Split(string strLine);
    int nthread;
    int t_alive;
    int f_cursor = 0;
    int[,] i_dis;
    ArrayList a_thread = new ArrayList();
    ArrayList a_text = new ArrayList();
    ArrayList strlist = new ArrayList();
    ArrayList a_rate = new ArrayList();
    Thread t_div,t_dis;
    string dpath,opFile,substr;
    public divChinese(ArrayList a_str,string d_path,int n_Thread,string OutputFile)
    {
    dpath = d_path;
    nthread = n_Thread;
    t_alive = n_Thread;
    opFile = OutputFile;
    i_dis = new int[nthread,3];
    for(int i=0;i<=nthread-1;i++)
    {
    a_text = new ArrayList();
    for(int j=i*a_str.Count/nthread;j<(1+i)*a_str.Count/nthread;j++)
    {
    a_text.Add((String)a_str[j]);
    }
    if(i==nthread-1&&a_str.Count%nthread>0)
    {
    for(int k=0;k<a_str.Count%nthread;k++)
    {
    a_text.Add((String)a_str[k+a_str.Count-a_str.Count%nthread]);
    }
    }
    a_rate.Add(a_text.Count);
    strlist.Add(a_text);
    t_div = new Thread(new ParameterizedThreadStart(div));
    a_thread.Add(t_div);
    }
    for(int i=0;i<=nthread-1;i++)
    {
    a_text = (ArrayList)strlist[i];
    a_text.Add(i);
    i_dis[i,0] = 0;
    i_dis[i,1] = a_text.Count;
    i_dis[i,2] = 0;
    t_div = (Thread)a_thread[i];
    if(i==0)
    {
    t_div.Start(a_text);
    }
    else
    {
    while(true)
    {
    if(i_dis[i-1,2]==1)
    {
    t_div.Start(a_text);
    break;
    }
    }
    }
    }
    }
    public void div(Object ostr)
    {
    ZZHDivideChinese.DivideChinese dc = new DivideChinese(dpath);
    ArrayList astr = (ArrayList)ostr;
    int t_id = (int)astr[astr.Count-1];
    if(f_cursor==0)
    {
    f_cursor = Console.CursorTop;
    }
    else if(Console.CursorTop==f_cursor+nthread-1&&t_dis==null)
    {
    t_dis = new Thread(displayRate);
    t_dis.Priority = ThreadPriority.Lowest;
    t_dis.Start();
    }
    Console.WriteLine("Thread "+t_id+" execute . . . ");
    i_dis[t_id,2] = 1;
    astr.RemoveAt(astr.Count-1);
    for(int i=0;i<=astr.Count-1;i++)
    {
    try
    {
    substr = (string)astr[i];
    if(substr.LastIndexOf(",")>=0)
    {
    substr = substr.Substring(substr.LastIndexOf(",")+1,substr.Length-substr.LastIndexOf(",")-1);
    }
    astr[i] = astr[i]+","+dc.Split(substr);
    i_dis[t_id,0] = (int)i_dis[t_id,0] +1;
    }
    catch
    {
    i_dis[t_id,0] = (int)i_dis[t_id,0] +1;
    continue;
    }
    }
    for(int i=0;i<a_thread.Count;++i)
    {
    t_div = (Thread)a_thread[i];
    if(!t_div.IsAlive)
    {
    t_alive = t_alive-1;
    }
    else 
    {
    i=i-1;
    }
    }
    if(t_alive==0)
    {
    t_dis.Abort();
    Console.SetCursorPosition(0,f_cursor+nthread);
    combineResult();
    t_div = (Thread)a_thread[t_id];
    t_div.Abort();
    }
    else
    {
    t_div = (Thread)a_thread[t_id];
    t_div.Abort();
    }
    }
    private void displayRate()
    {
    while(true)
    {
    int t_exe = 0;
    for(int i=0;i<nthread;i++)
    {
    if(i_dis[i,2]==1)
    {
    t_exe = t_exe + 1;
    }
    }
    if(t_exe==nthread)
    {
    break;
    }
    }
    while(true)
    {
    for(int i=0;i<nthread;i++)
    {
    Console.SetCursorPosition(30,f_cursor+i);
    Console.WriteLine("{0}    {1}%",i_dis[i,0],Math.Round(i_dis[i,0]*100F/i_dis[i,1]));
    }
    }
    }
    private void combineResult()
    {
    Console.WriteLine("Exproting file. . .");
    FileStream fs2 = new FileStream(opFile, FileMode.Create);
    StreamWriter sw2 = new StreamWriter(fs2);
    try
    {
    for(int i=0;i<nthread;i++)
    {
    ArrayList tstr = (ArrayList)strlist[i];
    foreach(Object oos in tstr)
    {
    sw2.WriteLine((String)oos);
    }
    tstr.Clear();
    }
    }
    catch(FieldAccessException e)
    {
    Console.WriteLine(e.Message);
    }
    strlist.Clear();
    sw2.Flush();
    sw2.Close();
    Console.WriteLine("Divide Chinese complite. Press any key to continue . . . ");
    Console.ReadKey(true);
    }
    }
    public class Ini
        {
            // 声明INI文件的写操作函数 WritePrivateProfileString()        [System.Runtime.InteropServices.DllImport("kernel32")]        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);        // 声明INI文件的读操作函数 GetPrivateProfileString()        [System.Runtime.InteropServices.DllImport("kernel32")]        private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);
            private string sPath = null;
            public Ini(string path)
            {
                this.sPath = path;
            }        public void Writue(string section, string key, string value)
            {            // section=配置节,key=键名,value=键值,path=路径            WritePrivateProfileString(section, key, value, sPath);        }
            public string ReadValue(string section, string key)
            {            // 每次从ini中读取多少字节            System.Text.StringBuilder temp = new System.Text.StringBuilder(255);            // section=配置节,key=键名,temp=上面,path=路径            GetPrivateProfileString(section, key, "", temp, 255, sPath);            return temp.ToString();        }
        }
      

  4.   

    代码写的真乱,各种Abort()
    程序逻辑执行完了,自己退出while()循环,而不要从外部Abort()
    没准你杀死了自己所在的线程,导致后面都不运行了
      

  5.   

    不过大神,Abort应该是强行终止一个线程吧,如果我不用abort,有什么替代品吗?可以给一个示例吗?
      

  6.   

    你好,我不好意思再要代码了,不过,不要的话没法调啊,到第七行
    StreamReader sr = new StreamReader(ImputFile,Encoding.GetEncoding("GBK"));
    它就报异常了,因为没有config.ini这个配置文件,所以空字串了异常了. 
    还有那个using ZZHDivideChinese;
    这个Dll也没有.
      

  7.   

    你不是判断if()然后Abort()么
    把这句话放到循环里去,if() break;//终止循环
    让线程自己退出,不要在外部结束.否则逻辑太混乱