Av.Raj
Av.Raj

Reputation: 65

How to Set a Timeout for Crystal Report Generation or Stop the Task After a Certain Period?

I'm facing an issue with Crystal Reports where, once the report generation starts, it blocks other tasks and does not stop even after a certain time. I want to implement a timeout feature, where the Crystal Report generation task stops after a set period (e.g., 30 or 45 seconds).

I’ve attempted to use a cancellation token, but it doesn’t stop the task once it's initiated. I also tried setting an SQL connection timeout using the ConnectionInfo object, but this approach doesn't seem to work either. Here’s the code I used:

    Task loadTask = Task.Run(async () =>
{
    await GenerateReportAsync(Dispatch, reportGenerationTimeOut);
}, cancellationTokenSource.Token);

// Wait for either the loadTask to complete or a timeout to occur
Task completedTask = Task.WhenAny(loadTask, Task.Delay(TimeSpan.FromSeconds(Convert.ToInt32(reportGenerationTimeOut)))).Result; // Timeout after 30 seconds

if (completedTask == loadTask)
{
    // Report loaded successfully within the timeout period
}
else
{
    // Timeout occurred
    cancellationTokenSource.Cancel(); // Cancel the operation if it's still running
    // Redirect to report error page

}

I have tried report document sql connection timeout as well

    public ReportDocument SetReportCommandTimeout(ReportDocument reportDocument, string commandTimeout)
{
    foreach (Table table in reportDocument.Database.Tables)
    {
        TableLogOnInfo logOnInfo = table.LogOnInfo;
        ConnectionInfo connectionInfo = new ConnectionInfo
        {
            ServerName = logOnInfo.ConnectionInfo.ServerName,
            DatabaseName = logOnInfo.ConnectionInfo.DatabaseName,
            UserID = logOnInfo.ConnectionInfo.UserID,
            Password = "Password",
        };
        
        connectionInfo.LogonProperties.Add(new NameValuePair2("Connect Timeout", commandTimeout));
        connectionInfo.LogonProperties.Set("Connect Timeout", commandTimeout);

        table.LogOnInfo.ConnectionInfo = connectionInfo;
        table.ApplyLogOnInfo(table.LogOnInfo);
    }
    return reportDocument;
}

Despite these efforts, the task continues to run indefinitely. Is there a way to properly set a timeout for the ReportDocument generation, or is there another method to stop the report generation after a specific time? Any suggestions or alternatives would be greatly appreciated.

Other wise how can I setup report document sql query exection time.

Upvotes: 1

Views: 153

Answers (0)

Related Questions