How to make the cleanest code when reporting progress to a user?

后端 未结 13 1487
失恋的感觉
失恋的感觉 2021-02-08 09:10

I\'ve struggled for the last couple of months to come up with some clean code to report progress to a user. Everything always seems to boil down to:

ReportProgr         


        
13条回答
  •  失恋的感觉
    2021-02-08 09:52

    set up your tasks as an event stream, and have the event-processing 'engine' report progress. Each event instance can have its own name, progress-reporting blurb/template, etc. if you want to go that far

    if this is a pattern that occurs often, it is worth the effort for the infrastructure. When you're done, the usable code might look something like:

    EventQueue tasks = new EventQueue();
    tasks.Add(new TaskEvent(this.doTask1,"Foo-ing the bar"));
    tasks.Add(new TaskEvent(this.doTask2,"Bar-ing the foo"));
    tasks.Add(new TaskEvent(this.doTask3,"Glitching and whinging"));
    ...
    tasks.Execute(this.ProgressEventHandler);
    

提交回复
热议问题