排队方法调用 - 知道怎么做吗?

发布于 2024-08-31 02:47:48 字数 340 浏览 1 评论 0原文

我编写了一个高度异步的应用程序。

我正在寻找一种对方法调用进行排队的方法,类似于 BeginInvoke / EndInvoke 所做的......但在我自己的队列上。原因是我拥有自己的优化消息队列系统,使用线程池,但同时确保每个组件在请求中都是单线程的(即一个线程仅处理组件的消息)。

我有很多来回的消息。对于有限的使用,我真的很希望能够使用参数对消息调用进行排队,而不是为了执行大量管理调用而必须定义自己的参数、方法包装/展开。我也不总是想绕过队列,并且我绝对不希望发送服务等待其他服务响应。

有人知道拦截方法调用的方法吗?有什么方法可以利用透明代理/虚拟代理来实现此目的? ;) 服务组件?我希望开销尽可能小;)

I write a heavily asynchronseous application.

I am looking for a way to queue method calls, similar to what BeginInvoke / EndInvoke does.... but on my OWN queue. The reaqson is that I am having my own optimized message queueing system using a threadpool but at the same time making sure every component is single threaded in the requests (i.e. one thread only handles messages for a component).

I Have a lot of messages going back and forth. For limited use, I would really love to be able to just queue a message call with parameters, instead of having to define my own parameter, method wrapping / unwrapping just for the sake of doing a lot of admnistrative calls. I also do not always want to bypass the queue, and I definitely do not want the sending service to wait for the other service to respond.

Anyone knows of a way to intercept a method call? Some way to utilize TransparentProxy / Virtual Proxy for this? ;) ServicedComponent? I would like this to be as little overhead as possible ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半边脸i 2024-09-07 02:47:48

使用 lambda 怎么样?

我的意思是,为什么你不创建一些队列,并以类似的方式处理它们,

while (!queue.Empty) 
{
    Action action = queue.Pop();
    action(); // this calls your action
}

你可以非常简单地添加操作:

Queue.Add( ()=>{  /* any code you wish here */})

这只是一个提示,我不确定是否有一些队列类,但它应该非常简单自己创建一个(并且是线程安全的!)。

解决方法可以(而且应该)更加明智,但要点就在那里。如果您想咨询请写信给我。

Pz,TaskConnect 开发人员

How about using lambdas?

I mean, why don't you create some Queue, and process them in manner like

while (!queue.Empty) 
{
    Action action = queue.Pop();
    action(); // this calls your action
}

You can add actions very simply:

Queue.Add( ()=>{  /* any code you wish here */})

This is just a tip, I'm not sure if there is some Queue class, but it should be pretty straightforward to create one (and threadsafe!) by yourself.

The workaround could (and should) be much wiser, but the main point is there. Write me if you want to consult.

Pz, the TaskConnect developer

冰雪之触 2024-09-07 02:47:48

创建 MethodInvoker 的队列

Queue<MethodInvoker> EventCall = new Queue<MethodInvoker>();

稍后将项目添加到您的队列

EventCall.Enqueue(ClearAllVals);
EventCall.Enqueue(saystuff);
EventCall.Enqueue(testFunc);

然后一次调用您的函数:

MethodInvoker bb = EventCall.Dequeue();
bb();
bb = EventCall.Dequeue();
bb();
bb = EventCall.Dequeue();
bb();

以安全的方式调用所有函数(这也会将它们从队列中删除,使队列为空并调用所有函数)

public bool InvokeAll(){
    MethodInvoker bb = null; // this will hold the function prior to use
    for(int i = 0; i<EventCall.count; i++){

        bb = EventCall.Dequeue(); //pull a method off of the queue
        bb(); //call the method you pulled off of the queue

    }
}

要全部调用它们,只需使用 InvokeAll(); 或在需要时一次调用它们:

public bool NextEvent(){
    MethodInvoker bb = null; // this will hold the function prior to use
    if(EventCall.count > 0){

        bb = EventCall.Dequeue(); //pull a method off of the queue
        bb(); //call the method you pulled off of the queue

        } else {
        MessageBox.Show("there was no event to call"); // this is optional, point being you should be handeling the fact that there is no events left in some way.
        }
}

Create a queue of MethodInvoker's

Queue<MethodInvoker> EventCall = new Queue<MethodInvoker>();

Later add items to your Queue

EventCall.Enqueue(ClearAllVals);
EventCall.Enqueue(saystuff);
EventCall.Enqueue(testFunc);

Then call your functions one at a time:

MethodInvoker bb = EventCall.Dequeue();
bb();
bb = EventCall.Dequeue();
bb();
bb = EventCall.Dequeue();
bb();

to call all of your functions in a safe way (this will also remove them all from the queue leaving the queue empty and all the functions called)

public bool InvokeAll(){
    MethodInvoker bb = null; // this will hold the function prior to use
    for(int i = 0; i<EventCall.count; i++){

        bb = EventCall.Dequeue(); //pull a method off of the queue
        bb(); //call the method you pulled off of the queue

    }
}

to call them all just use InvokeAll(); or to call them one a time whenever you want:

public bool NextEvent(){
    MethodInvoker bb = null; // this will hold the function prior to use
    if(EventCall.count > 0){

        bb = EventCall.Dequeue(); //pull a method off of the queue
        bb(); //call the method you pulled off of the queue

        } else {
        MessageBox.Show("there was no event to call"); // this is optional, point being you should be handeling the fact that there is no events left in some way.
        }
}
简单 2024-09-07 02:47:48

作为 Castle 项目一部分的 DynamicProxy 允许对象成员拦截,而无需一些典型的编组痛苦

http:// /www.castleproject.org/projects/dynamicproxy/

您可以使用它来拦截您的方法调用,然后对它们执行您想要的操作。

The DynamicProxy that is part of the Castle project allows object member interception without some of the typical Marshalling pain

http://www.castleproject.org/projects/dynamicproxy/

You could use this to intercept your methods calls and then do with them what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文