We help IT Professionals succeed at work.

C# lightweight timer callback

624 Views
Last Modified: 2015-07-29
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
Comment
Watch Question

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
WinForms, WPF, WebForms?...something else?

Author

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, CDSimple Geek from the '70s
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
use a try/catch if out of scope then gracefully exit

Author

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, CDSimple Geek from the '70s
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
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, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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

Author

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
}
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Sweet!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.