处理信息时,C#形式冻结。 [英] C# Form freeze when processing information.
问题描述
我写了一个个人网站刮板为自己的下脚料艺术家信息。代码工作,但是当我按下按钮,它开始处理while循环,在GUI冻结。我得到的文本框来.REFRESH()。但我不能移动的形式,也取消该计划的唯一办法是强制退出。我在此改写,所以我没有得到这个问题的过程。另外,我听说了踩水,想看看是否能够工作,也让它更快一点。程序被报废15,000页,然后将每个页面具有它需要报废另一个10个左右的页面。所以,在它终于完成了程序可以运行几个小时。
I wrote a personal web scrapper for myself that scraps artist information. the code works but when I press the button and it start processing the while loop, the GUI freezes. I got the textBoxes to .refresh(). But I can't move the form, also the only way to cancel the program is to force quit. I'm in the process of rewriting this so I don't get this problem. also, I heard about treading and wanted to see if that would work, and also make it a little faster. the program is scrapping 15,000+ pages, and then each page has another 10 or so pages it needs to scrap. So, the program could run for hours before it's finally finished.
下面是我的代码。
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
Any info would go a long way. Thanks, Throdne
推荐答案
多线程的确是解决你的问题。这里发生的是,在治疗上的GUI线程启动,一切都冻结,直到你的循环已处理完毕。
Multi-threading is indeed the solution to your problem. What happens here is that the treatment is launched on your GUI Thread and everything freezes until your loop has finished processing.
多线程的实施将取决于你的框架和您的需求,但如果你使用.NET 4.0,你可能要检查出TPL库。
The implementation of multi-threading will depend on your framework and your needs, but if you use .Net 4.0 you might want to check out the TPL library.
的 http://msdn.microsoft.com/en-us/library/dd460717.aspx
其他重要的是,要在任何时间约辑阵线程一个简单的谷歌搜索会得到你。
Other than that, a simple google search about mutli-threading will get you where you want to be in no time.
这篇关于处理信息时,C#形式冻结。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!