コード例 #1
0
        public static void ThreadT2()
        {
            ThreadRun    run     = new ThreadRun();
            TestRunClass myclass = new TestRunClass();

            run.back = new WaitCallback(d =>
            {
                var obj   = (TestRunClass)d;
                obj.Name  = "wj";
                obj.State = 1;
                Console.WriteLine($"Thread ID:{Thread.CurrentThread.ManagedThreadId} Name :{CallContext.LogicalGetData("Name")} state:{obj.Name}");
            });
            run.back(myclass);
            Console.WriteLine("主线程");
            Console.WriteLine($"Thread ID:{Thread.CurrentThread.ManagedThreadId}");
            Console.WriteLine();

            CallContext.LogicalSetData("Name", "Alice");
            ThreadPool.QueueUserWorkItem(run.back);
            Console.WriteLine("IsFlowSuppressed:{0}", ExecutionContext.IsFlowSuppressed());
            //ExecutionContext.SuppressFlow();
            //ThreadPool.QueueUserWorkItem(
            //    d =>
            //    {
            //        Console.WriteLine($"Thread ID:{Thread.CurrentThread.ManagedThreadId} Name :{CallContext.LogicalGetData("Name")}");
            //    });
            Console.WriteLine();
            Console.Write("Hit <Enter> to End.");
            Thread.Sleep(5000);
            Console.WriteLine($"MyClass Name{myclass.Name} State{myclass.State}");
        }
コード例 #2
0
        public static void ThreadT3()
        {
            ThreadRun r    = new ThreadRun();
            string    path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "test.txt");

            Console.WriteLine("主程序中!");
            Console.WriteLine("线程的Id:{0}", Thread.CurrentThread.ManagedThreadId);
            Console.WriteLine();

            FileStream fs = new FileStream(path, System.IO.FileMode.Open, System.Security.AccessControl.FileSystemRights.Read, System.IO.FileShare.Read, 8, System.IO.FileOptions.Asynchronous);

            byte[] butter = new byte[fs.Length];
            //fs.Read(butter, 0, butter.Length);
            //string message = Encoding.Default.GetString(butter, 0, butter.Length);
            //Console.WriteLine(message);
            AsyncCallback back = new AsyncCallback(r.AsyncCallback);
            //IAsyncResult result = fs.BeginRead(butter, 0, butter.Length, d =>
            //     {
            //         Console.WriteLine("第三步操作中,");
            //         Console.WriteLine("线程的 Id:{0}", Thread.CurrentThread.ManagedThreadId);
            //         //int lenth = fs.EndRead(d);
            //         //string message = Encoding.Default.GetString(butter, 0, butter.Length);
            //         //Console.WriteLine(message);
            //     }, fs);
            IAsyncResult result2 = fs.BeginRead(butter, 0, butter.Length, back, null);

            Console.WriteLine("第一步完成 !");
            Console.WriteLine("线程的Id:{0}", Thread.CurrentThread.ManagedThreadId);
            //result.AsyncWaitHandle.WaitOne();
            int    lenth   = fs.EndRead(result2);
            string message = Encoding.Default.GetString(butter, 0, butter.Length);

            Console.WriteLine(message);
            Console.WriteLine("结束》》》》》》》》》》》》》》》》》线程的Id:{0}", Thread.CurrentThread.ManagedThreadId);
            //Console.ReadLine();
        }