替换命名空间System.Threading.Timers命名空间 [英] Replacing the namespace System.Threading.Timers namespace

查看:96
本文介绍了替换命名空间System.Threading.Timers命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我正在使用使用System.Timers.Timer命名空间的代码,如下所示:


Dear All,

I am using the code Using System.Timers.Timer namespace as you can see below:


public partial class FilePooling : ServiceBase
    {
        #region Declarations
        private System.ComponentModel.Container components = null;
        private System.ServiceProcess.ServiceController serviceController1;        
        private System.Timers.Timer timer1;
        DataTable dtHub = new DataTable();
        DataTable dtImage = new DataTable();        
        #endregion
        public FilePooling()
        {
            InitializeComponent();
            this.ServiceName = "FilePooling";
        }
        static void Main()
        {
            System.ServiceProcess.ServiceBase[] ServicesToRun;
            ServicesToRun = new System.ServiceProcess.ServiceBase[] { new FilePooling() };
            System.ServiceProcess.ServiceBase.Run(ServicesToRun);
        }
        private void InitializeComponent()
        {
            this.serviceController1 = new System.ServiceProcess.ServiceController();
            this.timer1 = new System.Timers.Timer();
            ((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
            this.timer1.AutoReset = false;
            this.timer1.Enabled = true;
            this.timer1.Interval = Convert.ToDouble(ConfigurationSettings.AppSettings["TimerInterval"].ToString());
            this.timer1.Elapsed += new System.Timers.ElapsedEventHandler this.timer1_Elapsed);
            ((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();
        }

        # region timer1_Elapsed
        private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {            
            timer1.Enabled = true;                      
            FileFTPCopy();           
            timer1.Enabled = true;            
        }
}



我想将其替换为Using System.Threading.Timers命名空间.我需要删除System.Timers.Timer.请任何人建议我或编辑该文件,如果psbl .....


问候,
Raj



I want to replace the same with Using System.Threading.Timers namespace. I need to remove the System.Timers.Timer. Plz any one suggest me or edit this if psbl.....


Regards,
Raj

推荐答案

为什么?对于Timer对象,它们存在于多个名称空间中.当使用Timer对象时,这几乎迫使您使用完全限定的名称空间.我的建议是,如果该代码有效,请不要理会.

如果您拒绝该建议,则可以为命名空间添加别名,以减少输入的次数,但会在一定程度上混淆您的代码.

Why? In the case of Timer objects, they exist in more than one namespace. This almost forces you to use a fully qualified namespace when using Timer objects. My advice is that if the code works, leave it alone.

If you resist that advice, you could alias the namespace so that it takes less typing, but that obfuscates your code to a certain degree.

using ThreadTimer = System.Threading.Timers;



在那时,您可以更改以下内容:



At that point you can change this:

System.Threading.Timers.Timer myTimer = new System.Threading.Timers.Timer();



对此:



to this:

ThreadTimer.Timer myTimer = new ThreadTimer.Timer();


使用任何计时器,尤其是Windows服务中的计时器都非常糟糕.如果您创建一些线程(需要什么时间,进行一些轮询?)并使用System.Threading.Thread.Sleep,您的麻烦就会大大减少.

—SA
Using any timer, especially in the Windows Service is pretty bad. You will have much less trouble if your create some thread (what you need to time, some polling?) and use System.Threading.Thread.Sleep.

—SA


回答后续问题:如果替代方法正在使用线程,则如何使用它.
有很多要了解的.
请阅读我对类似问题的一些答案.抱歉,很多与服务无关的信息.首先,甚至不要玩写没有线程的真实Windows Service的想法.

这些技术高度依赖于服务的目的.例如,请参见通过TCP与多个客户端(可以视为订户)进行服务接口的框架:
来自同一端口号的多个客户端 [如何获取keydown事件以在vb.net中的其他线程上进行操作 [如何将ref参数传递给线程 [ ^ ].

对于线程间调用(这很重要,因为预定义的机制仅存在于UI中,而服务中不可用),请参阅我的提示/技巧文章,其中包含代码示例:
Answering a follow-up Question: if alternative is using threads, how to use it.

There is a lot to know about it.
Please read some of my Answers to similar Question. Sorry, a lot of information irrelevant to Services. To start with, stop even playing with the idea writing a real Windows Service without threading.

The techniques highly depend on the purpose of the Service. For an example, see a skeleton of Service interfacing via TCP with multiple client which can be considered as subscribers:
Multple clients from same port Number[^].

More Answers on threading are collected here: How to get a keydown event to operate on a different thread in vb.net[^].

Very helpful and simple thread wrapper I develop and use, see also discussion:
How to pass ref parameter to the thread[^].

For inter-thread invocation (which is very important, because the predefined mechanism only exists in UI, which is not available in a Service), see my Tips/Tricks article, with code samples: Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

The whole topic is too big to overview it all, especially for the novice. Hope this matter will give you a good ideas on what''s involved.

—SA


这篇关于替换命名空间System.Threading.Timers命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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