threads (background worker) question

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

    threads (background worker) question


    Hello,
    I got a question regarding the usage of background worker.
    How can I run few threads via background worker with different objects
    as parameter each time.

    I understood that I can't do this:

    for (int i = 0; i < inputNum; i++)
    {
    string param = i.ToString();
    bw.RunWorkerAsy nc(param);
    }

    So,how can I do it by running few threads with one background worker?

    Thank you!


    *** Sent via Developersdex http://www.developersdex.com ***
  • Michael D. Ober

    #2
    Re: threads (background worker) question

    "csharpula csharp" <csharpula@yaho o.comwrote in message
    news:ee9BFLXnIH A.1208@TK2MSFTN GP03.phx.gbl...
    >
    Hello,
    I got a question regarding the usage of background worker.
    How can I run few threads via background worker with different objects
    as parameter each time.
    >
    I understood that I can't do this:
    >
    for (int i = 0; i < inputNum; i++)
    {
    string param = i.ToString();
    bw.RunWorkerAsy nc(param);
    }
    >
    So,how can I do it by running few threads with one background worker?
    >
    Thank you!
    >
    >
    *** Sent via Developersdex http://www.developersdex.com ***
    >
    This appears to be a perfect case for the ThreadPool, which is a pool of
    background threads that you can use without declaring them. In addition, if
    there are more tasks than there are threads, it will queue them until a
    thread becomes available.

    Mike.


    Comment

    • csharpula csharp

      #3
      Re: threads (background worker) question



      But can't I use background worker for this purpose?

      *** Sent via Developersdex http://www.developersdex.com ***

      Comment

      • csharpula csharp

        #4
        Re: threads (background worker) question


        I mean how can I run few paralell threads using the background worker?
        Thank u!


        *** Sent via Developersdex http://www.developersdex.com ***

        Comment

        • Alain Boss

          #5
          Re: threads (background worker) question

          csharpula csharp wrote:
          I mean how can I run few paralell threads using the background worker?
          Thank u!
          >
          >
          *** Sent via Developersdex http://www.developersdex.com ***
          You will need a background worker for all threads. That is, if you want
          to run your task in 5 threads you need 5 backgroundworke rs. In your
          example you only have one but you'd need 5.

          Alain

          Comment

          • William Stacey [C# MVP]

            #6
            Re: threads (background worker) question

            1 worker - 1 thread. You could spawn other threads inside the worker.
            You may also want to look at the CCR in the robotics studio. It is not the
            api that is so interesting, it is the non-blocking pattern using ports and
            handlers.

            "csharpula csharp" <csharpula@yaho o.comwrote in message
            news:ee9BFLXnIH A.1208@TK2MSFTN GP03.phx.gbl...
            >
            Hello,
            I got a question regarding the usage of background worker.
            How can I run few threads via background worker with different objects
            as parameter each time.
            >
            I understood that I can't do this:
            >
            for (int i = 0; i < inputNum; i++)
            {
            string param = i.ToString();
            bw.RunWorkerAsy nc(param);
            }
            >
            So,how can I do it by running few threads with one background worker?
            >
            Thank you!
            >
            >
            *** Sent via Developersdex http://www.developersdex.com ***

            Comment

            • csharpula csharp

              #7
              Re: threads (background worker) question

              Buth the question is if can I do something like that using the
              background worker:

              BackgroundWorke r bw = new BackgroundWorke r();
              BackgroundWorke r bw2 = new BackgroundWorke r();

              bw.RunWorkerAsy nc(2);
              bw2.RunWorkerAs ync(3);


              and in this way create as much backgroundworke rs as threads i need or
              there is a better way doing it?
              Thank u!


              *** Sent via Developersdex http://www.developersdex.com ***

              Comment

              • William Stacey [C# MVP]

                #8
                Re: threads (background worker) question

                Yes you can do that. Create as many as you need (and resource permits). I
                thought you asked if you could do multiple threads with 1 BW.

                "csharpula csharp" <csharpula@yaho o.comwrote in message
                news:ewwLPBYnIH A.4712@TK2MSFTN GP04.phx.gbl...
                Buth the question is if can I do something like that using the
                background worker:
                >
                BackgroundWorke r bw = new BackgroundWorke r();
                BackgroundWorke r bw2 = new BackgroundWorke r();
                >
                bw.RunWorkerAsy nc(2);
                bw2.RunWorkerAs ync(3);
                >
                >
                and in this way create as much backgroundworke rs as threads i need or
                there is a better way doing it?
                Thank u!
                >
                >
                *** Sent via Developersdex http://www.developersdex.com ***

                Comment

                Working...