Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

An Action (callback) with parameters and return value

Posted on 2013-11-14
6
Medium Priority
?
396 Views
Last Modified: 2013-11-14
Hi Experts,

How would I go about creating/using an Action (callback) with parameters and return value.

So here's what I am trying to accomplish:

I have a UI exe and a DLL that performs syncing operations in a background thread.  Every now and again the syncing thread needs input from the user.  What I am thinking about doing, is when the UI instantiates the background thread, it would pass it a function pointer/Action (pointing to one of the UI’s functions that would pop up a message box for the user).  Then from that point on, any time the syncing thread needs to interact with the user, it’ll use this callback function to interact with the user.  The user would click Yes/No and then that would be returned to the thread and the read would zig or zag based on the user’s input.  How would the Action declaration look like for this and how would the class constructor (accepting the function pointer) look like?

I am thinking something like this but I know I’m missing the return value:

(Please ignore the simplification of the thread part)

public class SyncerClass
{
     private Action<MyParameterClass> _useMessageCallback;

     public SyncerClass(Action<MyParameterClass> useMessageCallback)
     {
          _useMessageCallback = useMessageCallback;
          Thread.StartThread(WorkerThread);
     }

     WorkerThread()
     {
      while(true)
      {
                         MyParameterClass parameter = new MyParameterClass();
                         arameter.Message = “Would you like me to Zig instead of Zag?”;

                         if(_useMessageCallback(parameter) == DialogResult.Yes)
                         {
                              //Zig
                         }
                         else
                         {
                              //Zag
                         }
               }
     }
}

Thank you!
0
Comment
Question by:axnst2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 22

Accepted Solution

by:
p_davis earned 2000 total points
ID: 39648374
if you want a result you would either have to use func<T, TResult> or a delegate... i would suggest a delegate
0
 

Author Comment

by:axnst2
ID: 39648423
With func it worked!  Thanks!

Why would you prefer the delegate and what would the delegate prototype look like?
0
 

Author Comment

by:axnst2
ID: 39648428
delegate TResult MyDelegate(T)?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:p_davis
ID: 39648451
Its just a preference of mine... if func suites you, use it.
0
 
LVL 22

Expert Comment

by:p_davis
ID: 39648457
func actually is a delegate too, anyway
0
 

Author Closing Comment

by:axnst2
ID: 39648548
Thank you!

I ended up using this:

Func<UIMessageParameters, DialogResult> _displayUserMessage;
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question