Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How a web application C# code keeps on running a while loop for every minute?

Posted on 2008-10-30
6
706 Views
Last Modified: 2013-12-17
Hi

I built a web application to send email at regular time. Now, I use a windows service to check whether there is new email needs to be sent for every minute. So, it has a web application for me to input email address, email content, datetime to send AND a windows service program to have a timer to check MSSQL regularly to send email.

Now, I want to combine them into one web or desktop application. How can the while loop in C# code can execute code for every minute?

Thanks for advice.
0
Comment
Question by:techques
  • 3
  • 2
6 Comments
 
LVL 1

Assisted Solution

by:VegasCid
VegasCid earned 50 total points
ID: 22841173

You can use timer class

reference       http://www.developer.com/lang/other/article.php/792901

1:  //  Timer02.cs - Displaying Date and Time
 2:  //      Using the Timer class. 
 3:  //      Press Ctrl+C to end program. 
 4:  //------------------------------------------
 5:  using System;
 6:  using System.Timers;
 7:  
 8:  class myApp
 9:  {
10:    public static void Main()
11:    {
12:      Timer myTimer = new Timer();
13:      myTimer.Elapsed += new ElapsedEventHandler( DisplayTimeEvent );
14:      myTimer.Interval = 1000;
15:      myTimer.Start();
16:  
17:      while ( Console.Read() != 'q' )
18:      {
19:          ;    // do nothing...
20:      }
21:    }
22:
23:    public static void DisplayTimeEvent( object source, ElapsedEventArgs e )
24:    {
25:        Console.Write("\r{0}", DateTime.Now);
26:    }

Open in new window

0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22841374
It is better to have a windows service to sent mail, because if you create a web application or windows application it requires that an user is logged in to the machine for this application to run but it is not the case in windows service.

What I would suggest is have a table where you have the mail ids of users to whom mail has to be sent. Change your web application to  update this table (add/remove mail ids) Your windows service should be checking this data at regular interval and sent mail.

Instead of windows service you can also create a console application that sents mail to the configured users. You can schedule this application to run in regular interval. This way you can avoid creating a windows service and the timer logics
0
 

Author Comment

by:techques
ID: 22842187
I had existing windows service program to send email. However, it is inconvenient to distribute to users as it will take much support work. So, I want to combine the client interface with the windows service program. When the user execute the program, it will run.

using System.Timers; is it for desktop application?
how about web application? how can i do it ?
while (600000)  //for each minute
{
execute the mail code
}
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 1

Accepted Solution

by:
VegasCid earned 50 total points
ID: 22842325
Yes is for desktop application.
Web enviroment need someone on client to call codebehind
-with javascript    
setTimeout ( "doSomething()", 1000 );
function doSomething(){
document.form1.submit()
}
You can read more about it in http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/
 
-with Meta-Tag
Also you can configure automatic submit with metaTag inside of head tag
<meta http-equiv="refresh" content="60">
 
You can use AjaxPro or any other AJAX framework to make this postback transparent if you need it.
 
Sorry for my English. I'm learning
0
 
LVL 1

Expert Comment

by:VegasCid
ID: 22842345
In code-Behind put your code in pageLoad section
0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22843021
A windows service cannot have user interface.
You can probably build a windows application for modifying email addresses and use the existing windows service for sending mails.

Because as I said if you are integrating mailing part with mail updating part into a windows application then an user has to be logged into the machine and make sure that the application is open so that it sends mail in regular interval
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

789 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