Question

I have a group of web services that get hit 2,000,000 times a day; 7 web services. Each service currently drops the incoming XML message to disk (it's a big disk).

The file drops happen at the start of the request, and I want to spin this off onto a new thread so I don't lose the 20ms it takes to write the file. Yes, those 20ms matter more than you think.

I'm using .NET 2.0, and have thought of 2 ways to do this: Asynchronous method call to the DropMessage() method, or starting a new Thread and calling it.

private void DumpMessage()// this method dumps the incoming message to File.

DumpMessages manages it's own business. If it fails, it knows what to do and how to contact my team.

public delegate void AsyncDumpMessage();
AsyncDumpMessage caller = new AsyncDumpMessage(DumpMessage);
IAsyncResult result = caller.BeginInvoke(null, null);

vs

System.Threading.Thread t = new System.Threading.Thread(DumpMessage);
t.Start();

Are there any caveats to the above two? I'm leaning on the Asynchronous method, as I've gotten better performance out of it through my testing

Any other ideas for a better way to do this? Performance and stability is key for me.

Was it helpful?

Solution

I would use ThreadPool.QueueUserWorkItem to schedule this work.

This will use a ThreadPool thread, which has less overhead than launching a separate thread. It also suggests a "fire and forget" method call, where the asynchronous delegate invocation suggests you'd want to use IAsyncResult.

In a more modern version of the framework, I would recommend Task.Run, but this will not work in .NET 2.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top