C# Thread test demo

    [The TestClass]
     public  class UnitTest1 
    { 
        [TestMethod] 
        public  void TestMethod1 () 
        { 
            / * 
             A process can create one or more threads to perform some program code associated with the process. In C #, the thread is processed using the Thread class, the class in the System.Threading namespace. 
             When using the Thread class to create a thread, only you need to provide a thread entrance, the entrance thread to tell the program what to do to make this thread. By instantiating a Thread object class you can create a thread. 
             When you create a new Thread object, you create a new managed threads. Receiving a Thread class or ParameterizedThreadStart ThreadStart delegate constructor delegate, the delegate packaging method calls the Start method is invoked by the new thread, the following sample code: 

             * / 
            // call the method without parameters 
            Thread Thread = new new Thread ( new new ThreadStart ( ProcessAndThread)); 
            Thread.start (); 

            // thread created by anonymous delegate
            = Thread1 the Thread new new the Thread ( delegate () {WriteLog ( String .Empty, null , " I am the thread created by anonymous delegate " );}); 
            thread1.Start (); 
            // commissioned by Lambda expressions created 
            Thread thread2 = new new the Thread (() => WriteLog ( String .Empty, null , " I was created by the delegate Lambda expressions " )); 
            thread2.Start (); 
        } 

        protected  void ProcessAndThread () 
        { 

            String [] = {Lines " Line First ", "Second line", "Third line" };
            WriteLog(string.Empty, lines, string.Empty);

            string text = "A class is the most powerful data type in C#. Like a structure, " + "a class defines the data and behavior of the data type. ";
            WriteLog(string.Empty, null, text);
        }

        protected void WriteLog(string pre, string[] lines, string logContent)
        {
            string dir = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\log\\";
            dir = AppDomain.CurrentDomain.BaseDirectory + "\\log\\";
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            if (string.IsNullOrEmpty(pre))
            {
                pre = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff") + " ";
            }
            string logFileUrl = Path.Combine(dir, DateTime.Now.ToString("yyyyMMdd") + ".log");
            if (!File.Exists(logFileUrl))
            {
                File.Create(logFileUrl).Close();
            }
            if (lines != null)
            {
                File.AppendAllLines(logFileUrl, lines);
            }
            //if (!string.IsNullOrEmpty(logContent))
            //{
            File.AppendAllText(logFileUrl, pre + logContent);
            //}
        }

    }
View Code

 

Published 259 original articles · won praise 2 · views 10000 +

Guess you like

Origin blog.csdn.net/hofmann/article/details/104058217