Article information
Article relates to
Telerik Reporting
Created by
IvanY
Last modified
08/14/2013
Last modified by
DESCRIPTION With the new .NET Framework (4.5) a new way to handle parallel programming has been added - with the help of the Task<> class and the keywords async and await parallel programming has become much easier. This raises the question how these new features can be used along with Telerik Reporting. Follow the steps below and with a few lines of code you will be able to export and print all your reports asynchronously.
SOLUTION First we will have to start with the ReportProcessor class and its RenderReport method, which provide all the needed functionality for programmatic generation of reports. Since RenderReport returns RenderingResult in order to use it asynchronously you will have to create a method that returns this result. However since we want to achieve the task in an async manner we will take advantage of the Task class. This means that our new method will look like this: C#
public
class
AsyncWrappers
{
// Wrap the RenderingResult like this:
async Task<RenderingResult> RenderReportAsync(Type reportType)
// Pass parameters here, like device info and report to render
ReportProcessor reportProcessor =
new
ReportProcessor();
// Apply any deviceInfo settings if necessary
Hashtable deviceInfo =
Hashtable();
// Any other Report Source can be used instead
// For example InstanceReportSource can be used if the instantiated report is passed as parameter of the method
TypeReportSource typeReportSource =
TypeReportSource();
typeReportSource.TypeName = reportType.AssemblyQualifiedName;
return
await Task.Run(() => reportProcessor.RenderReport(
"PDF"
, typeReportSource, deviceInfo));
}
VB.NET
Public
Class
' Wrap the RenderingResult like this:
Function
RenderReportAsync(reportType
As
Type)
Task(Of RenderingResult)
' Pass parameters here, like device info and report to render
Dim
reportProcessor
New
ReportProcessor()
' Apply any deviceInfo settings if necessary
deviceInfo
Hashtable()
' Any other Report Source can be used instead
' For example InstanceReportSource can be used if the instantiated report is passed as parameter of the method
typeReportSource
TypeReportSource()
typeReportSource.TypeName = reportType.AssemblyQualifiedName
Return
Await Task.Run(
() reportProcessor.RenderReport(
, typeReportSource, deviceInfo))
End
C#
//Use the wrapper in your code like this:
async Task<
string
> RenderReportAsync()
var asyncWrappers =
AsyncWrappers();
Console.WriteLine(
"Rendering started on: {0}"
, Thread.CurrentThread.ManagedThreadId);
var result = await asyncWrappers.RenderReportAsync(
typeof
(Telerik.Reporting.Report) );
// pass the type of your report here
fileName = result.DocumentName +
"."
+ result.Extension;
path = System.IO.Path.GetTempPath();
filePath = System.IO.Path.Combine(path, fileName);
using
(System.IO.FileStream fs =
System.IO.FileStream(filePath, System.IO.FileMode.Create))
fs.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);
"Rendering finished on: {0}\n"
// You can return void but that is not recommended
.Format(
"Successfully rendered! File saved in {0}\n"
, filePath);
'Use the wrapper in your code like this:
RenderReportAsync()
Task(Of
String
)
asyncWrappers =
AsyncWrappers()
, Thread.CurrentThread.ManagedThreadId)
result = Await asyncWrappers.RenderReportAsync(
GetType
(Telerik.Reporting.Report))
' pass the type of your report here
fileName
= Convert.ToString(result.DocumentName) &
& Convert.ToString(result.Extension)
path
= System.IO.Path.GetTempPath()
filePath
= System.IO.Path.Combine(path, fileName)
Using fs
System.IO.FileStream(filePath, System.IO.FileMode.Create)
fs.Write(result.DocumentBytes, 0, result.DocumentBytes.Length)
Using
"Rendering finished on: {0}"
& vbLf, Thread.CurrentThread.ManagedThreadId)
' You can return void but that is not recommended
"Successfully rendered! File saved in {0}"
& vbLf, filePath)
Program
// A working example
static
void
Main(
[] args)
// Use your class to render the report in an async manner
var myClass =
MyClass();
"Starting rendering..."
);
var task = myClass.RenderReportAsync();
// While the report is rendering do something else
Count();
"Main finished on: {0}"
"Render Status: {0}\n"
, task.Result);
Console.ReadLine();
Count()
for
(
int
i = 0; i < 10; i++)
Console.WriteLine(i);
' A working example
Private
Shared
Sub
Main(args
())
' Use your class to render the report in an async manner
[myClass] =
[
MyClass
]()
task = [myClass].RenderReportAsync()
' While the report is rendering do something else
"Render Status: {0}"
& vbLf, task.Result)
Console.ReadLine()
For
i
Integer
= 0
To
9
Console.WriteLine(i)
Next
Resources Buy Try