3 Replies - 825 Views - Last Post: 04 October 2010 - 04:48 PM Rate Topic: -----

#1 CodeGuru1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-October 10

A function that reports its progress, is this a bad approach?

Posted 04 October 2010 - 12:56 PM

Lets say you have some function that exists in a class library. It will do some work, and should be able to report its progress to the caller.

        public static void DoSomeWork(Func<double, int> SetProgress)
        {

        }


I know you can use a worker thread to report progress, but what if this function doesn't know if its being called on a worker thread?

I guess this method is kind of a throwback to my c/c++ days.

Are there any drawbacks to using this approach to reporting progress?

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: A function that reports its progress, is this a bad approach?

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: A function that reports its progress, is this a bad approach?

Posted 04 October 2010 - 04:17 PM

You can always just raise an event. It doesn't matter if someone is listening, just raise the event anyway.


Quick and easy custom events
Bulding an application - Part 1
Was This Post Helpful? 0
  • +
  • -

#3 CodeGuru1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-October 10

Re: A function that reports its progress, is this a bad approach?

Posted 04 October 2010 - 04:38 PM

Events is the way I do most of it now, but I'm liking this approach better.

When using the function I can simply do:

DoSomeWork(SetProgress);
or
DoSomeWork(null);

private int SetProgress(double Progress)
{
}

It just seems a lot cleaner than using an event and assigning the event to that function.
ie:
.eventname += ....SetProgress();

And, it avoids having to define the event for a static function. I know with static functions there are a few hoops you need to jump through for events.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: A function that reports its progress, is this a bad approach?

Posted 04 October 2010 - 04:48 PM

Its basically the same thing. When you raise a SetProgress it is an event, that has a double as the argument. You still have to have a handler method to do something with that value, like put it in a progress bar.

Like you say... you are already doing lots of events. Personally I like going into an application knowing I did everything the same way. 3 years from now I don't want to spend half the day wondering... "I did this and this with events.. I did this with a postback... I did this with a message pump... "
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1