473,516 Members | 3,064 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

where to thread..

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
=============
Nov 16 '05 #1
5 1381
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
Nov 16 '05 #2
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

Nov 16 '05 #3
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 Broadbent" <no************@no-spam-please.com> wrote in message
news:ut**************@TK2MSFTNGP10.phx.gbl...
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
=============

Nov 16 '05 #4
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


Nov 16 '05 #5


--
Br,
Mark Broadbent
mcdba , mcse+i
=============
"Mark Broadbent" <no************@no-spam-please.com> wrote in message
news:ut**************@TK2MSFTNGP10.phx.gbl...
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
=============

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2058
by: Ziaran _ | last post by:
I have written a new debugger. I think people will find it useful. Where can I post it for people to download? Thanks, Nir _________________________________________________________________ The new MSN 8: smart spam protection and 2 months FREE* http://join.msn.com/?page=features/junkmail
6
1812
by: S. David Rose | last post by:
Hello All! I am new to Python, and wanted to know if I might ask you a question regarding timing. I want a main loop which takes photos from 8 different web-cams, each can be addressed by http://ip-addr/image.jpg. That's the easy part, but I want to have 2 cameras poll 3-times a second, 4 cameras poll 2 times a second, and the remaining 2...
20
2572
by: Rich Grise | last post by:
I've been lurking for awhile, and I notice that there are some real tight-assed prigs around here. Bitch, bitch, bitch. So what if it's not "Standard C?" If it looks like C, smells like C, quacks like C, and compiles like C, it's probably close enough.
16
1587
by: mikelinyoho | last post by:
Regards: Where the code trouble is? #include <windows.h> #include <stdio.h> #include <stdlib.h> #include <string.h>
5
1453
by: Kallis | last post by:
Dear all I have an thread updating the GUI thread using the BeginInvoke mechanism. However, in one app where I use this mechanism, it works fine but in another it does not. The code looks like: public void OnDisplay(GUIEventArgs args) { object list = {args}; // We get here without problem. We are still in the other Thread // Now switch...
7
1859
by: Mr. Mountain | last post by:
In the following code I simulate work being done on different threads by sleeping a couple methods for about 40 ms. However, some of these methods that should finish in about 40 -80 ms take as long as 2300 ms to complete. This is fairly rare, but the test code below will definitely show it. Somehow, I must not have my design right. The...
5
2595
by: dpomt | last post by:
When the ASP.NET menu is rendered on downlevel browers, the text "^ up one level" is displayed. Any ideas how I can change that text? I did not find a property for the menu control where I can change it. Dieter
4
2576
by: dumbkiwi | last post by:
I have written a script that uses the urllib2 module to download web pages for parsing. If there is no network interface, urllib2 hangs for a very long time before it raises an exception. I have set the socket timeout with socket.setdefaulttimeout(), however, where there is no network interface, this seems to be ignored - presumably,...
22
2279
by: Christopher Nelson | last post by:
I have a little menu system which essentially takes HTML like: <div id='foo'></div> and retrieves foo.shtml from the server and inserts it inside the <div>. But sometimes I'd like foo.shtml to look like: <script language='JavaScript'> ...do something AJAX-y </script>
0
7276
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7182
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7581
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7548
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5110
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4773
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3259
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
825
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
488
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.