where to thread..

  • Thread starter Thread starter Mark Broadbent
  • Start date Start date
M

Mark Broadbent

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?

--


Br,
Mark Broadbent
mcdba , mcse+i
=============
 
Mark Broadbent said:
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.
 
thx Jon

--


Br,
Mark Broadbent
mcdba , mcse+i
=============
 
Hi, Mark

if method occupies CPU 100% there is no sense to build complexities around
it just like Jon remarked. If method uses IO or other devices, which cause
execution of other processes or drivers, you should consider to implement
such method as asynchronous. In your callback you can check then if caller
defined event handler and invoke event if yes. Take a look at example or
stream Read and BeginRead.

HTH
Alex
 
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/d...pconasynchronousprogrammingdesignpattern2.asp

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Mark Broadbent said:
thx Jon

--


Br,
Mark Broadbent
mcdba , mcse+i
=============
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top