C # Form freezes while processing information.

I wrote my own web scraper for myself that scrapes information about the artist. the code works, but when I click the button and run the while loop, the GUI freezes up. I got text fields in .refresh (). But I cannot move the form, also the only way to cancel the program is to force me to quit smoking. I am rewriting this now, so I don't understand the problem. Also, I heard about how to step and wanted to see if that would work and also make it a little faster. the program breaks 15,000+ pages and then on each page there are 10 or so more pages to be undone. Thus, the program can run for several hours before it finally ends.

Here is my code.

        private void btnGet_Click(object sender, EventArgs e)
    {
        int i = 0;
        int maxCount = 15000; //11234 was last value 

        progressBar.Maximum = maxCount;

        while (i <= maxCount)
        {
            txbURL.Text = "http://www.newreleasetuesday.com/albumdetail.php?album_id=" + i;
            label.Text = i.ToString() + " out of " + maxCount.ToString() + " Done.";
            progressBar.Value = i;

            string url = txbURL.Text;

            string sourceCode = sourceCode = WorkerClass.getSourceCode(url);
            int startIndex = sourceCode.IndexOf("//alert(document.getElementById(\"remcheck\").value)");
            sourceCode = sourceCode.Substring(startIndex, sourceCode.Length - startIndex);

            //Start Artist Name
            //Gets the Artist ID
            int idCountIndex = sourceCode.IndexOf("  by <a href=\"artistdetail.php?artist_id=") + 41;
            int idCountEndIndex = sourceCode.IndexOf("\">", idCountIndex);
            string artistID = sourceCode.Substring(idCountIndex, idCountEndIndex - idCountIndex) + "";
            txbArtistID.Text = artistID;

            //Gets Artist Name
            startIndex = sourceCode.IndexOf("  by <a href=\"artistdetail.php?artist_id=") + 43 + artistID.Length;
            int endIndex = sourceCode.IndexOf("</a> | Genre", startIndex);
            string artistName = sourceCode.Substring(startIndex, endIndex - startIndex) + "";
            txbArtist.Text = artistName;
            //End Artist Name

            //Start Album Name
            //Gets Album ID
            string albumID = url.Substring(url.IndexOf("=") + 1);
            txbAlbumID.Text = albumID;

            //Gets Album Name
            startIndex = sourceCode.IndexOf("absbottom\"></span></strong> ") + 28;
            endIndex = sourceCode.IndexOf("</span></td>", startIndex);
            string AlbumName = sourceCode.Substring(startIndex, endIndex - startIndex) + "";
            txbAlbum.Text = AlbumName;
            //End Album Name

            //Start Genre
            startIndex = sourceCode.IndexOf("</a> | Genre: ") + 14;
            endIndex = sourceCode.IndexOf(" | ", startIndex);
            string genre = sourceCode.Substring(startIndex, endIndex - startIndex) + "";
            txbGenre.Text = genre;
            //End Genre

            //Start Release Date
            startIndex = sourceCode.IndexOf("<a href=\"releasedate.php?release_date=") + 50;
            endIndex = sourceCode.IndexOf(" </a></td>", startIndex);
            string releaseDate = sourceCode.Substring(startIndex, endIndex - startIndex) + "";
            txbReleaseDate.Text = releaseDate;
            //End Release Date

            //Start Pic URL
            startIndex = sourceCode.IndexOf("<img  src=\"") + 11;
            endIndex = sourceCode.IndexOf("\"  alt=", startIndex);
            string PicURL = sourceCode.Substring(startIndex, endIndex - startIndex) + "";
            PicURL = PicURL.Replace("amp;", "");
            string fullLink = "http://www.newreleasetuesday.com/" + PicURL;
            txbPicURL.Text = fullLink;
            //End Pic URL


            //Refresh UI (Updates textBoxes, labels, and progressBar with new values)
            txbURL.Refresh();
            txbArtist.Refresh();
            txbAlbum.Refresh();
            txbReleaseDate.Refresh();
            txbGenre.Refresh();
            txbPicURL.Refresh();
            txbArtistID.Refresh();
            txbAlbumID.Refresh();
            label.Refresh();
            progressBar.Refresh();

            if (artistName == "")
            {
                // Adding info to Database if there is no artist name
                string cs = "SERVER=asdf.net;" +
                    "DATABASE=music;" +
                    "UID=root;" +
                    "PASSWORD=asdf;";

                MySqlConnection conn = null;

                conn = new MySqlConnection(cs);
                conn.Open();

                MySqlCommand cmd = new MySqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = "INSERT INTO `emptyalbums` (id, albumid) VALUES('',@albumid)";
                cmd.Prepare();

                cmd.Parameters.AddWithValue("@albumid", albumID);

                cmd.ExecuteNonQuery();
                conn.Close();
            }
            else
            {
                // Adding info to Database if there is an artist name
                string cs = "SERVER=asdf.net;" +
                    "DATABASE=music;" +
                    "UID=root;" +
                    "PASSWORD=asdf;";

                MySqlConnection conn = null;

                conn = new MySqlConnection(cs);
                conn.Open();

                MySqlCommand cmd = new MySqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = "INSERT INTO `database` (id, artist, album, releasedate, genre, pictureurl, artistid, albumid) VALUES('',@artist, @album, @releasedate, @genre, @pictureurl, @artistid, @albumid)";
                cmd.Prepare();

                cmd.Parameters.AddWithValue("@artist", artistName);
                cmd.Parameters.AddWithValue("@album", AlbumName);
                cmd.Parameters.AddWithValue("@releasedate", releaseDate);
                cmd.Parameters.AddWithValue("@genre", genre);
                cmd.Parameters.AddWithValue("@pictureurl", fullLink);
                cmd.Parameters.AddWithValue("@artistid", artistID);
                cmd.Parameters.AddWithValue("@albumid", albumID);

                cmd.ExecuteNonQuery();
                conn.Close();
            }
            i++;

        }

      

Any information has come a long way. Thanks, Throdne

+3


source to share


2 answers


Multithreading is really the solution to your problem. What is happening here is that the cure starts on your graphics thread and everything hangs until the loop finishes processing.

The multithreading implementation will depend on your structure and your needs, but if you are using .Net 4.0 you might want to check out the TPL library.



http://msdn.microsoft.com/en-us/library/dd460717.aspx

Also, a simple google search about mutli-threading will get you to where you want to be in no time.

+5


source


when i click the button and start the while loop the GUI freezes

Yes, that's because you're breaking one of the golden rules of Windows Forms (both WPF and Silverlight):

Don't do a lot of work with the UI thread.



The UI thread is for handling events, refreshing the display, etc. When you start blocking it with SQL queries and the like, it cannot do its job.

(Another golden rule is not to touch UI controls other than the UI thread.)

There are many ways to get around this, including BackgroundWorker

. See Joe Albahari's threading guide for details . Basically, the idea is to do non-UI work on a background thread and marshal back to the UI thread when you need to update the UI.

+5


source







All Articles