Threading using QueueUserWorkItem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bharathi

    Threading using QueueUserWorkItem

    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

  • Bharathi

    #2
    Re: Threading using QueueUserWorkIt em

    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

    Comment

    • William Stacey [MVP]

      #3
      Re: Threading using QueueUserWorkIt em

      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" <bharathidotnet @gmail.comwrote in message
      news:1158216034 .167157.213040@ 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
      |


      Comment

      • Marc Gravell

        #4
        Re: Threading using QueueUserWorkIt em

        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");
        }
        }


        Comment

        • Bharathi

          #5
          Re: Threading using QueueUserWorkIt em

          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");
          }
          }

          Comment

          Working...