About using Threads in Outsystems Extensions

Hi, I am trying to develop an extension that needs multi-threaded operations.

I tried to start a thread in  void Mss[EXTENSION_NAME] but it seemed to throw off an exception saying "failed to start thread".

Does anyone about the best way to do that? Is it thread safe to do that? 


Hi Pat,
I don't know what you're trying to achieve with that, but I don't find it safe to start a new thread in the server during an HTTP request.
Remember that most (and I want to believe all) application servers use thread pools to handle http requests. If you go and start a new thread inside your HTTP request, you can either:
1 - Start the thread in a fire and forget fashion;
2 - Wait until the thread finishes the job at and;
If 1 is your scenario, there's definitely more interesting ways of achieving this with OutSystems.
If 2 is your scenario, you can run into security issues, because the thread will be "given" back to the pool and potentially (and most likely) be reused by other user.  If your thread sets some data in the thread local, it can be available to subsequent requests!
So I would say that it's better (if you can) explain what you're trying to achieve and I'm sure this Great Community will help you on achieving it.
Hope it helps!
Yes, it is very helpful. Thanks a lot.
My best suggestion is to write it as a Web service, then call the Web service from Service Studio. multi-threaded stuff does NOT work well on Web servers, since the Web server is already spinning up as many threads as it can to process requests. By making it a Web service, you can either co-locate it on the Web server, or move it to another dedicated server if performance requires it. If you are using .NET, it is only a few minutes to wrap your existing code as a Web Service using WCF.

Yet another word of warning for anyone thinking of spawning threads within a web request...

While the threads created by the web application server to handle a HTTP request will have some kind of error handling (that will at least display an Error Page 500), other threads spawned by the user will have no error handling by default. This means that if an unhandled exception occurs in these threads, it will ultimately crash the whole worker process (in IIS), and any concurrent requests being processed will be therefore killed.

So if you're spawning your own threads, it's generally a good idea to start by handling all errors on a proper way (logging, etc).