1

我正在尝试编写一个程序,其中步骤 1 -> 启动步骤 2 -> 启动步骤 3。变量在每个步骤之间传递。

我可以在 C# 中以这种方式使用事件吗?编写执行此操作的程序的最佳方法可能是什么?

public class ProgramFlow // the listener program
{
    EventArgs args = null;
    public delegate void EventHandler(string str, EventArgs e);
    public static event EventHandler Step1Reached;
    public static event EventHandler Step2Reached;        
    public ProgramFlow()
    {
        Step1 step1 = new Step1();  
        // Print string and kick off Step2 
        Step2 step2 =new Step2();
        // Print String kick off next step 
    }
}

public class Step1
{
    string charRead;

    public Step1()
    {
        Console.Write("Input something for Step1: ");
        charRead = Console.ReadLine();
        Console.WriteLine();            
        ProgramFlow.Step1Reached += ProgramFlow_Step1Reached;
    }

    void ProgramFlow_Step2Reached(string str, EventArgs e)
    {
        Console.WriteLine(charRead);
    }

}

public class Step2
{
    string charRead;
    public Step2()
    {
        Console.Write("Input something for Step2: ");
        charRead = Console.ReadLine();
        Console.WriteLine();
        ProgramFlow.Step2Reached += ProgramFlow_Step2Reached;
    }

    void ProgramFlow_Step2Reached(string str, EventArgs e)
    {
        Console.WriteLine(charRead);
    }
}
class Program
{
    static void Main(string[] args)
    {
        ProgramFlow programFlow = new ProgramFlow();
        Console.ReadKey();              
    }
}
4

2 回答 2

1

连接这些步骤并让您的程序流程协调它们的执行是否符合您的要求,例如

public class ProgramFlow // the listener program
{
    public ProgramFlow()
    {
        IStep[] steps = new IStep[] { new Step1(), new Step2() };

        foreach (var step in steps)
        {
            step.Step();
            step.StepResult();
        }
    }
}

public interface IStep
{
    void Step();
    void StepResult();
}

public class Step1 : IStep
{
    string stringRead;

    public void Step()
    {
        Console.Write("Input something for Step1: ");
        stringRead = Console.ReadLine();
        Console.WriteLine();
    }

    public void StepResult()
    {
        Console.WriteLine(stringRead);
    }
}

public class Step2 : IStep
{
    string stringRead;

    public void Step()
    {
        Console.Write("Input something for Step2: ");
        stringRead = Console.ReadLine();
        Console.WriteLine();
    }

    public void StepResult()
    {
        Console.WriteLine(stringRead);
    }
}

class Program
{
    static void Main(string[] args)
    {
        ProgramFlow programFlow = new ProgramFlow();
        Console.ReadKey();
    }
}
于 2013-08-20T03:33:42.600 回答
0

当您希望在不派生新对象的情况下提供可扩展性时,事件将帮助您,并且在您希望发生多个操作时非常有用。当您使用乱序执行时,事件可以提供帮助(尽管异步也可以帮助您)。如果您有任何故障,或者如果您想向 GUI 报告进度,那么可以,事件是实现这一目标的好方法。

老实说,如果您需要按顺序执行操作 1、操作 2 和操作 3,如果您简单地编写代码是不是更简单、更易于维护:

public static void Main()
{
     var arg1 = ReadAction1();
     Action1(arg1);
     var arg2 = ReadAction2();
     Action2(arg2);
     var arg3 = ReadAction3();
     Action3(arg3);
}

也就是说,这将带您走上一条非常必要的道路。我想看看您是否可以以 OO 方式表示您的任务,以便您可以获得可以交换和测试等的东西。

于 2013-08-20T03:37:03.067 回答