Threading using QueueUserWorkItem

  • Thread starter Thread starter Bharathi
  • Start date Start date
B

Bharathi

Hi,

Iam working in windows appln using .NET framework 2.0.


My application is having several threads running.


All threads can call a common method.


I have following code in my common method.
Threading.ThreadPool.QueueUserWorkItem(New Threading.WaitCallback(

_
AddressOf MyFunction))


When I call the common method, its executing "MyFunction" twice.


I want the "MyFunction" to execute only once.


Any suggestions please.


Regards,
Bharathi Kumar
 
My problem is same whenther the code behing lang is VB or C#. Thats why
posted here.


Threading.ThreadPool.QueueUserWorkItem(New Threading.WaitCallback(
 
Call QueueUserWorkItem only once. Your solution to that can vary. One
option is to use a lock to gain atomic access to the test.

lock(syncRoot)
{
if ( ! alreadyCalled )
{
alreadyCalled = true;
ThreadPool.QueueUserWorkItem(...);
}
}

--
William Stacey [MVP]

| Hi,
|
| Iam working in windows appln using .NET framework 2.0.
|
|
| My application is having several threads running.
|
|
| All threads can call a common method.
|
|
| I have following code in my common method.
| Threading.ThreadPool.QueueUserWorkItem(New Threading.WaitCallback(
|
| _
| AddressOf MyFunction))
|
|
| When I call the common method, its executing "MyFunction" twice.
|
|
| I want the "MyFunction" to execute only once.
|
|
| Any suggestions please.
|
|
| Regards,
| Bharathi Kumar
|
 
It should only execute once per call to QueueUserItem; I would start by
putting some debug code to see how many times you call that method. If you
only want to run it once (across all threads) then you will need some kind
of (synchronised) variable to keep track this - e.g.

private static bool threadStarted;
private static readonly object syncLock = new object();

void SomeMethod() {
lock(syncLock) {
if(!threadStarted) {
ThreadPool.QueueUserWorkItem(SomeOtherMethod);
threadStarted = true;
}
}
}

An example of QueueUserItem firing the correct number of times follows:

using System;
using System.Threading;
class Program {
static void Main() {
Console.WriteLine("Count \'em");
// start 10 user threads
for (int i = 0; i < 10; i++) {
new Thread(UserThreadStart).Start();
}
Console.ReadLine();
}
static Random rand = new Random();
static void UserThreadStart() {
Thread.Sleep(rand.Next(4000)); // pause
ThreadPool.QueueUserWorkItem(PoolThreadStart);
}
static void PoolThreadStart(object state) {
Console.WriteLine("Did something");
}
}
 
Hi,

Thank you very much for the inputs.

Iam sorry. The appln is working fine.

One of my colleague had modified the code. They have added one more
thread thats calling "MyFunction".

So "MyFunction" had called twice.

I didn't notice that code and thought the behaviour was peculiar.

Thank you very much,
Regards,
Bharathi Kumar.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top