有条件地运行一个类的一个单独的线程的方法 [英] Conditionally run the methods of a class on a separate thread

查看:128
本文介绍了有条件地运行一个类的一个单独的线程的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个节省日志记录信息到数据库中(在NServiceBus消息处理方法)的类。

大多数时间该记录可对来自主处理一个单独的线程(和事务)来完成。但是这一切都需要在相同的背景线程中完成(这意味着他们必须按顺序执行,只是不同步与主过程)。

然而,一旦开始对外插接从NServiceBus的实际数据,它需要在同一线程。

下面是一些例子code:

 公共类myexample中
{

   公共无效LogSomeStuff(东西的东西)
   {
      使用(MoveOutsideTransaction())
      {
         //执行方法的东西在这里
         dataAccess.SaveChanges();
      }
   }


   公共无效LogSomeOtherStuff(东西的东西)
   {
      使用(MoveOutsideTransaction())
      {
         //做其他方法的东西在这里
         dataAccess.SaveChanges();
      }
   }

   私人IDisposable的MoveOutsideTransaction()
   {
       如果(loggingOutsideTransaction)
           返回新的TransactionScope(TransactionScopeOption.Sup preSS);

       返回null;
   }
}
 

我想知道是否有一种方法可以使用​​我的交易条件也有条件地将运行到一个不同的线程。 (但只有当它燮presses交易。)

解决方案
  

我想知道是否有一种方法可以使用​​我的交易条件,以   也有条件地运行移动到不同的主题。

您不能只是移动方法的执行到一个不同的线程。线程不工作方式一般。然而,你可以做的是建立能够参与其中呢,或多或少反正编组运行,模拟执行到另一个线程传输专用线程。重要的是要开车回家的一点,这个专用线程必须是特别codeD接受这种编组操作是很重要的。

下面是如何工作的。

 公共类DedicatedThread
{
  私人BlockingCollection<作用>行动=新BlockingCollection<作用>();

  公共DedicatedThread()
  {
    VAR线程=新主题(
      ()=>
      {
        而(真)
        {
          动作动作= actions.Take();
          行动();
        }
      });
  }

  公共无效SubmitAction(动作的动作)
  {
    actions.Add(动作);
  }
}
 

和你可能会使用这样的。

 如果(loggingOutsideTransaction)
{
  //在一个专门的线程异步执行。
  dedicatedThread.SubmitAction(yourActionDelegate);
}
其他
{
  //执行同步。
  yourActionDelegate();
}
 

I have a class that saves logging information to the database (in an NServiceBus message Handle method).

Most of the time that logging can be done on a separate thread (and transaction) from the main process. But it all needs to be done on the same background thread (meaning that they have to be done in order, just not in sync with the main process).

However, once it starts Foreign Keying to the actual data from NServiceBus, it needs to be on the same thread.

Here is some example code:

public class MyExample
{

   public void LogSomeStuff(Stuff stuff)
   {
      using (MoveOutsideTransaction())
      {
         // Do Method Stuff here
         dataAccess.SaveChanges();
      }
   }


   public void LogSomeOtherStuff(Stuff stuff)
   {
      using (MoveOutsideTransaction())
      {
         // Do Other Method Stuff here
         dataAccess.SaveChanges();
      }
   }

   private IDisposable MoveOutsideTransaction()
   {
       if (loggingOutsideTransaction)
           return new TransactionScope(TransactionScopeOption.Suppress);

       return null;
   }
}

I am wondering if there is a way to use my transaction conditional to also conditionally move the running to a different thread. (But only when it suppresses the transaction.)

解决方案

I am wondering if there is a way to use my transaction conditional to also conditionally move the running to a different thread.

You cannot just move the execution of a method onto a different thread. Threads do not work that way in general. However, what you can do is setup a dedicated thread that can participate in a marshaling operation which does, more or less anyway, simulate the transfer of execution to another thread. It is important to drive home the point that this dedicated thread must be specially coded to accept this marshaling operation.

Here is how it works.

public class DedicatedThread
{
  private BlockingCollection<Action> actions = new BlockingCollection<Action>();

  public DedicatedThread()
  {
    var thread = new Thread(
      () =>
      {
        while (true)
        {
          Action action = actions.Take();
          action();
        }
      });
  }

  public void SubmitAction(Action action)
  {
    actions.Add(action);
  }
}

And you might use it like this.

if (loggingOutsideTransaction) 
{  
  // Execute asynchronously on a dedicated thread.
  dedicatedThread.SubmitAction(yourActionDelegate); 
}
else
{
  // Execute synchronously.
  yourActionDelegate();
}

这篇关于有条件地运行一个类的一个单独的线程的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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