Iterating through an SPListItemCollection is never a good idea (but sadly sometimes the only way to go). You could consider wrapping up this code in a LongRunningOperation. Here's some code I adapted from some of my own:
ACTUAL CLASS:
using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.SharePoint;
namespace Common.LongRunningJobs
{
/// <summary>
/// Provides a long running job wrapper around converting multiple files
/// </summary>
[CLSCompliant(false)]
public class FileToPdfLongRunningJob : LongRunningOperationJob
{
private List<string> fileUrls;
/// <summary>
/// Initializes a new instance of the <see cref="FileToPdfLongRunningJob"/> class.
/// </summary>
/// <param name="urls">The urls of the files to create pdfs from.</param>
public FileToPdfLongRunningJob(List<string> urls)
{
fileUrls = urls;
}
/// <summary>
/// Does the actual work of converting the files, while providing the user with status updates.
/// </summary>
public override void DoWork()
{
try
{
using (var currentWeb = Site.OpenWeb(parentWeb))
{
OperationsPerformed = 0;
foreach (var url in fileUrls)
{
SPFile file = currentWeb.GetFile(url);
// DO PDF OUTPUT
StatusDescription = string.Format(CultureInfo.InvariantCulture, "busy converting {0} van {1}, file: {2}...", (OperationsPerformed + 1), TotalOperationsToBePerformed, file.Name);
UpdateStatus();
OperationsPerformed++;
}
}
}
catch (Exception ex)
{
// LOG ERROR
}
}
}
}
USING ABOVE CLASS:
private void ConvertFiles()
{
const string PROGRESS_PAGE_URL = "/_layouts/LongRunningOperationProgress.aspx";
var urls = new List<string>();
foreach (SPListItem item in yourlistitemcollection)
{
urls.Add(item.File.Url);
}
var longRunningJob = new FileMoveLongRunningJob(urls)
{
Title = "Pdf conversion Long Running Job",
TotalOperationsToBePerformed = urls.Count,
RedirectWhenFinished = true,
NavigateWhenDoneUrl = urlToRedirectTo;//i.e. SPContext.GetContext(Context).List.RootFolder.ServerRelativeUrl
};
longRunningJob.Start(SPContext.Current.Web);
string url = string.Format("{0}{1}?JobId={2}", SPContext.Current.Web.Url, PROGRESS_PAGE_URL, longRunningJob.JobId);
SPUtility.Redirect(url, SPRedirectFlags.Default, Context);
}