Solved

An Action (callback) with parameters and return value

Posted on 2013-11-14
6
386 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now