gpt4 book ai didi

c# - 寻找 OO 大师,在设计我的编程逻辑时需要一些帮助。没什么特别的,只是新的

转载 作者:太空宇宙 更新时间:2023-11-03 20:43:47 27 4
gpt4 key购买 nike

我会发布我的整个类(class),也许有更多经验的人可以帮助我设计更好的东西。我对异步做事真的很陌生,所以我真的迷失在这里。希望我的设计不是太糟糕。 :P

IMDB 类:

public class IMDB
{
WebClient WebClientX = new WebClient();
byte[] Buffer = null;

public string[] SearchForMovie(string SearchParameter)
{
//Format the search parameter so it forms a valid IMDB *SEARCH* url.
//From within the search website we're going to pull the actual movie
//link.
string sitesearchURL = FindURL(SearchParameter);

//Have a method download asynchronously the ENTIRE source code of the
//IMDB *search* website, and save it to the byte[] "Buffer".
WebClientX.DownloadDataCompleted += new DownloadDataCompletedEventHandler(WebClientX_DownloadDataCompleted);
WebClientX.DownloadDataAsync(new Uri(sitesearchURL));

//Convert the byte[] to a string so we can easily find the *ACTUAL*
//movie URL.
string sitesearchSource = Encoding.ASCII.GetString(Buffer);

//Pass the IMDB source code to method FindInformation() to FIND the movie
//URL.
string MovieURL = FindMovieURL(sitesearchSource);


//Download the source code from the recently found movie URL.
WebClientX.DownloadDataAsync(new Uri(MovieURL));

//Convert the source code to readable string for scraping of information.
string sitemovieSource = Encoding.ASCII.GetString(Buffer);

string[] MovieInformation = ScrapeInformation(sitemovieSource);
return MovieInformation;
}

void WebClientX_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
Buffer = e.Result;
throw new NotImplementedException();
}

/// <summary>
/// Formats a valid IMDB url for ease of use according to a search parameter.
/// </summary>
/// <param name="sitesearchSource"></param>
/// <returns></returns>
private string FindMovieURL(string sitesearchSource)
{
int Start = sitesearchSource.IndexOf("<link rel=\"canonical\" href=\"");
string IMDBCode = sitesearchSource.Substring((Start + 28), 35);

return IMDBCode;
}

private string[] ScrapeInformation(string Source)
{
string[] Information = new string[5];

Information[0] = FindTitle(Source);
Information[1] = FindDirector(Source);
Information[2] = FindYear(Source);
Information[3] = FindPlot(Source);
Information[4] = FindPoster(Source);

return Information;
}

/*************************************************************************/

private string FindURL(string Search)
{
string[] SearchArray = Search.Split(' ');
string FormattedQuery = "";
foreach (string X in SearchArray)
{
FormattedQuery += X + "+";
}

FormattedQuery.Remove((FormattedQuery.Length - 1), 1);

string TheFormattedQuery = "http://www.imdb.com/find?s=all&q=" + FormattedQuery + "&x=0&y=0";
return TheFormattedQuery;
}

private string FindTitle(string Source)
{
//<title>Couples Retreat (2009)</title>

int Start = Source.IndexOf("<title>");
string Bookmark = Source.Substring((Start + 7), 400);

int End = Bookmark.IndexOf("</title>");
string Title = Bookmark.Substring(0, End - 7);

return Title;
}

private string FindDirector(string Source)
{
int Start = Source.IndexOf("<h5>Director:</h5>");
string Bookmark = Source.Substring((Start + 18), 250);

Start = Bookmark.IndexOf(">");
Bookmark = Bookmark.Substring(Start + 1, 100);

int End = Bookmark.IndexOf("</a>");
string Director = Bookmark.Substring(0, End - 1);

return Director;
}

private string FindYear(string Source)
{
int Start = Source.IndexOf("<h5>Release Date:</h5>");
string Bookmark = Source.Substring((Start + 22), 40);

int End = Bookmark.IndexOf("<a class=");
string ReleaseYear = Bookmark.Substring(0, End - 1);

return ReleaseYear;
}

private string FindPlot(string Source)
{
int Start = Source.IndexOf("<h5>Plot:</h5>");
string Bookmark = Source.Substring((Start + 14), 700);

int End = Bookmark.IndexOf("<a class");
string Plot = Bookmark.Substring(0, End - 1);

return Plot;
}

private string FindPoster(string Source)
{
int Start = Source.IndexOf("<a name=\"poster\" href=");
string Bookmark = Source.Substring((Start + 22), 700);

Start = Bookmark.IndexOf("src=\"");
string PosterURL = Bookmark.Substring((Start + 5), 103);

return PosterURL;
}
}

Form1.cs 类(我的窗体):

public partial class MainSearchForm : Form
{
public MainSearchForm()
{
InitializeComponent();
}

SearchFunctions.IMDB IMDBClass = new QuickFlick.SearchFunctions.IMDB();
int YPosition = 5;

private void btnSearch_Click(object sender, EventArgs e)
{
string[] MovieInformation = IMDBClass.SearchForMovie(txtSearch.Text);
LoadMovieInformation(MovieInformation);
}

public void LoadMovieInformation(string[] FoundInfo)
{
MovieItem TheMovieItem = new MovieItem();
TheMovieItem.SetTitle(FoundInfo[0]);
TheMovieItem.SetDirector(FoundInfo[1]);
TheMovieItem.SetRelease(FoundInfo[2]);
TheMovieItem.SetPlot(FoundInfo[3]);
TheMovieItem.SetPoster(FoundInfo[4]);

TheMovieItem.Location = new Point(5, YPosition);
YPosition += 196;

panel1.Controls.Add(TheMovieItem);
}
}

现在,我的程序试图完成的要点。

用户会写下电影的名称,然后我会调出有关它的信息。没有其他的! :P 这主要是为了让我学习异步函数等。但我担心我可能会以完全错误的方式接近它。

再说一遍,我不是在寻找太多的代码,只是在寻找程序的设计。方法、方法的顺序、不必要的方法等。:D

非常感谢,一如既往,你很棒!

最佳答案

我不会对整个设计发表评论,因为像@Vinko 一样,我认为您应该在这方面更加关注您的问题。我要说的是,您从根本上误解了异步方法的工作原理。他们将立即返回到调用线程而不先完成。这就是异步线程的全部要点 - 它们不会等到完成后再返回。

为此,您必须使用同步调用或在调用后等待某个事件并在异步回调处理程序中向该事件发出信号,以便您的代码在调用完成之前不会尝试访问返回的数据.这种方法的好处是您的线程在等待调用完成时可以自由执行其他任务,但您必须承担管理等待过程的复杂性(并不多)。

请参阅 DownloadDataCompleted event 的示例代码在 MSDN。您需要等待您下载的每个 Web 客户端。请注意,您需要在处理程序的示例中对传递给处理程序的 AutoResetEvent 对象调用 Set() 方法。

关于c# - 寻找 OO 大师,在设计我的编程逻辑时需要一些帮助。没什么特别的,只是新的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1586247/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com