您的位置:首页 > 其它

20150907

2015-09-07 15:17 274 查看
利用DataRow,Select DataSet中的数据

          DataTable dt = GetDateSet("select * from QSDB_IMPORT_JOB").Tables[0];

          DataRow[] rows = dt.Select("underlying='0017.HK'");

          foreach (DataRow row in rows)

          {

              Console.WriteLine(row["REGION"].ToString());

          }

线程池

 public class TaskInfo

    {       

        public string Boilerplate;

        public int Value;

        // the information needed for the task.

        public TaskInfo(string text, int number)

        {

            Boilerplate = text;

            Value = number;

        }

    }

 static void Main(string[] args)

      {

         

          TaskInfo ti = new TaskInfo("This report displays the number {0}.", 42);          

          ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc), ti);   //Params: functions and parameters the function need to use      

          Thread.Sleep(1000);         

          Console.ReadKey();

      }

      static void ThreadProc(Object stateInfo)

      {

              TaskInfo ti = (TaskInfo)stateInfo;

              Console.WriteLine(ti.Boilerplate, ti.Value);          

      }

/*

类似互斥锁,但它可以允许多个线程同时访问一个共享资源

通过使用一个计数器来控制对共享资源的访问,如果计数器大于0,就允许访问,如果等于0,就拒绝访问。计数器累计的是“许可证”的数目,为了访问某个资源。线程必须从信号量获取一个许可证。

通常在使用信号量时,希望访问共享资源的线程将尝试获取一个许可证,如果信号量的计数器大于0,线程将获取一个许可证并将信号量的计数器减1,否则先线程将阻塞,直到获取一个许可证;当线程不再需要共享资源时,将释放锁拥有的许可证,并将许可证的数量加1,如果有其他的线程正在等待许可证,那么该线程将立刻获取许可证。*/
 class mythread

    {

       

        public Thread thrd;

        //创建一个可授权2个许可证的信号量,且初始值为2

        static Semaphore sem = new Semaphore(2, 2);

        public mythread(string name)

        {

            thrd = new Thread(this.run);

            thrd.Name = name;

            thrd.Start();

        }

        void run()

        {

            Console.WriteLine(thrd.Name + "wait for premission");

            //申请一个许可证

            sem.WaitOne();

            Console.WriteLine(thrd.Name + "premitted……");

            for (int i = 0; i < 4; i++)

            {

                Console.WriteLine(thrd.Name + ": " + i);

                Thread.Sleep(1000);

            }

            Console.WriteLine(thrd.Name + " release……");

            //释放

            sem.Release();

        }

    }

    class mysemaphore

    {

        public static void Main()

        {

            mythread mythrd1 = new mythread("Thrd #1");

            mythread mythrd2 = new mythread("Thrd #2");

            mythread mythrd3 = new mythread("Thrd #3");

            mythread mythrd4 = new mythread("Thrd #4");

            mythrd1.thrd.Join();

            mythrd2.thrd.Join();

            mythrd3.thrd.Join();

            mythrd4.thrd.Join();

        }

    } 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: