Solved

An Action (callback) with parameters and return value

Posted on 2013-11-14
6
392 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 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

733 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