1

我有一个系统,它只是去服务器并检索一些文本,以最大限度地提高我想一次发送许多请求的性能。代码如下所示:

var okButton.Text = TextSystem.Get("ok",language);

但是,它允许从代码中的任何位置进行 TextSystem 调用,并将其与其他代码混合。因此,如果开发人员必须在使用之前预加载所有内容,那么使用该系统会有些困难。

所以如果有可能我会伤害

  1. 有 TextSystem 调用时启动新线程
  2. 10ms 后将所有请求聚合为一个大
  3. 延迟向 okButton 赋值,直到收到响应

我想要这样的东西:

var okButton1.Text = TextSystem.Get("ok1",language); // Start thread X and buffer the 
// request
var okButton2.Text = TextSystem.Get("ok2",language); // Add ok2 to the request buffer
FinanceCall(); // Execute this call, oh 10ms has passed since thread X started
// send the request with ok1 and ok2 to remote server.
// The server has respond, change the value for okButton1.Text and okButton2.Text.
var okButton3.Text = TextSystem.Get("ok3",language); // Start thread Y and do the same..

此代码将在 ASP.NET 中执行,所以我关心的是 okButton1.Text 在页面呈现之前设置。我在考虑一些 async/await 方法是否可行,但我想就如何解决这个问题提供一些意见。

4

1 回答 1

1

不知道为什么需要聚合请求,但我建议让开发人员每个都添加请求,然后将它们放入队列或列表中,然后在需要时处理它们:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var r1 = new Request {Callback = r => Console.WriteLine(r), Parm1 = "ok1", Parm2 = "language"};
            TextSystem.QueueRequest(r1);

            var r2 = new Request {Callback = r => Console.WriteLine(r), Parm1 = "ok2", Parm2 = "language"};
            TextSystem.QueueRequest(r2);

            var r3 = new Request {Callback = r => Console.WriteLine(r), Parm1 = "ok3", Parm2 = "language"};
            TextSystem.QueueRequest(r3);

            var r4 = new Request {Callback = r => Console.WriteLine(r), Parm1 = "ok4", Parm2 = "language"};
            TextSystem.QueueRequest(r4);

            TextSystem.Flush();

            Console.Read();
        }
    }

    public static class TextSystem
    {
        private static readonly List<Request> requests = new List<Request>();

        static TextSystem()
        {
            requests = new List<Request>();
        }

        public static void QueueRequest(Request request)
        {
            requests.Add(request);
        }

        public async static void Flush()
        {
            List<Tuple<Request, string>> results = await Task.Run(() =>
                {
                    var list = new List<Tuple<Request, string>>();

                    //process each request
                    foreach (Request request in requests)
                    {
                        //Get data, process or whatever
                        list.Add(new Tuple<Request, string>(request, request.Parm1));
                    }
                    return list;
                });


            //Callback on same thread as the request was initiated on
            foreach (var result in results)
            {
                result.Item1.Callback(result.Item2);
            }
        }
    }

    public class Request
    {
        public Action<string> Callback { get; set; }
        public string Parm1 { get; set; }
        public string Parm2 { get; set; }
    }
}
于 2013-11-02T12:36:32.333 回答