Easily call any method asynchronously - fire and forget
I’ve been looking for a good way of implementing some “fire and forget it” calls in .NET. I’ve seen lots of code out there that handles this, but I didn’t like their solutions. They were prone to breaking when signatures changed. I want to know at compile time if I have the wrong parameter list.
I want to do insert, update, and delete operations in other threads, so that the user experience is faster for that request.
Ok, here is the code:
private void button1_Click(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(delegate { SyncDummyMethod(”test”, 4); });
}
private void SyncDummyMethod(string s, int i)
{
Thread.Sleep(3000);
}
Obviously you’ll want to do some logging if the call fails. You may also want to create your own thread pool depending on what type of work you’re doing.