运行异步线程 [英] Running Asynchronus threads

查看:95
本文介绍了运行异步线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序要在其中调用计时器,当该计时器到期时,将执行一些操作.

I have a program where I want to call a timer and when that timer expires some action takes place.

我正在等待计时器到期时,我想执行其他操作.我不知道我在下面做的事情是否可以接受.  运行以下代码会有什么后果?  

While I am waiting for the timer to expire I want to do other actions.  I don't know if what I am doing below is acceptable.  What are the consequences of running the below code?  

我正在尝试使用.net 2.0

I am trying to stay with .net 2.0

这是我执行操作的单独类:

Here is my separate class for running the action:

class SeperateThread
    {
        public SeperateThread()
        {
                //do my thing.  Wait for event.  On event return
                EventWatch.Start();
                //Thread will wait here until an event happens
                EventWatch.WaitForNextEvent(); 
                //after event happens the thread will return and the next line of code in the calling method will do something.
                EventWatch.Stop();
                return;
        }
    }

上面的代码就是这样

namespace ThreadTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //Call my seperate thread but dont wait for return
            Thread MySeperateThread = new Thread(StartThreadASC);
            MySeperateThread.Name = "regWatchThread";// Asigning name to the thread
            MySeperateThread.IsBackground = false;// Made the thread forground
            MySeperateThread.Priority = ThreadPriority.AboveNormal;// Setting thread priority
            MySeperateThread.Start();// Start DoTask method in a new thread
                                     //Do other tasks  while the thread above is running
            do
            {
                for (int i = 0; i < 100000; i++)
                {
                    Console.WriteLine(i.ToString());
                }
            } while (1 == 1);
            Console.WriteLine();

            return;
        }

        private static void StartThreadASC()
        {
            Console.WriteLine("called thread00");
            SeperateThread SeperateThread = new SeperateThread();
            Console.WriteLine("thread returned");
            //Do my event action
        }
}
}

推荐答案

那么正在做什么呢?


这篇关于运行异步线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆