473,406 Members | 2,707 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,406 software developers and data experts.

Async Delegate Execution

I have implemented an asyncrhonous execution of a method using the
delegate's BeginInvoke()
I am trying to spin multiple worker threads in a thread pool.
Here is the code:

TestProcess process = new TestProcess(TestProcess);
process.BeginInvoke(args,null,null);

private void TestProcess(args)
{
Thread.CurrentThread.Name = "TestProcess; Group: " + args;
}

However after a while it seems that the TestProcess method starts executing
on the same thread that became inactive.
I get exceptions pointing me to the line where I assign
Thread.CurrentThread.Name as it is already assigned by the previous
execution.

Is this the correct behaviour?
While I understand that the TreadPool object could choose existing thread
resources naturally I thought that the data on that new thread would be
reset.
Is Thread.CurrentThread.Name and exclusion to that rule not not considered
to be part of the thread data?
It basically becomes useless since I cannot uniquely identify the threads
anymore...

Could you please let me know if I am doing something wrong or if that is by
design. If it is by design, please refer me to the KB article or other
resources.

Thanks,
Vlad
Nov 15 '05 #1
4 3832
Hello Vlad,

Thanks for posting. I have done some research on your code but I still have
some difficulties understanding it. I would appreciate it very much if you
could clarify some points. What is TestProcess class? Is it derived from
the Delegate class? If yes, how do you define the BeginInvoke() method?

The Name property of a Thread can only be set once. This also applies to
ThreadPool's worker threads. If you would like to uniquely identify the
threads by their Names, you can use new threads, instead of pooling from a
ThreadPool. Currently we do not have any public KB article that addresses
the issue, however, you may refer to the MSDN library for more information
on ThreadPool and Thread.Name:

ThreadPool Class
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemthreadingthreadpoolclasstopic.asp?frame =true

Thread.Name Property
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfSystemThreadingThreadClassNameTopic.asp?frame =true

I hope this helps.

Regards,

Felix Wang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #2
Sorry for not making it clear...
the TestProcess is the delegate declared like this:
delegate void TestProcess(args);

Being a delegate it has BeginInvoke() method provided by the C# compiler
that allows asynchronous execution as part of the .net Asynchronous
Execution framework.

Say the first time BeginInvoke() is called and spawn a thread. That thread
is since exited and is now inactive in a thread pool. During the first
execution the thread's Name property has been set.
The next time the BeginInvoke() is called, instead of creating the new
thread it re-used the resources of the previous thread with all thread data
(any local variables) being reset.
However the thread's name has not been reset. That kind of defeats the
purpose of using the thread name to identify threads. The name originally
given to that thread is useless in a context of new thread execution.

So here is the question...why isn't the thread's name property being reset
just like any other data within that thread so that it would look to the
code as if the thread is brand new.

Thanks!

"Felix Wang" <v-*****@online.microsoft.com> wrote in message
news:Xp**************@cpmsftngxa07.phx.gbl...
Hello Vlad,

Thanks for posting. I have done some research on your code but I still have some difficulties understanding it. I would appreciate it very much if you
could clarify some points. What is TestProcess class? Is it derived from
the Delegate class? If yes, how do you define the BeginInvoke() method?

The Name property of a Thread can only be set once. This also applies to
ThreadPool's worker threads. If you would like to uniquely identify the
threads by their Names, you can use new threads, instead of pooling from a
ThreadPool. Currently we do not have any public KB article that addresses
the issue, however, you may refer to the MSDN library for more information
on ThreadPool and Thread.Name:

ThreadPool Class
http://msdn.microsoft.com/library/de...us/cpref/html/ frlrfsystemthreadingthreadpoolclasstopic.asp?frame =true

Thread.Name Property
http://msdn.microsoft.com/library/de...us/cpref/html/ frlrfSystemThreadingThreadClassNameTopic.asp?frame =true

I hope this helps.

Regards,

Felix Wang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #3
Hi Vlad,

I have done some tests and it seems to me that the "BeginInvoke()" method
will not use a thread from a thread pool. I am running on Windows 2003 with
Framework 1.1. Could you paste your code that can exactly reproduce the
problem?

On the other hand, you are correct that when a used thread is returned to
the ThreadPool, its Name will not be reset. The Name property of the thread
can only be set once and even the class library cannot violate this rule. I
also believe that apart from the "Name", other properties of the thread
(e.g. Priority) will not be reset. The following is my code for testing:

using System;
using System.Threading;
public class Example
{
public static void Main()
{
for (int i = 0; i < 3; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
}
Console.WriteLine("Main thread does some work, then sleeps.");
Thread.Sleep(100000);
}
static void ThreadProc(Object stateInfo)
{
Console.WriteLine("Hello from counter "+ counter.ToString());
Console.WriteLine(Thread.CurrentThread.Priority);
counter++;
Thread.CurrentThread.Priority = ThreadPriority.Lowest;
}
private static int counter = 0;
}

The result is the following:

Main thread does some work, then sleeps.
Hello from counter 0
Normal
Hello from counter 1
Lowest
Hello from counter 2
Lowest

It is obvious that the priority of the thread is not reset to Normal. I
look forward to your reply.

Regards,

Felix Wang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #4
That's fine...I just couldnt find any documentation on that if the thread
got deactivated it still retained all properties even though it is
reactivated "as new thread"
In your example the code is so fast in the for loop that the threads that
you spawn do not have a chance to complete execution before the next thread
is spawn
The better example would be:
for (int i = 0; i < 3; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
Thread.Sleep(100000)
}
Console.WriteLine("Main thread does some work, then sleeps.");
Thread.Sleep(1000000);

That way the second time a new thread is spawn the first thread has already
finished so the ThreadPool could re-use it.
Thanks for you help

"Felix Wang" <v-*****@online.microsoft.com> wrote in message
news:Ph**************@cpmsftngxa08.phx.gbl...
Hi Vlad,

I have done some tests and it seems to me that the "BeginInvoke()" method
will not use a thread from a thread pool. I am running on Windows 2003 with Framework 1.1. Could you paste your code that can exactly reproduce the
problem?

On the other hand, you are correct that when a used thread is returned to
the ThreadPool, its Name will not be reset. The Name property of the thread can only be set once and even the class library cannot violate this rule. I also believe that apart from the "Name", other properties of the thread
(e.g. Priority) will not be reset. The following is my code for testing:

using System;
using System.Threading;
public class Example
{
public static void Main()
{
for (int i = 0; i < 3; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
}
Console.WriteLine("Main thread does some work, then sleeps.");
Thread.Sleep(100000);
}
static void ThreadProc(Object stateInfo)
{
Console.WriteLine("Hello from counter "+ counter.ToString());
Console.WriteLine(Thread.CurrentThread.Priority);
counter++;
Thread.CurrentThread.Priority = ThreadPriority.Lowest;
}
private static int counter = 0;
}

The result is the following:

Main thread does some work, then sleeps.
Hello from counter 0
Normal
Hello from counter 1
Lowest
Hello from counter 2
Lowest

It is obvious that the priority of the thread is not reset to Normal. I
look forward to your reply.

Regards,

Felix Wang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #5

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

Similar topics

1
by: Gomaw Beoyr | last post by:
When the BeginInvoke method of a delegate is called, the execution of the delegate is in another thread. (I'm not sure if an entirely new thread is created, or if a "sleeping" thread is used.) ...
10
by: Shawn Meyer | last post by:
Hello - I am trying to write a class that has an async BeginX and EndX, plus the regular X syncronous method. Delegates seemed like the way to go, however, I still am having problems getting...
1
by: eminemabcd | last post by:
Hi, I encountered a problem trying to write a code that does the following: 1. Main GUI thread invokes a delegate using BeginInvoke. 2. A secondery thread (from the application's managed thread...
4
by: Bob Badger | last post by:
Hi, Simple question (although I guess with a complicated answer). Is HTTP an async protocol? For instance, if I send a message to a c# webservice via http what is the protocol actually doing? ...
7
by: Shak | last post by:
Hi all, I'm trying to write a thread-safe async method to send a message of the form (type)(contents). My model is as follows: private void SendMessage(int type, string message) { //lets...
1
by: jonathan | last post by:
I need to create a webpage that asynchronously loads a series of user controls onto a page. If the user control take longer than X seconds to load it should display an error message in it place....
9
by: BartMan | last post by:
Greetings, I am trying to fire aysnc events in c++/clr, and I can't seem to get it to work. It seems to work fine in c#, but when I try c++/clr, I can't seem to get it to compile in the c++/clr...
12
by: Tomaz Koritnik | last post by:
Is it possible to asynchronously call a method from worker thread so that this method would execute in main-thread? I'm doing some work in worker thread and would like to report progress to main...
3
by: Stefan Hoffmann | last post by:
hi @all, I've have this working piece of code in an async callback function used: private void SetCount() { if (listView.InvokeRequired) { listView.Invoke((MethodInvoker)(() =SetCount()));...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.