F
Fieldadvice
Hi,
Im starting a new thread in my main aspx page, but when I run it, it always
runs through the main code first THEN the workerthread. Can somebody shed
some light on the problem??
Here is the code:
public class WebForm1 : System.Web.UI.Page
{
private FeedAsync Feed1;
public XmlDocument state = new XmlDocument();
private void Page_Load(object sender, System.EventArgs e)
{
Feed1 = new
FeedAsync("http://www.bbc.co.uk/syndication/feeds/news/ukfs_news/technology/
rss091.xml", state);
Feed1.WorkerThread.Join();
System.DateTime start = DateTime.Now;
while (Feed1.Completed == false && ((TimeSpan)(DateTime.Now -
start)).Seconds < 20)
{}
if (((TimeSpan)(DateTime.Now - start)).Seconds > 20)
{
Response.Write("TimeOut");
}
else
{
Response.Write(Feed1.Result.InnerText); // <-------Feed1.Result is ALWAYS
NULL
}
}
public class FeedAsync
{
public bool Completed = false;
private HttpWebRequest HTTPRequest = null;
public HttpWebResponse HTTPResponse = null;
public XmlDocument Result = null;
private AsyncCallback cb = null;
public Thread WorkerThread;
public FeedAsync(string URL)
{
state = nstate;
cb = new AsyncCallback(BeginGetResponseAsyncCallback);
HTTPRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
WorkerThread = new Thread(new ThreadStart(Execute));
WorkerThread.Start();
}
public void Execute()
{
HTTPRequest.BeginGetResponse(cb, null);
}
private void BeginGetResponseAsyncCallback(IAsyncResult ar) // <------THIS
FUNCTION ONLY SEEMS TO GET CALLED AFTER THE MAIN THREAD HAS COMPLETED
{
if (ar.IsCompleted)
{
HTTPResponse = (HttpWebResponse)HTTPRequest.EndGetResponse(ar);
System.Text.Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader sr = new StreamReader(HTTPResponse.GetResponseStream(),
encode);
Result = new XmlDocument();
Result.Load(sr);
sr.Close();
HTTPResponse.Close();
Completed = true;
}
}
}
Im starting a new thread in my main aspx page, but when I run it, it always
runs through the main code first THEN the workerthread. Can somebody shed
some light on the problem??
Here is the code:
public class WebForm1 : System.Web.UI.Page
{
private FeedAsync Feed1;
public XmlDocument state = new XmlDocument();
private void Page_Load(object sender, System.EventArgs e)
{
Feed1 = new
FeedAsync("http://www.bbc.co.uk/syndication/feeds/news/ukfs_news/technology/
rss091.xml", state);
Feed1.WorkerThread.Join();
System.DateTime start = DateTime.Now;
while (Feed1.Completed == false && ((TimeSpan)(DateTime.Now -
start)).Seconds < 20)
{}
if (((TimeSpan)(DateTime.Now - start)).Seconds > 20)
{
Response.Write("TimeOut");
}
else
{
Response.Write(Feed1.Result.InnerText); // <-------Feed1.Result is ALWAYS
NULL
}
}
public class FeedAsync
{
public bool Completed = false;
private HttpWebRequest HTTPRequest = null;
public HttpWebResponse HTTPResponse = null;
public XmlDocument Result = null;
private AsyncCallback cb = null;
public Thread WorkerThread;
public FeedAsync(string URL)
{
state = nstate;
cb = new AsyncCallback(BeginGetResponseAsyncCallback);
HTTPRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
WorkerThread = new Thread(new ThreadStart(Execute));
WorkerThread.Start();
}
public void Execute()
{
HTTPRequest.BeginGetResponse(cb, null);
}
private void BeginGetResponseAsyncCallback(IAsyncResult ar) // <------THIS
FUNCTION ONLY SEEMS TO GET CALLED AFTER THE MAIN THREAD HAS COMPLETED
{
if (ar.IsCompleted)
{
HTTPResponse = (HttpWebResponse)HTTPRequest.EndGetResponse(ar);
System.Text.Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader sr = new StreamReader(HTTPResponse.GetResponseStream(),
encode);
Result = new XmlDocument();
Result.Load(sr);
sr.Close();
HTTPResponse.Close();
Completed = true;
}
}
}