• 异步 进程 读取文件内容


    class Class1
    {
    public delegate int AsyncEventHandler(int a);
    static List<string> l = new List<string>();
    static int ps = 1000;
    static long end = 0;
    static long start = 0;

    void Event1()
    {
    Console.WriteLine("Event1 Start");
    System.Threading.Thread.Sleep(2000);
    Console.WriteLine("Event1 End");
    }

    int Event2(int pi)
    {
    Console.WriteLine("Event2 Start 第" + pi + "次");

    for (int i = 0; i < ps; i++)
    {
    Console.WriteLine(l[(pi - 1) * ps + i]);
    }
    Console.WriteLine("Event2 End 第" + pi + "次");
    return pi;
    }

    void CallbackMethod(IAsyncResult ar)
    {
    var del = (AsyncEventHandler)ar.AsyncState;
    var a = del.EndInvoke(ar);
    Console.WriteLine(" 第" + a + "次 回调------------------耗时:" + (end - start).ToString() + "-------------------------");
    //((AsyncEventHandler)ar.AsyncState).EndInvoke(ar);
    }


    [STAThread]
    static void Main(string[] args)
    {
    Class1 c = new Class1();
    Console.WriteLine("ready");
    start = DateTime.Now.Ticks;

    //AsyncEventHandler asy = new AsyncEventHandler(c.Event1);
    //IAsyncResult ia = asy.BeginInvoke(new AsyncCallback(c.CallbackMethod), asy);
    //c.Event2();
    //asy.EndInvoke(ia);



    using (StreamReader sr = new StreamReader(@"R:\123.txt", Encoding.Default))
    {
    AsyncEventHandler asy = new AsyncEventHandler(c.Event2);
    var line = sr.ReadLine();
    while (line != null)
    {
    l.Add(line);
    if (l.Count % ps == 0 && l.Count > 0)
    {
    IAsyncResult ia = asy.BeginInvoke(l.Count / ps, new AsyncCallback(c.CallbackMethod), asy);
    }
    //Console.WriteLine(line);//=144218750
    //Task<int>.Factory.FromAsync(asy.BeginInvoke, asy.EndInvoke, (l.Count / ps), null).ContinueWith(o => Console.WriteLine(string.Format("结果:{0}", o.Result)));
    line = sr.ReadLine();
    }
    }
    //WaitHandle.WaitAll();
    end = DateTime.Now.Ticks;
    Console.WriteLine("时间刻度差=" + Convert.ToString(end - start));
    Console.ReadLine();

    }
    }
  • 相关阅读:
    插入排序-Java
    选择排序-java
    逻辑回归----梯度上升
    logistic回归----- 随机梯度下降法
    JAVA实现聚类指标的计算Purity、NMI、RI、Precision、Recall、F值。
    Python 条形图绘制
    java中接口的注意事项
    算法-双向队列
    算法-manacher-最长回文子串-1
    算法-kmp-1
  • 原文地址:https://www.cnblogs.com/yyjj/p/2385377.html
Copyright © 2020-2023  润新知