.net sukbir

Dear All,

I have set a timer for my windows service. The problem is that when I start my windows service it writes to a file and I find that it just wrote once that is when the service starts. I set the interval to 10000 that is 10s. So what is the possible problem here any help please



Re: Windows Forms Data Controls and Databinding Timer problem with windows service

Andrej Tozon

Hi,

what kind of timer are you using Can you show us some code where you're setting the timer please

Andrej






Re: Windows Forms Data Controls and Databinding Timer problem with windows service

.net sukbir

Dear Andrej,

Below is my sample code. Well about the timer I go to the designer view of my code and drag a timer component and set its interval to 10000. So can you tell me where is my mistake. In fact I was reading some where in the web it was saying that timer is not safe and threading is safer I dont really have any idea into this as I am very new to all this. Hope you can guide me Thanks man.
namespace WindowsService1
{
public partial class Service1 : ServiceBase
{

public Service1()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{

StreamWriter st = new StreamWriter("C:\\date1.txt", true, Encoding.Unicode);
st.Write("\n"+DateTime.Now+"\n");
st.Close();


}

protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
}


}
}





Re: Windows Forms Data Controls and Databinding Timer problem with windows service

Andrej Tozon

Yes, that timer won't work, using Threading.Timer would be better. See my second post in this thread for a sample on how to use it.

Hope it helps,

Andrej






Re: Windows Forms Data Controls and Databinding Timer problem with windows service

.net sukbir

Dear Andrej,

I have seen your thread. Why is it that using threading.timer is better So how can I b sure that is wont break after some hours or days I guess I should be doing a log rite. So if I want to control by service to run just between 10 am and 6 pm then I guess I must do a check in the function below rite

private void OnTick(object stateObject)
{
// Don't let service crash
try
{

if(time is between 10 am n 6 pm)

{
// Do your stuff here
}

}
catch (Exception
ex)
{
try
{
this.EventLog.WriteEntry(ex.Message, EventLogEntryType.Error);
}
catch (Exception exx)
{
// Bad luck, handle it differently or rethrow original exception
}
}
}
}





Re: Windows Forms Data Controls and Databinding Timer problem with windows service

Andrej Tozon

Windows Forms Timer won't work at all with services... Threading.Timer will always use a worker thread and is considered the most reliable. Logging your actions is always a good idea, Windows Service already provides a log source for you. And you're correct about time checking...

Andrej






Re: Windows Forms Data Controls and Databinding Timer problem with windows service

.net sukbir

Dear Andrej,

Actually after doing some depth thought I find that using service i cant control to run my service every interval of 30 minutes between say 10 am to 6pm I cant specify to run say 10, 10.30 11, 11.30 etc. This is because it all depends wat time I stary my pc rite. Because if I set the timer interval as 30minutes and start my pc at 10.10 am then obiviously the next time the service will run is 10.40 rite Actually what is the worker thread Each time my service run for interval is one thread is it Will this cause a problem in the long run





Re: Windows Forms Data Controls and Databinding Timer problem with windows service

Andrej Tozon

How about you set the timer to fire once every minute (or so) and then determine if it's the right time to do something Of course your service should "remember" the time it last executed that "something".

Worker thread is a non-UI thread, used for executing tasks "behind" the UI, which is important when using it with Windows Service [also not providing UI]. You'll have to ensure your worker method executes and exits before it's called again by the timer [or disable timer for the time this method is executing].

Andrej






Re: Windows Forms Data Controls and Databinding Timer problem with windows service

.net sukbir

Dear Andrej,

Ya my problem now is like u said "You'll have to ensure your worker method executes and exits before it's called again by the timer [or disable timer for the time this method is executing]". How can I ensure this Thanks.





Re: Windows Forms Data Controls and Databinding Timer problem with windows service

.net sukbir

Dear Andrej,

Your example of timerDelegate = new TimerCallback(OnTick);
stateTimer =
new Timer(timerDelegate, null, 0, 5000); // 5000 = 5 secs . I am a bit lost when now comes to threadig and things like that Can you give me some explaination on that Thanks man.






Re: Windows Forms Data Controls and Databinding Timer problem with windows service

Andrej Tozon

.net sukbir wrote:
Ya my problem now is like u said "You'll have to ensure your worker method executes and exits before it's called again by the timer [or disable timer for the time this method is executing]". How can I ensure this Thanks.

You could either make the timer interval long enough for your code execute completely in that time, or make the timer call your function only once (no consecutive calls) and reset your timer by calling its Change() method.

Andrej






Re: Windows Forms Data Controls and Databinding Timer problem with windows service

Andrej Tozon

.net sukbir wrote:

timerDelegate = new TimerCallback(OnTick);
stateTimer =
new Timer(timerDelegate, null, 0, 5000);

The above code creates a new TimerCallback (a reference to the method called OnTick), which will be invoked when timer fires.

The first number (0) in the Timer constructor specifies the number of milliseconds, after which the timer will be first called. The second number specifies the interval (again, in milliseconds) for each consecutive call.

Andrej






Re: Windows Forms Data Controls and Databinding Timer problem with windows service

.net sukbir

Dear Andrej,

What do you mean by "make the timer call your function only once (no consecutive calls) and reset your timer by calling its Change() method". The zero in the timer constructor is nothing much rite. Thanks man.





Re: Windows Forms Data Controls and Databinding Timer problem with windows service

Andrej Tozon

The zero in the constructor means that function is called immediately (no waiting). If you'd pass a value of 1000 instead of 0, the function would be called after one second (1000 ms).

What I meant was you could implement it like this:

1. You set the timer to fire only once, after, let's say, 5 minutes (300000, 0).
2. When you're done with checking the time and / or processing, re-set the timer to either fire again in 5 seconds, or maybe in one hour. You'd use timer's Change() method to set the new values (the same as you passed in the constructor).

Andrej