Louis van Tonder Louis van Tonder - 2 years ago 110
ASP.NET (C#) Question

Threading in ASP.net life cycle

I need to do some TCP communications in an ASP.net Web App.

I have built a working prototype of my code that communicates to the server using winforms. I would now implement this code into my web app, and I am not sure what the best way would be to do this.

I thinking of firing off a thread that does the TCP connection and listening to events from the TCP server.

If I do spawn a thread from my ASP.net web app, what is the lifecycle of this thread? Will it continue to run "orphaned" when the page lifecycle is recreated and a post back, refresh etc? Or will the page somehow clean this up when the life cycle is re-created on postback/refresh?

I wish to run my thread until I want to manually stop it.

I am experimenting with different options, but I would like to get some ideas on the "correct" way to achieve my end result.

(P.S. my thread will update application variables with relevant data coming in from the TCP server). Data to be used across multiple pages in this web app.

My other ideas are to run a standalone desktop app/service to collect the data and merge it into the IIS Application Pool session. (But that is my last resort)

Amy Amy
Answer Source

To address your questions:

If I do spawn a thread from my ASP.net web app, what is the lifecycle of this thread?

It will run until its done, or until the process is done, or until you explicitly tell it to end. This is the same as a thread in any other application.

Will it continue to run "orphaned" when the page lifecycle is recreated and a post back, refresh etc?

The page lifecycle is completely irrelevant. The thread will continue to run independently of the page.

Or will the page somehow clean this up when the life cycle is re-created on postback/refresh?

See previous answer.

Again, I suggest you not take this route and implement your threading scenario inside a Windows service.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download