在poskbak期间中止进程. [英] Abort a process during poskbak.

查看:94
本文介绍了在poskbak期间中止进程.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

在我的ASP.Net Web应用程序中,我们提供一些在线报告,其中一个报告从其他报告中收集数据,进行处理并显示为新报告.
在开发服务器上,整个过程大约需要15分钟.

客户对此很满意.

但是他们想要的是,我们应该为他们提供一个选项,以在两者之间终止该过程.
也就是说,如果用户单击显示报告"按钮并且该过程已经开始,同时,如果用户改变主意取消该过程,则他们将单击取消"按钮,该按钮将停止/终止该过程.指向它所在的位置.

我尝试实现线程选项,但无法终止/中止在一个事件(显示报告"事件)中与另一个事件(取消"事件)中启动的线程.

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).

推荐答案



一个可能的解决方案是,您使用可由多个线程访问的静态(共享)变量.

这是我的意思的一个小例子:

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:

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);
	}
}



ProcessingClass可以是您想要的任何类,也可以是ASP.NET页,这无关紧要.

希望这会有所帮助!

致以诚挚的问候,祝您编程愉快,
克里斯



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


这篇关于在poskbak期间中止进程.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆