IProgress synchronization

后端 未结 4 1752
夕颜
夕颜 2021-01-11 11:58

I have the following in C#

public static void Main()
{
    var result = Foo(new Progress(i =>
        Console.WriteLine(\"Progress: \" + i)));
         


        
4条回答
  •  [愿得一人]
    2021-01-11 12:16

    As pointed out several times before by other answers, it's due to how Progress is implemented. You could provide your clients (users of the library) with example code, or an implementation of IProgress for a console project. This is basic, but should do.

    public class ConsoleProgress : IProgress
    {
        private Action _action;
    
        public ConsoleProgress(Action action) {
            if(action == null) {
                throw new ArgumentNullException(nameof(action));
            }
    
            _action = action;
        }
    
        public void Report(T value) {
            _action(value);
        }
    }
    

提交回复
热议问题