Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

In my ASP.Net web application we are provideing some online reports, one of the report collets data from some other report, process it and showas a new report.
This entire process takes about 15 minutes on development server .

Client is good with that.

But what they want is we should provide them a option to terminate that process in between.
That is, if user has clicked on the “show report” button and the process has been started, and meanwhile if user change his mind to cancel the process, they will click on “Cancel” button which will stop/terminate the process from the point where it is.

I’ve tried to implement the threading option, but unable to terminate/abort a thread which has been started in one event (Show Report event) from another event (Cancel event).
Posted
Comments
D-Kishore 29-Aug-12 6:00am    
it is running under the thread or sql stored procedure or what?

1 solution

Hi,

a possible solution would be, that you use a static (shared) variable which can be accessed by multiple threads.

Here is a small example of what I mean:

C#
public class ProcessingClass
{
	/// <summary>
	/// Lock object to synchronize the _workerThread access.
	/// </summary>
	private static volatile object _lock = new object();

	/// <summary>
	/// The worker thread doing your stuff.
	/// </summary>
	private static Thread _workerThread;

	/// <summary>
	/// Method to start the processing.
	/// </summary>
	public void StartProcessing()
	{
		// first aquire a lock (that you can be sure you're the only one accessing the _workerThread property).
		lock (_lock)
		{
			if (_workerThread != null)
			{
				throw new NotSupportedException("The thread is already processing...");
			}

			// Instantiate the thread.
			_workerThread = new Thread(ProcessingMethod);

			// Start the processing.
			_workerThread.Start();
		}
	}

	/// <summary>
	/// Method to end the processing.
	/// </summary>
	public void EndProcess()
	{
		// first aquire a lock (that you can be sure you're the only one accessing the _workerThread property).
		lock (_lock)
		{
			if (_workerThread == null)
			{
				throw new NotSupportedException("The thread is not processing anything.");
			}

			// Join the thread and give him 1000ms to terminate
			_workerThread.Join(1000);
				
			// Thread terminated completely.
		}
	}

	/// <summary>
	/// The processing method itself.
	/// </summary>
	public void ProcessingMethod()
	{
		// do some stuff in it...
		Thread.Sleep(10000);
	}
}


The ProcessingClass can be any class you want, it could also be the ASP.NET Page, that should not matter.

Hope this helps!

Best regards and happy coding,
Chris
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900