I’m trying to create a form that will animate something while processing a particular task (passed as a delegate to the constructor). It’s working fine, but the problem I’m having is that I can’t instantiate a copy of my generic class if the particular method that I want to perform has a return type of void.
I understand that this is by design and all, but I’m wondering if there is a known workaround for situations like this.
If it helps at all my windows form looks like so (trimmed for brevity):
public partial class operatingWindow<T> : Form { public delegate T Operation(); private Operation m_Operation; private T m_ReturnValue; public T ValueReturned { get { return m_ReturnValue; } } public operatingWindow(Operation operation) { /*...*/ } }
And I call it like:
operatingWindow<int> processing = new operatingWindow<int>(new operatingWindow<int>.Operation(this.doStuff)); processing.ShowDialog(); // ... private int doStuff() { Thread.Sleep(3000); return 0; }
I would rethink your design slightly.
If you implemented the processing in a base class, you could subclass it with 2 alternatives that would be nearly interchangable.
The first could be like yours, where it takes an Operation that returns a value. (I would rework this to use
Func<T>instead of having your own delegate type, though.The second could just take a simple Action and not provide a return value. Both could use the same animation/display/etc. routines, but provide a different way of working. You could also pass this off to other methods using the base class, which would provide a lot of flexibility.
With this approach, you could call this like:
This would look something like: