Originally Posted by
codebot
Thanks!
I have two classes http and log class. The http class is going to handle the requests and the log class is going to store or display the information. I'm not going to call the log class directly, but instead let the http class deal with the log. Then I can in my Main call http.request("http://google.com"); and don't have to deal with anything else. Is it possible or is that a bad design? Thanks!
Don't overcomplicate things. That is my advise. Use the default design you get in Visual Studio and try to integrate your http and log classes inside the Form1 class. My logic is working from where the default environment has stopped rather than redesigning the classes from the beginning even if it makes more sense. That way you keep separated your implementation and the default code given to you.
For example: Create a new .cs file and implement an http class there. Don't bother on the "displaying" part. That class will get the results and store them in a List<string> or something else convinitent. Create the log class as well. Then in the main form you can have a button "Get Results" which would do something like this
Code:
private void GetResults_Click(object sender, EventArgs e)
{
http myHttp = new http("http://www.google.com");
List<string> results = myHttp.GetResults();
log myLog = new Log();
log.Append(results);
resultBox = log.ToString(); //resultsBox being a TextBox to put the results
}
You could more simply have a GetResults() function that does what you want and not need two classes, but that is up to you. You would also probably want to instantiate the http and log classes outside the function, the above is just an example.
To fix what theobe said you have another solution which I prefer.
Run the GetResults() in a separate thread. You have separated your code from the displaying part so you can freely call another thread.
When it is done it can set a flag. Then use a "wait code" like this
Code:
private void GetResults_Click(object sender, EventArgs e)
{
http myHttp = new http("http://www.google.com");
List<string> results = myHttp.GetResults();
while(!myHttp.Done)
Application.DoEvents();
log myLog = new Log();
log.Append(results);
resultBox = log.ToString(); //resultsBox being a TextBox to put the results
}
the GetResults will set the Done flag.
Finally, you might want to set one more flag not to get multiple presses of the Get Results button like
Code:
bool getting = false;
private void GetResults_Click(object sender, EventArgs e)
{
if (getting) return;
getting = true;
http myHttp = new http("http://www.google.com");
List<string> results = myHttp.GetResults();
while(!myHttp.Done)
Application.DoEvents();
log myLog = new Log();
log.Append(results);
resultBox = log.ToString(); //resultsBox being a TextBox to put the results
getting = false;
}