473,385 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Threading - Is This Clever or Stupid?

Hi All,

Just a general threading question. Would it be considered good programming
practice, or a bad idea, or whatever, to create a thread with a special name
in order to "pass parameters" to the thread.

In other words, something like this simple example:

btnRunThreads_Click(object sender, EventArgs e)
{
for (int i = 1; i <= 10; i++)
{
ThreadStart workerStart = new ThreadStart(StartMethod);
Thread workerThread = new Thread(workerStart);
workerThread.Name = "Worker_" + i.ToString();
workerThread.Start();
}
}

void StartMethod()
{
Thread thisThread = Thread.CurrentThread;
string threadName = thisThread.Name;
string Task = threadName.Substring(threadName.IndexOf("_") + 1);

switch (Task)
{
case "1":
DoTaskOne();
break;

case "2":
DoTaskTwo();
break;

// More cases, etc.

case "10":
DoTaskTen();
break;

default:
break;
}
}

More "parameters" could be added by continuing to use a separator character
(probably an underscore, since it needs to be a valid thread name), and using
(for instance) the string.split command to enumerate them.

What say you, oh wise ones?

Thanks,
pagates
Nov 17 '05 #1
3 1021
"pagates" <pa*****@discussions.microsoft.com> wrote in message
news:53**********************************@microsof t.com...
Hi All,

Just a general threading question. Would it be considered good programming practice, or a bad idea, or whatever, to create a thread with a special name in order to "pass parameters" to the thread.


Bad in so many ways ;¬)

Because ThreadStart methods can't take arguments, what you would normally do
is set up member variables to hold the arguments, then call a parameterless
method which effectively collects those variables and pass them into your
real worker method.

Another thing, it is usually considered bad practice to create threads in a
loop.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
Nov 17 '05 #2
I would not call myself a wise one. Although I like the idea. Bearded
with white hair blowing in the wind as I sit atop a mountain reflecting
on the passing of time.

Ah f@ck it, I'll probably turn out to be a dirty old man who whinges
about the good ole days and always forgets where he left his false
teeth.

Anyways, IMHO, your approach should run OK but I would find it hard to
read if I needed to maintain the code. I would be inclined to split
things up before you start off the threads. You should be able to work
out what you are going to do before you set off the thread, so you can
kick off the relevant routine straight away.

Here are some words of wisdom for you - Man who go through turnstiles
sideways going to Bangkok

For what it is worth
Bill

Nov 17 '05 #3
Hi Tim,

Thanks for the feedback. Just some clarifications for me to make...

I realize that "thread loops" aren't a great idea - I was just looking to
make a quick-n-dirty example. Perhaps a better example (although, again,
quick-n-dirty, and off the top of my head) would be a recurisive database
search, based on a parent-child heirarchy.

Assuming proper care was taken to make sure there weren't a runaway number
of threads, a search could be done that says "check all children of this
parent to see if it fits the criteria." Then, a thread could be started for
the children of that parent, and so on. In that case, it seems difficult to
set member variables, as you don't know when a certain search starts or ends.

As an aside, why are threads parameterless (since data is available anyway
by using member variables)?

Thanks again,
pagates

"Tim Haughton" wrote:
"pagates" <pa*****@discussions.microsoft.com> wrote in message
news:53**********************************@microsof t.com...
Hi All,

Just a general threading question. Would it be considered good

programming
practice, or a bad idea, or whatever, to create a thread with a special

name
in order to "pass parameters" to the thread.


Bad in so many ways ;¬)

Because ThreadStart methods can't take arguments, what you would normally do
is set up member variables to hold the arguments, then call a parameterless
method which effectively collects those variables and pass them into your
real worker method.

Another thing, it is usually considered bad practice to create threads in a
loop.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton

Nov 17 '05 #4

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

Similar topics

2
by: Ben Floyd | last post by:
Hey everyone, Why doesn't this work? The code speaks much more clearly than I do, so i shortened it and pasted it below. Running this and clicking on 'Break Me' will... freak out the window......
26
by: JKop | last post by:
Why the hell did they make "this" a pointer and not a reference?!! It's so stupid! -JKop
3
by: Zach | last post by:
Can anyone suggest a *really* good book on threading. No million pages. No Deitel & Deitel type would-be clever examples. Many thanks, Zach.
4
by: Phillip N Rounds | last post by:
I'm cursed by being a linear thinker. Anyway, I have an application (service) where I can't decide on the appropriate way to implement threading. I have a timer which periodically initiates...
8
by: Yatharth | last post by:
Hi, I m new to threading and i have successfully runed threading but i could display value on my web page ,but its working in code behind when i see it through debugger,plzzzzzzz help me here...
13
by: John | last post by:
I've got some reasonably complex business logic in my C# code, in a class called by a ASP.NET page. This takes around 3-4 seconds to execute. It's not dependent on SQL calls or anything like that....
0
by: Alvin A. Delagon | last post by:
I have a multithreaded application that spawns threads which query a database server. During stress test I encountered some threads failing due "lost connection errors" and sometimes the actual...
2
by: akameswaran | last post by:
Admittedly this problem causes no actual functional issues aside from an occasional error message when the program exits. The error is: Unhandled exception in thread started by Error in...
7
by: Bruce W. Darby | last post by:
I've been doing some reading on threading, using the BackgroundWorker, but I'm getting in over my head a bit. I've run through the tutorial at MSDN and, while it has some good information, it is a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.