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

threading problem

hi,

i have a problem: i wrote a class that encapsulates communication with
database. i wont go into details, but i wanted to use threads to avoid
unresponsive ui so i did something more or less like this:

public void ExecuteScalar(string query)
{
this.query = query;
this.workingThread.Resume(); //the worker thread is suspended
}

private void WorkerThreadProc()
{
while(!this.threadStop)
{
if (this.query.Length > 0)
{
[check connection]
[retrieve data]
this.result = [assign result]
this.query = string.Empty;
Thread.Sleep(500);
}
}
}

now when thread finishes its job i call this.workingThread.Suspend();
after that the thread is rady to process next query. but then my app
eats whole processor time! i've checkd the workingThread state and its
96 what means it is in WaitSleepJoin state and is Suspended. in that
case why it is eating whole cpu time? does it mean that sleeping is "do
nothing loop"???

please help. how can i solve this problem?
Nov 17 '05 #1
7 1589
Hi,

"SharpCoderMP" <cs*******@interia.pl.NFSPM> schreef in bericht
news:eM**************@TK2MSFTNGP11.phx.gbl...
hi,

i have a problem: i wrote a class that encapsulates communication with
database. i wont go into details, but i wanted to use threads to avoid
unresponsive ui so i did something more or less like this:

public void ExecuteScalar(string query)
{
this.query = query;
this.workingThread.Resume(); //the worker thread is suspended
}

private void WorkerThreadProc()
{
while(!this.threadStop)
{
if (this.query.Length > 0)
{
[check connection]
[retrieve data]
this.result = [assign result]
this.query = string.Empty;
Thread.Sleep(500);
}
}
}

now when thread finishes its job i call this.workingThread.Suspend();
after that the thread is rady to process next query. but then my app
eats whole processor time! i've checkd the workingThread state and its
96 what means it is in WaitSleepJoin state and is Suspended. in that
case why it is eating whole cpu time? does it mean that sleeping is "do
nothing loop"???

please help. how can i solve this problem?


Don't ever call SuspendThread/ResumeThread yourself. An excerpt from the
documentation on these methods:
<quote>
CAUTION Do not use the Suspend and Resume methods to synchronize the
activities of threads. You have no way of knowing what code a thread is
executing when you suspend it. If you suspend a thread while it holds locks
during a security permission evaluation, other threads in the AppDomain
might be blocked. If you suspend a thread while it is executing a class
constructor, other threads in the AppDomain that attempt to use that class
are blocked. Deadlocks can occur very easily.
</quote>

Find another way to accomplish what you want to do. Check the net for
something on threading in .NET, for instance try the following link
http://www.yoda.arachsys.com/csharp/threads/

Kind regards,
PS: I thought that methods were deprecated already?
--
Tom Tempelaere.
Nov 17 '05 #2
You definitely shouldn't be doing what you are doing. Using Resume and
Suspend is not a good way to handle access to a thread. You should be using
something like a mutex here to limit access to one execution at a time.

However, even that is unecessary. Why not just create a method which
will execute the query (taking the query to execute), and then wrap that in
a delegate, passing it to the ThreadPool (specifically, the static
QueueUserWorkItem method on the ThreadPool class). You can pass your query
as your parameter, and then execute.

Also, if you are using .NET 2.0, then you can use the
BeginExecuteReader/BeginExecuteScalar methods on your SqlCommand class.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"SharpCoderMP" <cs*******@interia.pl.NFSPM> wrote in message
news:eM**************@TK2MSFTNGP11.phx.gbl...
hi,

i have a problem: i wrote a class that encapsulates communication with
database. i wont go into details, but i wanted to use threads to avoid
unresponsive ui so i did something more or less like this:

public void ExecuteScalar(string query)
{
this.query = query;
this.workingThread.Resume(); //the worker thread is suspended
}

private void WorkerThreadProc()
{
while(!this.threadStop)
{
if (this.query.Length > 0)
{
[check connection]
[retrieve data]
this.result = [assign result]
this.query = string.Empty;
Thread.Sleep(500);
}
}
}

now when thread finishes its job i call this.workingThread.Suspend();
after that the thread is rady to process next query. but then my app
eats whole processor time! i've checkd the workingThread state and its
96 what means it is in WaitSleepJoin state and is Suspended. in that
case why it is eating whole cpu time? does it mean that sleeping is "do
nothing loop"???

please help. how can i solve this problem?

Nov 17 '05 #3
TT (Tom Tempelaere) wrote:
Don't ever call SuspendThread/ResumeThread yourself. An excerpt from the
documentation on these methods:
<quote>
[...]
</quote>

Find another way to accomplish what you want to do. Check the net for
something on threading in .NET, for instance try the following link
http://www.yoda.arachsys.com/csharp/threads/ first of all i did some research, mainly on codeproject - the articles
were interesting, but i needed something very simple with one or two
treads dedicated for fixed tasks, like processing queries.
thanks for the "yoda" link. i red it some time ago.... just forgot to
take a look there now.
as to the documentation - i use doc shipped with sdk that was shipped
with vs - there is no warning about resume/suspend thread... maybe it
got a bit out of date.

Kind regards,
PS: I thought that methods were deprecated already?

even if it is not good to use them... how could it be possible to do
advenced threading without them?
Nov 17 '05 #4
i was hoping to avoid thread pool...
i tried few things, now i've dropped suspend/resume, and left my thread
running inside this pseudo infinite "while" loop with call to sleep(...)

private void WorkerThreadProc()
{
while(!this.threadStop)
{
if (this.query.Length > 0)
{
[check connection]
[retrieve data]
this.result = [assign result]
this.query = string.Empty;
}
Thread.Sleep(someReasonableAmountOfTime);
}
}

is this also wrong? how can i improve this approach to keep it as simple
as possible? please tell me if thread pool would be more efficent in
this case.

oh, and i don't want to use .net 2.0
anyway this class works with MySql Connector - i don't think they've
gone so far :) you know... MySql.Data.MySqlClient is a bit... buggy :|

Nicholas Paldino [.NET/C# MVP] wrote:
You definitely shouldn't be doing what you are doing. Using Resume and
Suspend is not a good way to handle access to a thread. You should be using
something like a mutex here to limit access to one execution at a time.

However, even that is unecessary. Why not just create a method which
will execute the query (taking the query to execute), and then wrap that in
a delegate, passing it to the ThreadPool (specifically, the static
QueueUserWorkItem method on the ThreadPool class). You can pass your query
as your parameter, and then execute.

Also, if you are using .NET 2.0, then you can use the
BeginExecuteReader/BeginExecuteScalar methods on your SqlCommand class.

Hope this helps.

Nov 17 '05 #5
ThreadPool is simple. You can also make use of async delegate, which
uses threadpool internally. Then your code could look like this:

public delegate void QueryExecutor(string query);
public void ExecuteScalar(string query)
{
new QueryExecutor(ExecuteQuery).BeginInvoke(query, null, null);
}

private void ExecuteQuery(string query)
{
[check connection]
[retrieve data]
this.result = [assign result]
}

You never need to explicitly create the worker thread, the CLR takes
care of it for you.
Note that if updating your windows form UI from your worker thread, you
need to use Control.Invoke or Control.BeginInvoke method.

--------
Thi - http://thith.blogspot.com

Nov 17 '05 #6
SharpCoderMP <cs*******@interia.pl.NFSPM> wrote:
PS: I thought that methods were deprecated already?

even if it is not good to use them... how could it be possible to do
advenced threading without them?


By using Monitor.Wait/Pulse or WaitHandles.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7
And in v2.0, both Suspend and Resume are made obsolete.

Willy.

"TT (Tom Tempelaere)" </\/_0_$P@/\/\titi____AThotmailD.Tcom/\/\@P$_0_/\/>
wrote in message news:KH*********************@phobos.telenet-ops.be...
Hi,

"SharpCoderMP" <cs*******@interia.pl.NFSPM> schreef in bericht
news:eM**************@TK2MSFTNGP11.phx.gbl...
hi,

i have a problem: i wrote a class that encapsulates communication with
database. i wont go into details, but i wanted to use threads to avoid
unresponsive ui so i did something more or less like this:

public void ExecuteScalar(string query)
{
this.query = query;
this.workingThread.Resume(); //the worker thread is suspended
}

private void WorkerThreadProc()
{
while(!this.threadStop)
{
if (this.query.Length > 0)
{
[check connection]
[retrieve data]
this.result = [assign result]
this.query = string.Empty;
Thread.Sleep(500);
}
}
}

now when thread finishes its job i call this.workingThread.Suspend();
after that the thread is rady to process next query. but then my app
eats whole processor time! i've checkd the workingThread state and its
96 what means it is in WaitSleepJoin state and is Suspended. in that
case why it is eating whole cpu time? does it mean that sleeping is "do
nothing loop"???

please help. how can i solve this problem?


Don't ever call SuspendThread/ResumeThread yourself. An excerpt from the
documentation on these methods:
<quote>
CAUTION Do not use the Suspend and Resume methods to synchronize the
activities of threads. You have no way of knowing what code a thread is
executing when you suspend it. If you suspend a thread while it holds
locks during a security permission evaluation, other threads in the
AppDomain might be blocked. If you suspend a thread while it is executing
a class constructor, other threads in the AppDomain that attempt to use
that class are blocked. Deadlocks can occur very easily.
</quote>

Find another way to accomplish what you want to do. Check the net for
something on threading in .NET, for instance try the following link
http://www.yoda.arachsys.com/csharp/threads/

Kind regards,
PS: I thought that methods were deprecated already?
--
Tom Tempelaere.

Nov 17 '05 #8

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

Similar topics

65
by: Anthony_Barker | last post by:
I have been reading a book about the evolution of the Basic programming language. The author states that Basic - particularly Microsoft's version is full of compromises which crept in along the...
2
by: Egor Bolonev | last post by:
hi all my program terminates with error i dont know why it tells 'TypeError: run() takes exactly 1 argument (10 given)' =program==================== import os, os.path, threading, sys def...
77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
11
by: Paul Sijben | last post by:
I am stumped by the following problem. I have a large multi-threaded server accepting communications on one UDP port (chosen for its supposed speed). I have been profiling the code and found...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...

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.