473,785 Members | 2,882 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Threading using QueueUserWorkIt em

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.Threa dPool.QueueUser WorkItem(New Threading.WaitC allback(

_
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

Sep 14 '06 #1
4 12145
My problem is same whenther the code behing lang is VB or C#. Thats why
posted here.
Threading.Threa dPool.QueueUser WorkItem(New Threading.WaitC allback(
>
_
AddressOf MyFunction))

Bharathi wrote:
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.Threa dPool.QueueUser WorkItem(New Threading.WaitC allback(

_
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
Sep 14 '06 #2
Call QueueUserWorkIt em 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.Queu eUserWorkItem(. ..);
}
}

--
William Stacey [MVP]

"Bharathi" <bh************ @gmail.comwrote in message
news:11******** **************@ e63g2000cwd.goo glegroups.com.. .
| 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.Threa dPool.QueueUser WorkItem(New Threading.WaitC allback(
|
| _
| 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
|
Sep 14 '06 #3
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(!threadStart ed) {
ThreadPool.Queu eUserWorkItem(S omeOtherMethod) ;
threadStarted = true;
}
}
}

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

using System;
using System.Threadin g;
class Program {
static void Main() {
Console.WriteLi ne("Count \'em");
// start 10 user threads
for (int i = 0; i < 10; i++) {
new Thread(UserThre adStart).Start( );
}
Console.ReadLin e();
}
static Random rand = new Random();
static void UserThreadStart () {
Thread.Sleep(ra nd.Next(4000)); // pause
ThreadPool.Queu eUserWorkItem(P oolThreadStart) ;
}
static void PoolThreadStart (object state) {
Console.WriteLi ne("Did something");
}
}
Sep 14 '06 #4
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.

Marc Gravell wrote:
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(!threadStart ed) {
ThreadPool.Queu eUserWorkItem(S omeOtherMethod) ;
threadStarted = true;
}
}
}

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

using System;
using System.Threadin g;
class Program {
static void Main() {
Console.WriteLi ne("Count \'em");
// start 10 user threads
for (int i = 0; i < 10; i++) {
new Thread(UserThre adStart).Start( );
}
Console.ReadLin e();
}
static Random rand = new Random();
static void UserThreadStart () {
Thread.Sleep(ra nd.Next(4000)); // pause
ThreadPool.Queu eUserWorkItem(P oolThreadStart) ;
}
static void PoolThreadStart (object state) {
Console.WriteLi ne("Did something");
}
}
Sep 15 '06 #5

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

Similar topics

5
1534
by: OpticTygre | last post by:
Heheh...I've got lots of questions today. If I have a loop that calls the same subroutine several times (that subroutine may be processor and network intensive): For i = 1 to 100 Call mySub(IPAddress(i)) Next And inside mySub, I'm initializing a new network connection (SFTP if you
0
1987
by: Colmeister | last post by:
I recently read Jason Clark's excellent article on Unhandled Exceptions (http://msdn.microsoft.com/msdnmag/issues/04/06/NET/default.aspx) and have attempted to incorporate the features he talks about in a new application I'm writing. However, when I try to use ThreadStart to do some work in a separate thread from my GUI, the methods Jason described don't seem to catch the exception. Take the following source code: Public Class...
3
7955
by: Jayme Pechan | last post by:
I was wondering if someone could help me understand why it seems that creating threads using the code below causes handles to seemingly leak. System.Threading.Thread threadRunAsync = new System.Threading.Thread(RunFunc); threadRunAsync.Start(listParams); I have to use ThreadPools in order to avoid the handle leaks. System.Threading.ThreadPool.QueueUserWorkItem(new
6
3114
by: venkat | last post by:
Hi, I have a page that calls a sql procedure and it runs for 20 - 40 min. I use usual threading for this page. <code>Thread objthread = new Thread(new ThreadStart(ticketingThread)); objthread.Start();</code> As i dont know when it will end. i never abort the thread. what happens is when user uses the page for 1 week, the page starts hanging. I dont know what happens. When i tried debugging it works fine.
3
1369
by: Bharathi kumar | last post by:
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.
4
321
by: DBC User | last post by:
I have a background process which reads a table to see if there are any pending requests. If there are any, then it will start a worker thread (only 10 allowed at a time) and executes a method. In this method, I iniate a PROCESS and on completion, it reduces the available worker thread and continue. I have couple of questions; 1. Since I am launching multiple threads on a same method, do I have to take care of locking so that each one...
7
2377
by: Mike P | last post by:
I am trying to write my first program using threading..basically I am moving messages from an Outlook inbox and want to show the user where the process is up to without having to wait until it has finished. I am trying to follow this example : http://www.codeproject.com/cs/miscctrl/progressdialog.asp But although the messages still get moved, the progress window never does anything. Here is my code in full, if anybody who knows...
2
1661
by: Mike P | last post by:
I am using the following code, what I need to know is, what do I do if I want to pass parameters to DoSomeWork? I don't understand how the callback variable is passed to DoSomeWork. private void SpawnSomeWork() { IProgressCallback callback; // = ??? System.Threading.ThreadPool.QueueUserWorkItem( new System.Threading.WaitCallback( DoSomeWork ),
19
1807
by: frankiespark | last post by:
Hello all, I was perusing the internet for information on threading when I came across this group. Since there seems to be a lot of good ideas and useful info I thought I'd pose a question. Threading is a new concept for me to implement. Here is my problem. I have a system that receives xml files and records their file locations in a database. I can potentially receive thousands,
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10327
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10151
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10092
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8973
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3647
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.