Solved

sophisticated timer control in asp.net

Posted on 2014-12-21
18
127 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 75

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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
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 75

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 75

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
 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

820 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