Mark,
If you are going to fire another thread, then I would recommend that you
use the asynchronous pattern that is currently in use in the framework.
Basically, it involves creating Begin<methodname> and End<methodname>
methods which would be called to start and stop the asynchronous call.
This is rather easy to implement. All you have to do is create a
delegate with a signature that matches the method you want to make
asynchronous. Once you have that, have your Begin and End methods just
create a delegate on your method, and return the results of the calls to
BeginInvoke and EndInvoke.
You will also need to store the delegate per call. You should use a
hashtable for this.
Here is an example.
// The method that is to be made asynchronous.
public int DoSomething(string param1, ref int param2, out int param3)
{
}
// The delegate represending the method.
private delegate int DoSomethingDelegate(string param1, ref int param2, out
int param3);
// Store a hashtable which is keyed on the IAsyncResult, and returns
private Hashtable mobjAsyncDoSomethingCalls = new Hashtable();
// The declaration of BeginDoSomething.
public IAsyncResult BeginDoSomething(string param1, ref int param2, out int
param3, AsyncCallback callback, object state)
{
// Create a delegate on the DoSomething method.
DoSomethingDelegate pobjCall = new DoSomethingDelegate(DoSomething);
// The return call. Store in the hashtable.
IAsyncResult pobjRetVal = pobjCall.BeginInvoke(param1, ref param2, out
param3, callback, state);
// Place in the hashtable.
mobjAsyncDoSomethingCalls[pobjRetVal] = pobjCall;
// Begin the result.
return pobjRetVal;
}
// The call to EndDoSomething.
public int EndDoSomething(ref int param2, out int param3, IAsyncResult
result)
{
// Get the item from the hashtable. If it doesn't exist, then raise an
exception.
// Also, if the result argument is null, raise an exception.
// That part is left to the reader.
// Get the item.
DoSomethingDelegate pobjCall = (DoSomethingDelegate)
mobjAsyncDoSomethingCalls[result];
// Remove the item from the hashtable.
mobjAsyncDoSomethingCalls.Remove(result);
// Finish off the call.
return pobjCall.EndInvoke(ref param2, out param3, result);
}
For more information, check out the section of the .NET framework
documentation titled "Asynchronous Programming Design Pattern", located at
(watch for line wrap):
http://msdn.microsoft.com/library/de...gnpattern2.asp
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
-
mv*@spam.guard.caspershouse.com
"Mark Broadbent" <no************@no-spam-please.com> wrote in message
news:OO**************@TK2MSFTNGP09.phx.gbl...
thx Jon
--
Br,
Mark Broadbent
mcdba , mcse+i
=============
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... Mark Broadbent <no************@no-spam-please.com> wrote: I have written a class that has a public method which could take some time to complete (20 secs). The idea is for it to raise an event on completion. Should I create a separate thread for this lengthy operation inside
the class itself OR as I suspected let any calling code of the class's method worry about threading?
I would let the caller worry about the threading, but document that the
method could take some time to complete. In some situations (eg if the
main app has already created a worker thread) there may well be no need
to create a new thread.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too