English 中文(简体)
C# 创建函数队列
原标题:C# creating function queue

我写了个班 叫做"管理队"

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?

最佳回答

因为使用 conferenceQuee.Add (Method2( 类Object)) 您会排队等待您的呼叫结果, 而不是电话本身 。

要输入带有参数的方法, 您应该更改 < code> Add 原型以接受参数( 并与代表一起存储) 。 作为替代, 您可以使用 ambdas :

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

(然后您第二次超载 Add 是没用的, 您总是可以排队 : Action , 在关闭内给出所有参数 ) 。

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).

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...