Solved

sophisticated timer control in asp.net

Posted on 2014-12-21
18
122 Views
Last Modified: 2014-12-29
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
0
Comment
Question by:Deraldo Silva
  • 9
  • 4
  • 3
  • +1
18 Comments
 
LVL 12

Expert Comment

by:Ammar Gaffar
ID: 40511521
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
 

Author Comment

by:Deraldo Silva
ID: 40511582
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 40511929
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
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 40511953
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
 

Author Comment

by:Deraldo Silva
ID: 40511988
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 40512041
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
 

Author Comment

by:Deraldo Silva
ID: 40512084
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 40512126
Why do you have timer.Start as the last line of the event handler? That's why your timer fires more than once.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Deraldo Silva
ID: 40512134
Hi.
I took off the timer.Start on the last line and the problem still the same.
0
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 40512266
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
 

Author Comment

by:Deraldo Silva
ID: 40512486
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
 

Author Comment

by:Deraldo Silva
ID: 40512535
Hey. it works fine if I run it without debug.
only with debug it duplicate the records.
0
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 40514688
How are you declaring timer variable?
It should be declared private static and instantiated in a singleton pattern as shown here.
0
 

Author Comment

by:Deraldo Silva
ID: 40515271
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
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 40515595
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
 

Author Comment

by:Deraldo Silva
ID: 40515803
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
 

Author Closing Comment

by:Deraldo Silva
ID: 40521557
I change the project to windows service and everything is working fine.
thx for your help.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now