开发者

How do I spin these 2 processes in threads?

I have a method which loads data from 2 regions. Currently its processes region1 and then processes region 2. So its single thread. I would need to do them so its simultaneous. Here is the curent code. Please advice what do I need to do.

enter code here

public override object RProcess()            
{            
      string queueName = Config.AppSettings["MQSIInboundQueueName"];

        string Region1 = Config.AppSettings["Region1"];
        string Region2 = Config.AppSettings["Region2"];

        string returnMessage = string.Empty;
        if (Region1.Trim().Length >开发者_Go百科; 0)
        {                
            returnMessage = ProcessMessage(string.Format(queueName, Region1));
            Logger.Log(returnMessage);
        }

        if (Region2.Trim().Length > 0)
        {
            returnMessage = ProcessMessage(string.Format(queueName, Region2));
            Logger.Log(returnMessage);
        }

        return null;
    }

--


Wow, loaded question. I have to make a couple assumptions with my answer here:

  1. You're using .NET 4.0
  2. The ProcessMessage method does not access any non-protected, shared state that would be impacted by multiple threads calling into it concurrently.
  3. Logger.Log also does not access any non-protected, shared state that would be impacted by multiple threads calling into it concurrently.

With that, Parallel::Invoke is your friend:

Parallel.Invoke(
    () =>
    {
       string region1 = Config.AppSettings["Region1"];

       if(region1.Trim().Length > 0)
       {
            string region1ReturnMessage = ProcessMessage(string.Format(queueName, region1));
            Logger.Log(region1ReturnMessage);
       }
    },
    () =>
    {
       string region2 = Config.AppSettings["Region2"];

       if(region2.Trim().Length > 0)
       {
            string region2ReturnMessage = ProcessMessage(string.Format(queueName, region2));
            Logger.Log(region2ReturnMessage);
       }
    });

Also, something else to consider, is that if it's a common occurrence to not actually call ProcessMessage for a particular region because of those checks, then it could make sense to do those checks up front on the main thread and not always dispatch the multiple calls. In that case you would need to drop down into pure TPL task management code and dynamically allocate only the number of tasks you need. I would bother only do this if this code is going to be executed under load inside of some kind of service process itself. Otherwise this is a premature optimization. If you'd like to see what the raw TPL approach would look like, just let me know in a comment and I will provide an example.


In addition to @Drew Marsh answer here if you are using older version of .net, you can invoke every operation in new thread, using Thread class or ThreadPool.QueueUserWorkItem

//example using Thread class
new Thread(() =>
{
    if (Region1.Trim().Length > 0)
    {                
        returnMessage = ProcessMessage(string.Format(queueName, Region1));
        Logger.Log(returnMessage);
    }
}) { IsBackground = true }.Start();

//example using ThreadPool
ThreadPool.QueueUserWorkItem(new WaitCallback((_) =>
{
    if (Region2.Trim().Length > 0)
    {
        returnMessage = ProcessMessage(string.Format(queueName, Region2));
        Logger.Log(returnMessage);
    }
}));

Anyway if you are updating Region1 or Region2 form any other place in the application, then you should consider using synchronization mechanism to access them here such as lock..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜