I believe that you can use the SPLongOperation class to achieve this.
You need to put the long running code between SPLogOperation.Begin() and SPLongOperaiton.End() method invocation.
var longOperation=new SPLongOperation(this.Page);
longOperation.LeadingHTML = "Please wait while the operation is running";
longOperation.TrailingHTML = "Once the operation is finished you will be redirected to result page";
longOperation.Begin();
//Do long operation here
Thread.Sleep(10000);
longOperation.End("Result.aspx");