C# lightweight timer callback

Hi Experts,

In C#, is there a way to have a function called, without storing any kind of memory variable - just an async callback - or even a lambda function, that will call back in a supplied time in milliseconds.  A 2nd criteria is that the object it calls back may no longer exist.

I'm interested in this because I'd like to schedule animations to occur without knowing if the user will exit the app.  I want the callback to die gracefully and would love to know a way to write this code super-simply if possible.

Thank you very much,
Mike
LVL 1
threadyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
WinForms, WPF, WebForms?...something else?
threadyAuthor Commented:
Just pure C# logic.  This is for a portable class library I'm writing for Xamarin that is platform/API unaware.  Thanks!
David Johnson, CD, MVPOwnerCommented:
use a try/catch if out of scope then gracefully exit
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

threadyAuthor Commented:
Let me rephrase my question, now that I've got a better understanding of what I need at this point.

Can I write a function in C#, that will take some function with its input parameters as input?

Let's say I want to have some arbitrary functions like:
MyFunc(1, 2, "hello")
AnotherFunc(25.3, "have a good day", "here's another string", 22)

Can I pass this function off to a function that takes arbitrary functions that will get called later when some event occurs?

I'm not at all interested in events - just the syntax to call these functions later on....  
In javascript, this is super simple.  Not sure if this is possible in C#.

Thanks!
Mike
David Johnson, CD, MVPOwnerCommented:
MyFunc(1, 2, "hello")
AnotherFunc(25.3, "have a good day", "here's another string", 22)


void Myfunc(int one,int two,string hello){
//todo: do something here
}
AnotherFunc(double dblone,string strGreeting,string strAnother, int param4)
{
//todo: do something here
}

Can I pass this function off to a function that takes arbitrary functions that will get called later when some event occurs? You would have to have the variables set as global otherwise they would be out of scope.  Global variables are highly frowned upon by the code nazis
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
You might be looking for Delegates...but we'd need more details about how you'd actually do this to be sure:
https://msdn.microsoft.com/en-us/library/ms173171.aspx
threadyAuthor Commented:
I'm looking for what JavaScript has but in c#.  You can pass a function that exists nowhere else- as an argument to another function.

Something like this
B(A(str) { print(str); } );

Void B(func f){
   //do some work
  f("hello"):   // prints hello
}
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Sure,  that's called an anonymous method/delegate:
https://msdn.microsoft.com/en-us/library/0yw3tz5k.aspx

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
threadyAuthor Commented:
Sweet!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.