[C#] Program Putting Window To Sleep

tom982

Emeritus
Joined
May 31, 2012
Posts
4,351
Location
New York
Hello all,

I have a really annoying issue with a little app I've written and I have absolutely no idea what's causing it. The app is to check the results in an online competition which doesn't list the results; instead it has the number of votes each contestant has on their entrant page but my app grabs all the links from the list of contestants, then grabs the data from their page and writes it to a textbox. Here's the code:

Code:
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            string webaddress = "";
            string name = "";
            string votes = "";
            string line = "";

            using (WebClient client = new WebClient())
            {
                client.DownloadFile(@"http://www.mobilityawarenessmonth.com/local-heroes/?icp=1&ihpp=3900", @"C:\\NMEDA\\list.html");
            }

            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.Load(@"C:\\NMEDA\\list.html");
            foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
            {
                HtmlAttribute att = link.Attributes["href"];
                if (att.Value.ToString().Contains("entrant"))
                {
                    webaddress = att.Value.ToString();

                    using (WebClient client2 = new WebClient())
                    {
                        client2.DownloadFile(@webaddress, @"C:\\NMEDA\\file.html");
                    }

                    using (StreamReader reader = new StreamReader(@"C:\\NMEDA\\file.html"))
                    {
                        while ((line = reader.ReadLine()) != null)
                        {
                            if (line.Contains("votecountvotes"))
                            {
                                votes = line.ToString();
                            }
                            if (line.Contains(@"in the 2014 National Mobility Awareness Month Local Hero contest!	"))
                            {
                                name = line.ToString();
                            }
                        }
                    }

                    // Tidy Votes

                    votes = votes.Trim();

                    string[] v = votes.Split('<');
                    votes = v[0].ToString();
                    votes = votes.Replace(",", "");

                    // Tidy entrant name

                    name = name.Trim();

                    string[] ent = name.Split('-');
                    name = ent[0].ToString();
                    name = name.Replace("Vote for ", "");

                    txtResults.AppendText(votes + " | " + name + " | " + webaddress + "\r\n");
                }
            }

                doc.Save("C:\\NMEDA\\list.html");
        }
    }
}

Now, the issue I'm facing is that when this app is running (it's currently in the VS debugger and I haven't tested it running without a debugger attached yet) it puts my screen to sleep instantly when the window is in focus. The minute it is minimised, the problem goes away. If I keep my mouse moving, I can see the form but the minute I stop my mouse, off goes the screen.

Any ideas what's causing this?

Thanks,

Tom

P.S. I know it's not the neatest or most efficient code in the world - I haven't been learning C# for long :)
 
Thanks for the help guys! Well I'm stumped, it's not happening any more. I haven't changed anything, but it's working fine now. Something I forgot to add in my first post: this only happened when the app was grabbing the data; the second it finished, it stopped putting my screen to sleep.

What does your form look like? I don't see anything here... Have you tried using a different monitor?

I've only got one monitor with me at the moment so I can't try another I'm afraid, but I've attached the project.

Could you possibly upload the entire project & a compiled binary too?

Sure, I've attached it all :)
 

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top