Click here to Skip to main content
15,909,530 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;            
        }
}


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
Posted
Updated 23-Mar-11 23:49pm
v3

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.

C#
using ThreadTimer = System.Threading.Timers;


At that point you can change this:

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


to this:

C#
ThreadTimer.Timer myTimer = new ThreadTimer.Timer();
 
Share this answer
 
v2
Comments
Raj.rcr 24-Mar-11 2:11am    
I tried the code that u have posted to replace.. Its not working. Plz give me some other idea..
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
 
Share this answer
 
Comments
Raj.rcr 24-Mar-11 1:30am    
Actually, here I am working on windows service with Timers namespace as I have posted above. But I think, I am given the task to remove System.Timers namespace and Use Thread instead. I have no idea on this.. how to do?
Sergey Alexandrovich Kryukov 24-Mar-11 2:35am    
This is a big topic. Please see my second Answer.
--SA
Raj.rcr 24-Mar-11 1:45am    
how to use Thread.Sleep ? If it is required to use in the code that I have posted, then it vl b helpful for me.
Sergey Alexandrovich Kryukov 24-Mar-11 2:34am    
Just call it. Naturally, it's a blocking call, so you call it in a separate thread, for example if this thread is supposed to take periodic action. Do some operation and sleep, in cycle.
--SA
Espen Harlinn 24-Mar-11 3:53am    
My 5
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
 
Share this answer
 
Comments
Espen Harlinn 24-Mar-11 3:53am    
Good effort, my 5
Sergey Alexandrovich Kryukov 24-Mar-11 3:58am    
Thank you, Espen.
--SA
Raj.rcr 24-Mar-11 5:49am    
Hi SA, Thankx for the links.. I have gone through, but I am unable to convert from System.Timers to Threading.Timers... I am having an issue in InitializeComponent() method.. Can u plz edit my code?
Sergey Alexandrovich Kryukov 24-Mar-11 12:13pm    
Sorry 1) I don't have time for that; 2) I think it makes no sense, so you should not do it, too; 3) your code is not editable; it means you need brand new code.

Please understand: writing a Windows Service is a serious task taking considerable amount of time. It is much more than a fragment of code you show. It is not possible to develop without all the rest, including installers.

Please understand, who will be interesting in writing any code on your request?
You can only expect advice, but fix, illustrated codelets, and only if someone is interested to do so for you.

Sorry,
--SA
Raj.rcr 24-Mar-11 14:27pm    
Its ok SA.. I can understand.. anyways, thank u so much for the kind reply as well as the way u tried to explain the solution.. I will try it agn and agn until i get the solution for this.
cya

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900