I have WCF service Service with some method LongTimeMethod. To avoid request timeout I do next:
public void LongTimeMethod(...)
{
Thread t = new Thread(delegate() {
LocalLongTimeMethod(...);
});
t.IsBackground = true;
t.Start();
}
private void LocalLongTimeMethod(...)
{
SomeOperations();
}
After that SomeOperations runs 10 times faster(!?). The result of SomeOperations is right. So what is the reason and is it normal?
One more question: is Service instance alive while my thread hasn’t finished?
The method is unlikely to be executing 10 times faster. Since your WCF operation is a
void– you aren’t returning anything. Rather, the introduction is the thread is allowing the WCF operation to complete and it will keep processing your work on a different thread.So you call your WCF operation, it fires off a thread, and completes. It isn’t going to wait for the thread to finish. If the client calls the operation synchronously, it will appear to have completed to the client even though the background thread is still there doing all the work.
This is often called “Fire-and-forget”.
A better option for this that is built into WCF is to create a One Way Contract.
Most likely it is.