sophisticated timer control in asp.net

Hi.
I have an asp.net application running normally at lan. Sometimes there is internet connection and sometimes dont. Because of this, I receive some files to import or from internet or manually placed into a local folder.

to handle this, I created an httpmodule. after a interval time (configured) if there is a local file, I process it and check if there is connection. If there is, I check if there is some file to download. If there is, I do that and store it in the same folder. After the next interval, the process continues.

so. everything is working fine, except the timer. all the timers that I use do not stop the counter. even when stopped, the events are queued.
The best timer was the System.Diagnostics.StopWatch. however, how handle the interval? thread.sleep() no please.

thx for any help
Deraldo SilvaAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Miguel OzConnect With a Mentor Software EngineerCommented:
That is very odd, please make sure you are not calling timer.Start or the event itself  in multiple places  or creating a timer multiple times.

My main question will be if you need to build this as an httpmodule, if you only need to schedule the execution every hour when the site is running then you are better off using a windows service or if  your site needs to be self contained you could use WebBackgrounder. There is an interesting discussion and recommendations in this link
Note: The WebBackgrounder internally uses a Threading.Timer.

For further help, please post your http module code as well as how you configure in web.config, OS/VS/.NET version details.
0
 
Ammar GaffarSoftware EngineerCommented:
Why you are implementing this functionality in ASP.NET?

The purpose of this functionality is to sync with specific folder, I do recommend to implement it as a Windows Service using FileSystemWatcher to monitor any added files to your configured folder and execute the rest of your code, in this case you don't even need the timer because FileSystemWatcher has Created event.

Good Luck
0
 
Deraldo SilvaAuthor Commented:
Hi Ammar.
Thx for your attention.

well. I have two reasons.
1. is not only to synchronize with a folder. If the internet connection is present, then I can get the records from it. I only realized that if I download the records as file from the internet site, I have only one process to manage.
2. the sites could be remote, hard to manage. an asp.net application has a simple instalation .

regards
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
käµfm³d 👽Commented:
StopWatch isn't a timer; rather it's a chronograph. Timers count down to some interval; chronographs count up until stopped.

We would probably need to see your module code to fully understand what you are doing.
0
 
Miguel OzSoftware EngineerCommented:
Please answer questions below:
Q1. What kind of timers have you used?
Q2. What do you mean by "all the timers that I use do not stop the counter"?
Q3. Have you used Threading.Timer or Timers.Timer
Q4. is the execution of your timer callback > timer interval? If so, you may need to synchronize or disable the timer until execution is done as discussed here.
0
 
Deraldo SilvaAuthor Commented:
Ok.
as attached some pictures about the problem.
I will try to disable the timer, using the duetime in system.threading. Am I right?
timerSnapshot.docx
0
 
käµfm³d 👽Commented:
I suppose that while the process is stopped at line 123, the timer count still goes on.
It shouldn't be--you set the timer's AutoReset to false. Is the File.Exists code in the timer_Elapsed handler? Are you sure you're not calling that same method elsewhere?
0
 
Deraldo SilvaAuthor Commented:
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            DataTable dt;
            byte[] b1 = null;

            // check if there is a xlsx file in "diretorio"
 
            string arquivo = diretorio + "\\BBBMatUnidadesDeNegocio.xlsx";
            if (File.Exists(arquivo))
            {
                dt = new DAL().Get_ExcelSheet(arquivo);
                foreach (DataRow dr in dt.Rows)
                {
                    using (var nucleo = new Nucleo())
                    {
                        var bo = new UnidadeDeNegociosBo(nucleo);
                        UnidadesDeNegocio unidade = new UnidadesDeNegocio();
                        unidade.UnidadeDeNegocioID = dr["CODIGO"].ToString();
                        unidade.Descricao = dr["DESCRICAO"].ToString();
                        try
                        {
                            bo.IncluirUnidadeDeNegocio(unidade);
                        }
                        catch (AggregateException ex)
                        {
                            nucleo.Undo();
                        }
                    }
                }
                File.Delete(arquivo);
            }
            b1 = receiveFile("BBBMatUnidadesDeNegocio.xlsx");
            if (b1 != null)
            {
                // get the file from web service
                FileStream fs1 = new FileStream(arquivo, FileMode.Create);
                fs1.Write(b1, 0, b1.Length);
                fs1.Close();
                fs1 = null;
            }
            timer.Start();
        }
0
 
käµfm³d 👽Commented:
Why do you have timer.Start as the last line of the event handler? That's why your timer fires more than once.
0
 
Deraldo SilvaAuthor Commented:
Hi.
I took off the timer.Start on the last line and the problem still the same.
0
 
Miguel OzSoftware EngineerCommented:
I think you have a re-entrant problem in your elapsed method (your timer is firing the elapsed method again because the interval < elapsed execution time); to solve it, you need to disable timer, run your code and then enable timer again as shown below: (A try finally for final production code is recommended)
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
  timer.Enable = false; // or timer. Stop();
  //your code goes here
  timer.Enable = true; //timer.Start();
}

The only issue is that the timer will no longer fires at exactly the specified configuration interval but the new elapsed time = specified configuration interval  + elapsed execution time.
If your problem is only debugging (as described in doco) then you need code above only for debugging purposes unless you may face big execution times in production.
0
 
Deraldo SilvaAuthor Commented:
Sorry Miguel.
this did not work either. still the same problem.
the interval not have to be regular. In production will be for example, each hour.
0
 
Deraldo SilvaAuthor Commented:
Hey. it works fine if I run it without debug.
only with debug it duplicate the records.
0
 
Miguel OzSoftware EngineerCommented:
How are you declaring timer variable?
It should be declared private static and instantiated in a singleton pattern as shown here.
0
 
Deraldo SilvaAuthor Commented:
I put the static. Did not work eiher.
I already used the lock condition. did not work.
you think I need to try this again?
thx in advance.
0
 
Deraldo SilvaAuthor Commented:
Hi Miguel.
Send me your email and I send the code to you.

Well. Now I am thinking in other solution than httpmodule. Before all this oddities(as you said) , the httpmodule looks a great solution.

But if the debug problem is relative to timer, will be good know the solution isnt?

regards
0
 
Deraldo SilvaAuthor Commented:
I change the project to windows service and everything is working fine.
thx for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.