How to use threading to activate a sperate function without freezingthe current window

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

    How to use threading to activate a sperate function without freezingthe current window

    I am learning C sharp. I want to call a function (any simple function.
    E.g. : Never ending while loop or large For loop inside the function).
    I want to call this function from the main application window. I found
    couple of tutorials but for some reason window freezes.

    THis is what I did,

    Thread FirstThread = new Thread(new ThreadStart(Thr eadFunc));
    FirstThread.Sta rt();
    FirstThread.Joi n();

    protected static void ThreadFunc()
    {
    for (Int64 i = 0; i < 10000000; i++)
    {
    // Do my job . . . . .
    }
    }

    Can some one help me to resolve this problem. I am still a bigginner.

    Pubudu
  • Jon Skeet [C# MVP]

    #2
    Re: How to use threading to activate a sperate function without freezing the current window

    Pubs <pubuducg@gmail .comwrote:
    I am learning C sharp. I want to call a function (any simple function.
    E.g. : Never ending while loop or large For loop inside the function).
    I want to call this function from the main application window. I found
    couple of tutorials but for some reason window freezes.
    >
    THis is what I did,
    >
    Thread FirstThread = new Thread(new ThreadStart(Thr eadFunc));
    FirstThread.Sta rt();
    FirstThread.Joi n();
    When you call Join, that basically says "wait until the thread has
    finished" - which completely negates the point of doing threading.

    Just don't call Join and it should be fine.

    --
    Jon Skeet - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon.skeet
    C# in Depth: http://csharpindepth.com

    Comment

    • Marc Gravell

      #3
      Re: How to use threading to activate a sperate function withoutfreezing the current window

      You are calling Join, which by definition waits for the other thread
      to exit. In short, don't call Join here - just start the other thread
      and let it run. By the way, if the worker thread is touching the form
      or any controls (such as setting text etc), you will need to use
      Control.Invoke to switch back to the UI thread, due to thread
      affinity.

      Marc

      Comment

      • Pubs

        #4
        Re: How to use threading to activate a sperate function withoutfreezing the current window

        On May 31, 11:40 am, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
        Pubs <pubud...@gmail .comwrote:
        I am learning C sharp. I want to call a function (any simple function.
        E.g. : Never ending while loop or large For loop inside the function).
        I want to call this function from the main application window. I found
        couple of tutorials but for some reason window freezes.
        >
        THis is what I did,
        >
        Thread FirstThread = new Thread(new ThreadStart(Thr eadFunc));
                   FirstThread.Sta rt();
                   FirstThread.Joi n();
        >
        When you call Join, that basically says "wait until the thread has
        finished" - which completely negates the point of doing threading.
        >
        Just don't call Join and it should be fine.
        >
        --
        Jon Skeet - <sk...@pobox.co m>
        Web site:http://www.pobox.com/~skeet 
        Blog:http://www.msmvps.com/jon.skeet
        C# in Depth:http://csharpindepth.com

        Jon Skeet

        Thank you very much for the help. It works now.
        How do I know once the function is completed, thread is terminated ?

        Pubudu

        Comment

        • Pubs

          #5
          Re: How to use threading to activate a sperate function withoutfreezing the current window

          On May 31, 11:43 am, Marc Gravell <marc.grav...@g mail.comwrote:
          You are calling Join, which by definition waits for the other thread
          to exit. In short, don't call Join here - just start the other thread
          and let it run. By the way, if the worker thread is touching the form
          or any controls (such as setting text etc), you will need to use
          Control.Invoke to switch back to the UI thread, due to thread
          affinity.
          >
          Marc
          Marc,

          Thank you very much Marc. It is working fine.

          Pubudu

          Comment

          • Peter Duniho

            #6
            Re: How to use threading to activate a sperate function without freezing the current window

            On Sat, 31 May 2008 09:51:15 -0700, Pubs <pubuducg@gmail .comwrote:
            Thank you very much for the help. It works now.
            How do I know once the function is completed, thread is terminated ?
            One approach would be to have your thread call a specific method when it's
            done. If you want the method to do something with the UI, you should do
            as Marc suggests with that method, calling it with Control.Invoke( )
            instead of calling it directly.

            Pete

            Comment

            • Marc Gravell

              #7
              Re: How to use threading to activate a sperate function withoutfreezing the current window

              To second Peter's answer: if the code in question is "close" to the
              other code, then just call some method directly, usually via
              Control.Invoke - i.e.

              void WorkerMethod()
              { // runs on background thread
              // ... some long code
              this.Invoke((Me thodInvoker) LongOpFinished) ;
              }
              void LongOpFinished( )
              { // runs on UI thread when WorkerMethod is complete
              }

              If the code is more separated (such as library methods), then you
              might use a "callback" - which at the simplest could be as simple as
              passing a MethodInvoker (or Action, or ThreadStart, or whatever) into
              the method as an argument, and then invoking it at the end. Callbacks
              can also be implemented via events (and about 20 other ways) - or you
              can just have the UI worry about it locally (i.e. the UI calls a local
              method 9worker thread) that calls the library method (worker thread),
              then calls another local method (UI thread).

              Marc

              Comment

              • Pubs

                #8
                Re: How to use threading to activate a sperate function withoutfreezing the current window

                On May 31, 4:26 pm, Marc Gravell <marc.grav...@g mail.comwrote:
                To second Peter's answer: if the code in question is "close" to the
                other code, then just call some method directly, usually via
                Control.Invoke - i.e.
                >
                void WorkerMethod()
                { // runs on background thread
                  // ... some long code
                  this.Invoke((Me thodInvoker) LongOpFinished) ;}
                >
                void LongOpFinished( )
                { // runs on UI thread when WorkerMethod is complete
                >
                }
                >
                If the code is more separated (such as library methods), then you
                might use a "callback" - which at the simplest could be as simple as
                passing a MethodInvoker (or Action, or ThreadStart, or whatever) into
                the method as an argument, and then invoking it at the end. Callbacks
                can also be implemented via events (and about 20 other ways) - or you
                can just have the UI worry about it locally (i.e. the UI calls a local
                method 9worker thread) that calls the library method (worker thread),
                then calls another local method (UI thread).
                >
                Marc
                Marc,

                Thank you very much. This helped me a lot.

                Pubudu

                Comment

                Working...