开发者

Simple Delegate Not Working

开发者 https://www.devze.com 2022-12-10 12:14 出处:网络
class SimpleDelegate { public delegate void LogHandler(string message); public void Process(LogHandler logHandler)
class SimpleDelegate
{
  public delegate void LogHandler(string message);

  public void Process(LogHandler logHandler)
  {
    if (logHandler != null)
    {
      Console.WriteLine("Process begin");
    }

    if (logHandler != null)
    {
      Console.WriteLine("Process end");
    }
  }
}

class FileLogger
{
  FileStream fileStre开发者_运维问答am;
  StreamWriter writer;

  public FileLogger(string fileName)
  {
    fileStream = new FileStream(fileName, FileMode.Create);
    writer = new StreamWriter(fileStream);
  }

  public void Logger(string s)
  {
    writer.WriteLine(s);
  }

  public void Close()
  {
    writer.Close();
    fileStream.Close();
  }
}

class Program
{
  static void Main(string[] args)
  {
    SimpleDelegate cp = new SimpleDelegate();

    FileLogger fl = new FileLogger(@"C:\TEMP\MyLog.log");

    SimpleDelegate.LogHandler handler = null;
    handler += new SimpleDelegate.LogHandler(Logger);
    handler += new SimpleDelegate.LogHandler(fl.Logger);

    cp.Process(handler);

    fl.Close();
  }

  static void Logger(string s)
  {
    Console.WriteLine("writing s " + s);
  }
}

On executing I get following output:

Process begin

Process end

Press any key to continue . . .

Actually it should be:

Process begin

Process end

Process begin

Process end

Press any key to continue . . .

I am not able to figure out the mistake :(


You're only calling SimpleDelegate.Process once - why would you expect to see output twice?

You're never actually invoking the delegate... just testing it for nullity. You're testing that twice, once before writing "Process begin" and once before writing "Process end", but that's all.


I'm not sure if I'm getting the point ...

But you never call the logHandler in this code:

    public void Process(LogHandler logHandler)
    {
        if (logHandler != null)
        {
            Console.WriteLine("Process begin");
        }

        if (logHandler != null)
        {
            Console.WriteLine("Process end");
        }
    }


I believe your confusion is around the fact that you called "LogHandler += ..." twice, but are only getting two outputs. Notice that the Process function you are calling doesn't do a loop that uses the LogHandler! That's why you're not getting the results you expect.

Also, the model you are using is just slightly off. Instead of directly using the delegate, it is better to create an event and subscribe to that.

public delegate void LogHandler(string message);
public event LogHandler OnLog;
...
OnLog += ...


I think this is what is happening

    SimpleDelegate cp = new SimpleDelegate();

    FileLogger fl = new FileLogger(@"C:\TEMP\MyLog.log");

    cp.Process(null);
    fl.Close();

That is handler is null, so that code never gets executed. You see your message once because of the call to cp.Process(null)

0

精彩评论

暂无评论...
验证码 换一张
取 消