如何阻止WCF服务在后面运行 [英] How to stop the WCF service running behind

查看:50
本文介绍了如何阻止WCF服务在后面运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,





我有一个WCF服务,它会以收集的形式从数据库中获取一个包含大量数据的对象。

我有一个获取和取消按钮的UI。



获取按钮:发送服务请求并继续使用返回的数据填充来自service的数据。我按如下所示开始



Hi All,


I have a WCF service which will get an object with huge data from database in the form of collection.
I have a UI which has get and cancel buttons .

Get button : make service request and continue with data population with the returned data from service.I start it as below

Thread _workerThread = new Thread(DoExportTreeLoad);
_workerThread.Start();





取消:停止上述线程。它执行如下





Cancel : stops the above thread.It does as below

_workerThread.Abort()





我的DoExportTreeLoad()有同步服务调用,它反过来命中数据库并获取数据。





My DoExportTreeLoad() has the synchronous servcie call which in turn hits database and gets data.

result = serivce.GetData(criteria);





问题:当我在请求服务后取消操作时,DAL代码仍然在后台运行(从数据库获取数据),它不会发生。



一旦我从UI取消操作,就需要中止数据库中的事务(获取数据)。

它导致大的性能问题。



请在线程取消时以任何方式结束后端任务。

提前致谢



Problem: When I cancel the operation after requesting the service, the DAL code is still running in the background (getting data from database) where it should not happen.

As soon as i cancel the operation from UI ,the transaction (to get the data ) from database need to be aborted.
It leads to big performance problem.

Please help me with any way to end up the backend task when thread is cancelled.
Thanks in advance.

推荐答案

Plz尝试以下代码。

Plz try below code.
private object m_lock = new object();
private bool m_isRunning = false;
private bool m_isAbortRequested = false;

public void OnButtonClick(object sender, EventArgs e) {
  lock ( m_lock ) {
    if ( m_isRunning ) {
      m_isAbortRequested = true;
    } else {
      m_isAbortRequested = false;
      m_isRunning = true;
      ThreadPool.QueueUserWorkItem(BackgroundMethod);
    }
  }
}

private void BackgroundMethod() {
  try {
    DoRealWork();
  } finally {
    lock (m_lock) {
      m_isRunning = false;
    }
  }
}

private void DoRealWork() {
  ...
  if ( m_isAbortRequested ) {
    return;
  }
}






和我一样多明白你可能需要在客户来电后停止WCF操作。



现在如果你想要这样的灵活性,你应该去:异步 [ ^ ]



这是实施的一个例子: http://robbincremers.me/ 2011/12/31 / wcf-asynchronous-client-proxy-and-asynchronous-service-operations / [ ^ ]



谢谢

Suvabrata
Hi,

As much as I understand that's you may required to stop WCF operation after the call from client.

Now if you want such flexibility then you should go for : Asynchronously[^]

Here is a example of Implementation : http://robbincremers.me/2011/12/31/wcf-asynchronous-client-proxy-and-asynchronous-service-operations/[^]

Thanks
Suvabrata


这篇关于如何阻止WCF服务在后面运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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