Can't seem to make URL request from ElapsedEventHandler

  • #1

Main Question or Discussion Point

I've spent 10+ hours on trying to solve this and can't get it to work!

In short, I have a method

Code:
        private async void LoadAndSavePage(object source, ElapsedEventArgs e)
        {
             // ... 
        }
which is being called every 1 second when the Windows Service that contains it is running. I've confirmed that it is indeed being called; I can verify with

Code:
        private async void LoadAndSavePage(object source, ElapsedEventArgs e)
        {
            EventLog.WriteEntry("I got called!");
        }
However, as soon as I try to get a web page, for example

Code:
        private async void LoadAndSavePage(object source, ElapsedEventArgs e)
        {
            string html = new WebClient().DownloadString("[PLAIN]http://physicsforums.com");[/PLAIN] [Broken]
            EventLog.WriteEntry(html);
        }
it doesn't work. The function gets called as scheduled but it never gets to the point where it writes to the event log. Something about introducing a web request messes it up. I'm guessing maybe some type of permissions issue?

I've tried with several libraries including .NET's WebClient and HtmlAgilityPack's HtmlWeb, I've tried with different URLs, etc.

I know it doesn't have to do with the event having a maximum resolution time, for I've also tried

Code:
        private async void LoadAndSavePage(object source, ElapsedEventArgs e)
        {
            EventLog.WriteEntry("before sleep");
            Thread.Sleep(1500);
            EventLog.WriteEntry("after sleep");
        }
and confirmed that the whole thing runs.
 
Last edited by a moderator:

Answers and Replies

  • #3
33,075
4,779
However, as soon as I try to get a web page, for example

Code:
        private async void LoadAndSavePage(object source, ElapsedEventArgs e)
        {
            string html = new WebClient().DownloadString("[PLAIN]http://physicsforums.com");[/PLAIN] [Broken]
            EventLog.WriteEntry(html);
        }
it doesn't work.

The function gets called as scheduled but it never gets to the point where it writes to the event log. Something about introducing a web request messes it up. I'm guessing maybe some type of permissions issue?
That would be my guess, but that's just a guess. In your following post, you suggest running the service as a NetworkService. I would give that a shot.
 
Last edited by a moderator:

Related Threads for: Can't seem to make URL request from ElapsedEventHandler

Replies
6
Views
3K
Replies
15
Views
905
Replies
4
Views
2K
Replies
10
Views
6K
Replies
1
Views
695
Replies
7
Views
1K
Top