Threading

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

    Threading

    Hi,

    I want a certain task to be run by multiple threads. The issue is do we have
    to create a new thread object foreach new thread and specify the same
    threadStart deligate or is there a different approach to this.

    Thanks
    Amendra.


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Threading

    Amendra,

    That is exactly what you want to do. You want to create the separate
    threads and then point them all to the same ThreadStart delegate. However,
    make sure that if you are accessing class-level fields (or static variables,
    if it is a static method), that you synchronize access to those fields.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Amendra" <amendra@virtus a.com> wrote in message
    news:OfObxI6pDH A.1728@TK2MSFTN GP09.phx.gbl...[color=blue]
    > Hi,
    >
    > I want a certain task to be run by multiple threads. The issue is do we[/color]
    have[color=blue]
    > to create a new thread object foreach new thread and specify the same
    > threadStart deligate or is there a different approach to this.
    >
    > Thanks
    > Amendra.
    >
    >[/color]


    Comment

    • Amendra

      #3
      Re: Threading

      Yes, I am aware of the sync issues, what if we want to make the number of
      threads dynamic, then I would not know how many threads to create in
      advance.... any solution..

      Thanks
      Threading


      "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
      message news:%23D3NuL6p DHA.2188@TK2MSF TNGP11.phx.gbl. ..[color=blue]
      > Amendra,
      >
      > That is exactly what you want to do. You want to create the separate
      > threads and then point them all to the same ThreadStart delegate.[/color]
      However,[color=blue]
      > make sure that if you are accessing class-level fields (or static[/color]
      variables,[color=blue]
      > if it is a static method), that you synchronize access to those fields.
      >
      > Hope this helps.
      >
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - mvp@spam.guard. caspershouse.co m
      >
      > "Amendra" <amendra@virtus a.com> wrote in message
      > news:OfObxI6pDH A.1728@TK2MSFTN GP09.phx.gbl...[color=green]
      > > Hi,
      > >
      > > I want a certain task to be run by multiple threads. The issue is do we[/color]
      > have[color=green]
      > > to create a new thread object foreach new thread and specify the same
      > > threadStart deligate or is there a different approach to this.
      > >
      > > Thanks
      > > Amendra.
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Threading

        Amendra,

        If the operation is short running, then you should use the ThreadPool
        class, and have the system delegate when to do the work. If not, then you
        can have a collection of threads that is added to when you create your new
        thread, and removed from when the thread has completed the work.


        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m

        "Amendra" <amendra@virtus a.com> wrote in message
        news:%23vb87P6p DHA.2244@TK2MSF TNGP12.phx.gbl. ..[color=blue]
        > Yes, I am aware of the sync issues, what if we want to make the number of
        > threads dynamic, then I would not know how many threads to create in
        > advance.... any solution..
        >
        > Thanks
        > Threading
        >
        >
        > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote[/color]
        in[color=blue]
        > message news:%23D3NuL6p DHA.2188@TK2MSF TNGP11.phx.gbl. ..[color=green]
        > > Amendra,
        > >
        > > That is exactly what you want to do. You want to create the[/color][/color]
        separate[color=blue][color=green]
        > > threads and then point them all to the same ThreadStart delegate.[/color]
        > However,[color=green]
        > > make sure that if you are accessing class-level fields (or static[/color]
        > variables,[color=green]
        > > if it is a static method), that you synchronize access to those fields.
        > >
        > > Hope this helps.
        > >
        > >
        > > --
        > > - Nicholas Paldino [.NET/C# MVP]
        > > - mvp@spam.guard. caspershouse.co m
        > >
        > > "Amendra" <amendra@virtus a.com> wrote in message
        > > news:OfObxI6pDH A.1728@TK2MSFTN GP09.phx.gbl...[color=darkred]
        > > > Hi,
        > > >
        > > > I want a certain task to be run by multiple threads. The issue is do[/color][/color][/color]
        we[color=blue][color=green]
        > > have[color=darkred]
        > > > to create a new thread object foreach new thread and specify the same
        > > > threadStart deligate or is there a different approach to this.
        > > >
        > > > Thanks
        > > > Amendra.
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        Working...