How to get a string from a thread onto a page?

C

charles

Hi:

I created a thread in Page_Load and get a string through a socket from
another process. Then I show this string in a textbox on the page. When
Page_Load exits, the string does not display on the textbox.

If I add a Response.Flush() statement, I get an exception of type:
{System.Web.HttpException}.

What went wrong with my approach? Please help. Thank you in advance!

Charles
2/23
 
B

Bruce Barker

you proably did not add code in prerender or onload to wait for the the
thread to complete. thus by the time the thread complete, the request is
done, and the browser has already rendered the page.

-- bruce (sqlwork.com)
 
C

charles

Thank you, Bruce, for your promptive response!

Would you please tell me how to write in onload to wait for the thread
to complete?

I have 2 lines in Page_Load to start a thread:

readThread = new Thread( new ThreadStart( RunServer ) );
readThread.Start();

Thanks!

Charles
2/23
 
J

John Timney \( MVP \)

You could try to join() your thread to the existing thread actioning your
web request. This should pause your on_load method until the thread
concludes. Be warned though, your delaying the web server from responding
efficiently.

http://msdn.microsoft.com/library/d...frlrfsystemthreadingthreadclassjointopic1.asp

If your making a socket call, make sure you get a timeout invoked if the
call fails so the joined thread can return, otherwise your in trouble if the
thread fails to terminate. Thats one of the problems with sub-threading
events in asp.net.

You might want to consider instead making a webservice call within the load
method to invoke the socket and pass your value back, and make use of its
timeout ability to allow your load method to continue cleanly.

http://msdn.microsoft.com/library/d...otocolswebclientprotocolclasstimeouttopic.asp
-- Regards
John Timney
Microsoft MVP
 
C

charles

Dear John:

Thank you for your valuable advice. I did try the thread Join()
approach. Then I found that I could not end my thread. The situation
is: I am implementing an event that will fire when the data from
another process arrives. And I need to continuously receive and display
the data in a web form until that process quits. If I end the thread in
order to display the data, I need to re-create a thread, re-connect the
socket and wait for data (and probably already missed the data). In a
Windows form I never needed to do that, since there are many ways to
display the arrived data such as using Winsock.

I have not tried using Web Service yet. Do you think that I will end up
with the same problem as using thread? Or could you give me some
totally different suggestion? Thanks again!

Charles
2/27
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top