threading problem

S

SharpCoderMP

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?
 
T

TT \(Tom Tempelaere\)

Hi,

SharpCoderMP said:
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?
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
S

SharpCoderMP

TT said:
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?
 
S

SharpCoderMP

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 :|
 
T

Truong Hong Thi

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.
 
J

Jon Skeet [C# MVP]

SharpCoderMP said:
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.
 
W

Willy Denoyette [MVP]

And in v2.0, both Suspend and Resume are made obsolete.

Willy.

TT (Tom Tempelaere) said:
Hi,

SharpCoderMP said:
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?
 
Top