C# 创建函数队列 [英] C# creating function queue

查看:29
本文介绍了C# 创建函数队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个名为 QueueManager 的类:

类 QueueManager{队列函数Queue;公共布尔 IsEmpty{得到{if (functionsQueue.Count == 0)返回真;别的返回假;}}公共队列管理器(){函数队列 = 新队列();}公共布尔包含(动作动作){if (functionsQueue.Contains(action))返回真;别的返回假;}公共动作弹出(){返回函数Queue.Dequeue() 作为动作;}公共无效添加(操作函数){函数队列.入队(函数);}public void Add(Func function){函数队列.入队(函数);}

当我创建这个类的一个实例并调用 Add 方法时,它适用于没有参数的函数,例如:functionQueue.Add(方法);,但是当调用具有参数和返回值的方法时(在我的情况下,ClassType 作为参数,Boolean 作为返回值),例如 functionQueue.Add(Method2(classObject));它没有编译,我错过了什么?

解决方案

因为使用 functionQueue.Add(Method2(classObject)) 您将调用的结果排入队列,而不是调用本身.p>

要将带有参数的方法加入队列,您应该更改 Add 原型以接受参数(并将它们与委托一起存储).作为替代方案,您可以使用 lambda:

functionQueue.Add(() => Method2(classObject));

(那么你的第二次重载 Add 是没用的,你总是可以排队一个 Action 你在闭包内提供所有参数).

更新
这种类型的队列的一个例子是在 WinForms 内部,从主线程以外的其他线程分派方法是使用 方法队列 完成的(查看 Control.MarshaledInvoke 的反汇编).跳过同步和上下文,它会保留一个 System.Collections.Queue,其中每个条目都是 ThreadMethodEntry(用于保存所需数据的结构).

I have written a class called QueueManager:

class QueueManager
{
    Queue functionsQueue;

    public bool IsEmpty 
    { 
        get 
            {
                if (functionsQueue.Count == 0)
                    return true;
                else
                    return false;
            }
    }

    public QueueManager()
    {
        functionsQueue = new Queue();
    }

    public bool Contains(Action action)
    {
        if (functionsQueue.Contains(action))
            return true;
        else 
            return false;
    }

    public Action Pop()
    {
        return functionsQueue.Dequeue() as Action;
    }

    public void Add(Action function)
    {
        functionsQueue.Enqueue(function);
    }

    public void Add(Func<CacheObject,Boolean> function)
    {
        functionsQueue.Enqueue(function);
    }

and when I create an instance of this class and call Add method it works fine for functions with no arguments, for example: functionQueue.Add(Method); , but when calling on methods that have an argument and return value(in my case ClassType as argument, and Boolean as return value), for example functionQueue.Add(Method2(classObject)); it does not compile, what am I missing?

解决方案

Because with functionQueue.Add(Method2(classObject)) you queue the result of your call, not the call itself.

To enqueue a method with parameters you should change the Add prototype to accept parameters (and store them together with the delegate). As alternative you can use lambdas:

functionQueue.Add(() => Method2(classObject));

(then your second overload of Add is useless, you can always queue an Action where you give all the parameters inside the closure).

Update
An example of a queue of this type is inside WinForms, dispatching of methods from other threads than the main thread is done with a method queue (look at the disassembly of Control.MarshaledInvoke). Skipping synchronization and contexts it keeps a System.Collections.Queue where each entry is ThreadMethodEntry (a structure used to hold needed data).

这篇关于C# 创建函数队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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