views:

117

answers:

1

I'm getting a weird threading issue inside a SharePoint workflow that I'd like to learn more about. I'm trying to send an email using SPUtility.SendEmail asynchronously by firing up a new thread to do it. This successfully works at the end of a workflow for a summary email. I tried doing the same to replace a task notification email but ended up receiving the following error:

Microsoft.SharePoint.SPException: Attempted to make calls on more than one thread in single threaded mode.

The only difference I can think of is that the task is inside a replication activity so it can create several of these tasks (although testing this it's only creating just the one). What specifically is the replication activity doing behind the scenes that might cause this error when I try to spawn a new thread?

[NOTE] The reason I'm using threads to send the emails is because SPUtility.SendEmail is a blocking call, and in the case of a slow SMTP server the user might be getting their page back for up to a minute. And the reason I'm sending emails in the first place is because the standard task emails suck big time.

+2  A: 

SPUtility.SendEmail uses an SPWeb parameter which is NOT thread-safe. SPSite, SPWeb objects are actually thread-specific so you shouldn't pass them from one thread to another. You should pass the site ID and web URL to the new thread and recreate the SPWeb object there.

Panagiotis Kanavos
You are correct, this is where my problem lies. Creating a new SPWeb in the thread fixed the issue. Thanks!
Dan Revell