473,386 Members | 1,803 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,386 software developers and data experts.

I want to thread heavily

I am developing a new application.

Since it is being developed from scratch, I want to implement a lot of
threading - especially for my SQL calls.

The problem I noticed is that I can't seem to interact with data that is in
a thread, and all data that goes to a thread must be static - hence my
application can not access it.

Is there a place that spells out how to thread my routines and make it so
that I can pass data to it and read the resulting data that results?

My preferred language is C# or VB, using Visual Studio 2005.

Thanks for the help!
Jun 27 '08 #1
5 1063
jp2msft wrote:
I am developing a new application.

Since it is being developed from scratch, I want to implement a lot of
threading - especially for my SQL calls.
Threading is not some sort of fairy dust that will make your application
good if you sprinkle lots of it. Threading is never a goal, it's a means, so
starting out with the idea that you want to have "a lot of threading" is
broken to begin with. What you want is the least threading that still meets
your goals, because threading adds complexity.

In particular, if your calls all go to the same database, threading is
unlikely to be of much help, because the database will only have a limited
capacity for processing your requests in parallel. Your average server will
of course have tremendous parallel capacity, but a) you'll have to share it
with everyone else and b) your requests will probably get through *faster*
if you execute them one by one rather than forcing the DB to do them in
parallel.

That's not to say executing DB requests in parallel in a single application
never has any merit, but if your goal is just to increase throughput, it's
probably sufficient to use a limited number (possibly just one) of
asynchronous calls (.BeginExecuteReader() and the like) and leave the rest
to the thread pool.

If you just want to ensure your UI remains response as you're processing
data, using asynchronous calls, BackgroundWorker and/or
ThreadPool.QueueUserWorkItem() should do the trick.
The problem I noticed is that I can't seem to interact with data that is in
a thread, and all data that goes to a thread must be static - hence my
application can not access it.
Where do you get this idea? A thread can access any data that's reachable to
it, and you can start one from any object.

A thread routine is a delegate that has full access to the members of its
declaring class, and on top of that, Thread.Start() and all the asynchronous
method allow you to pass an arbitrary object "state" to store whatever
additional data you require. So:

class A {
int a;

void foo() {
string extraData;
Thread thread = new Thread(threadMethod, extraData);
thread.Start();
}

void threadMethod(object state) {
string extraData = (string) state;
// "a" is accessible here too
}

void bar(SqlCommand c) {
string extraData;
c.BeginExecuteReader(endExecuteReader, c);
}

void endExecuteReader(IAsyncResult ar) {
SqlCommand c = (SqlCommand) ar.AsyncState;
// "a" is accessible here too
using (SqlDataReader r = c.EndExecuteReader(ar)) {
...
}
}
}
Is there a place that spells out how to thread my routines and make it so
that I can pass data to it and read the resulting data that results?
Try googling "asynchronous programming". It's the least bothersome way of
leveraging parallel processing.

--
J.
http://symbolsprose.blogspot.com
Jun 27 '08 #2
Thanks Mr. Mostert.

My goal is to keep the UI responsive. Sometimes our connections are broken
in this manufacturing environment, and this causes the App to hang until the
server request times out.

I'll look into the background worker threads.

ThreadPool.QueueUserWorkItem() is something I've never heard of.

"Jeroen Mostert" wrote:
If you just want to ensure your UI remains response as you're processing
data, using asynchronous calls, BackgroundWorker and/or
ThreadPool.QueueUserWorkItem() should do the trick.

Try googling "asynchronous programming". It's the least bothersome way of
leveraging parallel processing.

--
J.
http://symbolsprose.blogspot.com
Jun 27 '08 #3
jp2msft wrote:
Thanks Mr. Mostert.
Please, either "Jeroen" or "whatsyerface" will do fine.
My goal is to keep the UI responsive. Sometimes our connections are broken
in this manufacturing environment, and this causes the App to hang until the
server request times out.
BackgroundWorker should be good for this, as it was designed for UI
scenarios. In fact, the documentation mentions your very scenario:

"The BackgroundWorker class allows you to run an operation on a separate,
dedicated thread. Time-consuming operations like downloads and database
transactions can cause your user interface (UI) to seem as though it has
stopped responding while they are running. When you want a responsive UI and
you are faced with long delays associated with such operations, the
BackgroundWorker class provides a convenient solution."
ThreadPool.QueueUserWorkItem() is something I've never heard of.
It's quite useful, but also quite specialized -- it's a "fire and forget"
way of doing something in the background, and the threadpool will ensure
these tasks will run with optimal parallelism for the system.

Unfortunately, the "forget" part means you have to implement synchronization
yourself if you want to know if/when the work was done. For most scenarios
asynchronous calls are more appropriate, and for applications with a UI
BackgroundWorker can do the heavy lifting, since it was designed with
progress reporting in mind.

--
J.
http://symbolsprose.blogspot.com
Jun 27 '08 #4
Jeroen,

I've found a very through example on the BackgroundWorker Class in the VS
Help, but I have a question that it does not address.

I have at least three (3) different, time-consuming tasks that I'd like to
use the BackgroundWorker Class on. Two are for database queries, and one is
for text validation.

My question is: Can the one BackgroundWorker Class be used for all three (3)
of these tasks? Some of the tasks do, on occasion, call one of the other
tasks.

I see in the BackgroundWorker_DoWork method that there is a "sender" object.
Would the "sender" be the name of the function that I used to call the
BackgroundWorker's RunWorkerAsync method?

Or, should I drop a separate BackgroundWorker component on the form for each
of my time-consuming tasks?

Did this question make sense?
Jun 27 '08 #5
jp2msft wrote:
I've found a very through example on the BackgroundWorker Class in the VS
Help, but I have a question that it does not address.

I have at least three (3) different, time-consuming tasks that I'd like to
use the BackgroundWorker Class on. Two are for database queries, and one is
for text validation.

My question is: Can the one BackgroundWorker Class be used for all three (3)
of these tasks? Some of the tasks do, on occasion, call one of the other
tasks.
If you're already running a task in the background, then there's no point to
having that task start its dependent task in the background separately. I'll
demonstrate what I mean shortly.
I see in the BackgroundWorker_DoWork method that there is a "sender" object.
Would the "sender" be the name of the function that I used to call the
BackgroundWorker's RunWorkerAsync method?
No, "sender" in an event handler is always the object that triggered the
event. You can have multiple BackgroundWorkers share the same event handler,
and "sender" would contain the BackgroundWorker that the event is actually
for. Let's put this aside for a minute.

What you could do is create your own methods that perform whatever
long-running task you need, possibly returning data. Let's say you have this:

DataTable getLotsOfData() { ... }
ValidationResults validateLotsOfText(string lotsOfText) { ... }

Now, when you need to perform one of these tasks from your UI thread, you
can call on a BackgroundWorker. You can create these programmatically or
drag them as components. Let's say you use components, as it's a little
easier. You can do it all in one BackgroundWorker, but this is harder to
program -- it's easier to use separate BackgroundWorkers for separate tasks.
(If you have lots of different tasks that need to run this way, or more than
one instance of the same task, you should create BackgroundWorkers from your
code rather than using design-time components.)

So let's say you have two BackgroundWorker components, one for LotsOfData
and the other for LotsOfText. When you need to start either of these
activities from your UI, you call .RunWorkerAsync() on the appropriate
BackgroundWorker:

GetLotsOfDataBackgroundWorker.RunWorkerAsync();
ValidateLotsOfTextWorker.RunWorkerAsync(lotsOfText );

The event handler for ValidateLotsOfTextWorker might look like this, calling
your method:

private void ValidateLotsOfTextWorker_DoWork(object sender,
DoWorkEventArgs e) {
string lotsOfText = (string) e.Argument;
e.Result = validateLotsOfText(lotsOfText)
}

When this is done, the worker's RunWorkerCompleted event will fire:

private void ValidateLotsOfTextWorker_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e) {
if (e.Error != null) {
// Handle the error
} else {
ValidationResults v = (ValidationResults) e.Result;
// do something with the results
}
}

Now let's say you do the same for LotsOfData, but LotsOfData needs to
validate LotsOfText as part of its operation. Simply call that method
directly from within the DoWork handler:

private void GetLotsOfDataBackgroundWorker_DoWork(object sender,
DoWorkEventArgs e) {
// go to the database and get LotsOfText here
ValidationResults v = validateLotsOfText(lotsOfText);
}

When you want a long-running task done from your UI, use the
BackgroundWorker. If you're already in the background, you don't need to
bother with that and you can simply call methods as you're used to. There's
no need for the *background* task to be responsive, after all.

There's one thing that won't work very well if you do it like this, and
that's progress reporting. If you want your long-running task to report
progress, things get more complicated because you'll want your methods to
call on a BackgroundWorker to do that. But you can't call on just any
BackgroundWorker, because your UI thread might be using it -- you need to
pass the BackgroundWorker to the dependent method. Once you've reached this
point it's probably easier to create BackgroundWorker instances yourself and
not use components. If you don't need to report progress, the above method
will do fine.

--
J.
http://symbolsprose.blogspot.com
Jun 27 '08 #6

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

Similar topics

0
by: Fortepianissimo | last post by:
Just a quick question: are popen*() thread-safe? I'm using popen() and popen2() in a heavily threaded script and seem to have some stability problem (just hangs). (also it looks like the python...
15
by: aurora | last post by:
This may sound a little crazy. I capture the output of one class by redirecting the sys.stdout. However the is another threading running at the same time and occasionaly it output some messages to...
8
by: Ola Natvig | last post by:
Anybody out there who knows if the 4suite implementation of XSLT are a threadsafe one? -- -------------------------------------- Ola Natvig <ola.natvig@infosense.no> infoSense AS / development
4
by: Sidd | last post by:
Hello, I was recently reading an article on threading in python and I came across Global Interpreter Lock,now as a novince in python I was cusrious about 1.Is writing a threaded code in python...
5
by: Droopy | last post by:
Hi, I am writing a C# program that is calling C++ legacy code. I wrote a C++ managed wrapper for this legacy code. It seems to work well. The legacy code is handling serial (RS232 or RS422)...
18
by: Urs Vogel | last post by:
Hi I wrote an application server (a remoting sinlgeton), where processes must be stopped in very rare cases, done thru a Thread.Abort(). Occasionally, and only after a Thread.Abort(), this...
182
by: Jim Hubbard | last post by:
http://www.eweek.com/article2/0,1759,1774642,00.asp
23
by: Boltar | last post by:
Hi I'm writing a threading class using posix threads on unix with each thread being run by an object instance. One thing I'm not sure about is , if I do the following: myclass::~myclass() {...
8
by: Markus | last post by:
Hello everyone. Recently I stumbled upon an interesting problem related to thread-parallel programming in C (and similarily C++). As an example assume a simple "buffer" array of size 8, e.g....
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...

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.