C# 处理信息时的表格冻结

C# 处理信息时的表格冻结,c#,winforms,web-scraping,C#,Winforms,Web Scraping,我为自己写了一个个人网页刮板,用来刮除艺术家的信息。代码可以工作,但当我按下按钮并开始处理while循环时,GUI冻结。我要更新文本框。但是我不能移动表单,取消程序的唯一方法就是强制退出。我正在重写这个,所以我没有这个问题。另外,我听说过踩踏,想看看这是否有效,并且让它更快一点。程序正在刮取15000多页,然后每一页都有大约10页需要刮取。因此,程序在最终完成之前可以运行数小时 这是我的密码 private void btnGet_Click(object sender, Eve

我为自己写了一个个人网页刮板,用来刮除艺术家的信息。代码可以工作,但当我按下按钮并开始处理while循环时,GUI冻结。我要更新文本框。但是我不能移动表单,取消程序的唯一方法就是强制退出。我正在重写这个,所以我没有这个问题。另外,我听说过踩踏,想看看这是否有效,并且让它更快一点。程序正在刮取15000多页,然后每一页都有大约10页需要刮取。因此,程序在最终完成之前可以运行数小时

这是我的密码

        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's 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's 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's ID
            string albumID = url.Substring(url.IndexOf("=") + 1);
            txbAlbumID.Text = albumID;

            //Gets Album's 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++;
            
        }
任何信息都会有很大帮助。 谢谢 Throdne

当我按下按钮,它开始处理while循环时,GUI冻结

是的,这是因为您违反了Windows窗体、WPF和Silverlight的黄金规则之一:

不要在UI线程上做很多工作

UI线程用于处理事件、更新显示等。当您开始使用SQL查询等来阻止它时,它无法完成其工作

另一条金科玉律是,除了在UI线程上,不要触摸UI控件


有很多方法可以解决这个问题,包括BackgroundWorker。更多信息,请参见Joe Albahari's。基本上,这个想法是在后台线程上执行非UI工作,并在需要更新UI时封送回UI线程。

多线程确实是问题的解决方案。这里发生的情况是,处理在GUI线程上启动,所有内容都冻结,直到循环完成处理

多线程的实现将取决于您的框架和需求,但是如果您使用.NET4.0,您可能需要查看TPL库


除此之外,只需在谷歌上搜索一下mutli线程,你就能马上找到你想去的地方。

很高兴知道。谢谢你的信息。这真的很有帮助。快速提问,对于编程来说还是一个新问题,treading和BackgroundWorker是同一件事还是BackgroundWorker是treading的一个更简单的版本?BackgroundWorker是实现线程的一种方法。我建议你使用它,因为它是最简单的方法,特别是如果你是编程新手,谢谢你的帖子。我还是个编程新手。我想我该走的路是踩踏板。是的,我正在使用.NET4.0。