0

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.

This question is specific to thread life cycle in relation to ASP page life cycles.

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)

Louis van Tonder
  • 3,664
  • 3
  • 31
  • 62
  • Possible duplicate of [How to use threads in asp.net?](https://stackoverflow.com/questions/14296188/how-to-use-threads-in-asp-net) – mjwills Jul 27 '17 at 13:11
  • 4
    You would be better off hosting this in a Windows service than in Asp.Net so you don't have to concern yourself with the appdomain getting unloaded periodically. Your last resort should actually be your first resort. –  Jul 27 '17 at 13:13
  • 2
    Websites running in IIS are not the place to do this. Create a service that connects to the TCP host and saves updates locally (database of some sort?) then have the website query that. –  Jul 27 '17 at 13:13
  • Thanks, all. I know my last resort should be my first (in a perfect world). What I am however trying to do, is to condense my app footprint. I would at some point like to offer this app as a product, and I don't want to have different components having to be tended to. Especially in a possibly hosted environment. If it turns out to be an issue, I will revert to a separate service. Thanks. – Louis van Tonder Jul 27 '17 at 13:44

1 Answers1

1

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.