これは、次の質問に関連しています: カスタム メッセージ ポンプを作成するには?
基本的に同じメッセージ ポンプが必要ですが、入力パラメーターもサポートできる必要があります。上記の質問からの回答は、パラメーターを受け入れない Action() デリゲートのみをサポートします。アクションにパラメータを渡せるようにしたい。パラメータなしのバージョンは次のとおりです。
public class MessagePump
{
private BlockingCollection<Action> actions = new BlockingCollection<Action>();
public void Run() //you may want to restrict this so that only one caller from one thread is running messages
{
foreach (var action in actions.GetConsumingEnumerable())
action();
}
public void AddWork(Action action)
{
actions.Add(action);
}
public void Stop()
{
actions.CompleteAdding();
}
}
これを行う正しい方法は何ですか?BlockingCollection に Action の代わりにカスタム クラスを格納することを考えていました。
class ActionWithParameter
{
Action action;
object parameter;
}
しかし、それは不格好に思えます。加えて、action(parameter) を呼び出せるようにするために、アクションを取得するときにパラメータの型を把握するための switch ステートメントも必要になります。また、複数のパラメーターをサポートしたい場合はどうすればよいですか? 私はobject[] parameters
それを使用する必要がありますか?きっともっと良い解決策がありますか?