Two days ago, I wrote a regular weather reportProgramThere are always such bugs and sometimes dos errors. I found some information on the Internet and found that there was a certain problem with my fixed-point sending. Now I will summarize my experience and discuss it with you.
First, we need to use the event in global. asax to start our task in application_start and stop our task in application_end. Of course, to start our task, we must start a thread to complete the corresponding work.
At the beginning, what I do in the thread is to judge every MS whether the current event is equal to the specified time. If it is equal to, the corresponding task will be executed, however, this is prone to frequent access and excessive CPU resources.
Later, I thought of the following function to calculate the number of milliseconds from the current time to the specified time:
Private int getsleeptime (INT hour, int min, int second)
{
Datetime now = datetime. now;
Int sleep = (hour-Now. Hour) * 60 + (min-Now. Minute) * 60 + (second-Now. Second) * 1000;
If (sleep <0)
Sleep + = 24*3600*1000;
Return sleep;
}
In this way, you can directly sleep to the specified event point and then execute our task.
In order to be able to think about stopping in the middle, sleep is not very convenient, so I decided to use wait. The specific thread method is as follows:
Private void run ()
{
While (isstart)
{
Int sleep = getsleeptime (19, 0, 0 );
Lock (locker)
{
Monitor. Wait (locker, sleep );
}
If (isstart)
Sendweatherreport (); // This is the job I want to accomplish at a specified point
}
}
To stop the service midway through, we call the following method:
Public void stop ()
{
Isstart = false;
Lock (locker)
{
Monitor. pulseall (locker );
}
}
In this way, our goal will be reached. However, I found that I still couldn't receive the weather forecast information the next morning. I checked it online and found that the ASP. Ne program will be recycled after a while without being accessed. On the Internet, we also found a relatively cumbersome solution to find other non-trivial solutions, but we have not yet found any, or some of them are not feasible.
In the application_end event, access our program and reactivate the application.
Void application_end (Object sender, eventargs E)
{
// Run when the application is closed Code
Job. Stop ();
String url = "http: // locahost/weatherreport/default. aspx"; // specify the address of your Asp.net program here
Httpwebrequest myhttpwebrequest = NULL;
Httpwebresponse myhttpwebresponse = NULL;
Streamreader sr = NULL;
Stream receivestream;
String STR = NULL;
Try
{
Myhttpwebrequest = (httpwebrequest) webrequest. Create (URL );
Myhttpwebresponse = (httpwebresponse) myhttpwebrequest. getresponse ();
Receivestream = myhttpwebresponse. getresponsestream ();
Sr = new streamreader (receivestream, encoding. utf8 );
STR = Sr. readtoend (); // This statement does not seem to work, otherwise it cannot be activated.
}
Catch
{
}
Finally
{
If (SR! = NULL)
Sr. Close ();
If (myhttpwebresponse! = NULL)
Myhttpwebresponse. Close ();
}
}
The conclusion is so much that we should complete the settings in the next time.