Solved

An Action (callback) with parameters and return value

Posted on 2013-11-14
6
391 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
  • 3
  • 3
6 Comments
 
LVL 22

Accepted Solution

by:
p_davis earned 500 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

766 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