Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Extending System.Threading.Thread ?

Extending System.Threading.Thread ?

Scheduled Pinned Locked Moved C#
csharpjavaquestion
10 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Skoder
    wrote on last edited by
    #1

    Hey, i just want to know how i make a class extend the Thread object ? soo the class itself acts like a Thread ? I am use to do the following in Java extend the java.lang.Thread class, and then implement java.lang.Runnable Then i could just override the Run() method, and i could create a new thread by instanciating my class like MyThreadClass tc = new MyThreadClass(); and call tc.Run() to make the thread start. But how is this done in C# .NET ? I seem to only be able to find examples which passes delegates to the Thread. Like Thread myThread = new Thread(new ThreadStart(someMethod)); Can i somehow just extend the System.Threading.Thread class ? and which interface should i Implement ? Martin :confused:

    J 1 Reply Last reply
    0
    • S Skoder

      Hey, i just want to know how i make a class extend the Thread object ? soo the class itself acts like a Thread ? I am use to do the following in Java extend the java.lang.Thread class, and then implement java.lang.Runnable Then i could just override the Run() method, and i could create a new thread by instanciating my class like MyThreadClass tc = new MyThreadClass(); and call tc.Run() to make the thread start. But how is this done in C# .NET ? I seem to only be able to find examples which passes delegates to the Thread. Like Thread myThread = new Thread(new ThreadStart(someMethod)); Can i somehow just extend the System.Threading.Thread class ? and which interface should i Implement ? Martin :confused:

      J Offline
      J Offline
      Jared Parsons
      wrote on last edited by
      #2

      You can't extend Thread because it is a sealed class.

      Skoder wrote:

      Then i could just override the Run() method, and i could create a new thread by instanciating my class like MyThreadClass tc = new MyThreadClass(); and call tc.Run() to make the thread start.

      I'm not sure 100% what kind of effect you are going for here. You could have a class that implements the equivalent to Runnable. Then override the Run method to spawn a thread that calls a method in that class. Jared Parsons jaredp@beanseed.org http://spaces.msn.com/members/jaredp/

      S 1 Reply Last reply
      0
      • J Jared Parsons

        You can't extend Thread because it is a sealed class.

        Skoder wrote:

        Then i could just override the Run() method, and i could create a new thread by instanciating my class like MyThreadClass tc = new MyThreadClass(); and call tc.Run() to make the thread start.

        I'm not sure 100% what kind of effect you are going for here. You could have a class that implements the equivalent to Runnable. Then override the Run method to spawn a thread that calls a method in that class. Jared Parsons jaredp@beanseed.org http://spaces.msn.com/members/jaredp/

        S Offline
        S Offline
        Skoder
        wrote on last edited by
        #3

        Hey, thanks for the answer. Yes i found out that it was a sealed class according to msdn. But isnt there some way to make a class behave like a Thread ? Or is the only way to create instances of a Thread and pass a delegate ?

        J 1 Reply Last reply
        0
        • S Skoder

          Hey, thanks for the answer. Yes i found out that it was a sealed class according to msdn. But isnt there some way to make a class behave like a Thread ? Or is the only way to create instances of a Thread and pass a delegate ?

          J Offline
          J Offline
          Jared Parsons
          wrote on last edited by
          #4

          Skoder wrote:

          But isnt there some way to make a class behave like a Thread ?

          What type of behavior are you wanting? Can you give us a code sample and describe what you want to happen?

          Skoder wrote:

          Or is the only way to create instances of a Thread and pass a delegate ?

          That's the best way (IMHO). Jared Parsons jaredp@beanseed.org http://spaces.msn.com/members/jaredp/

          J S 2 Replies Last reply
          0
          • J Jared Parsons

            Skoder wrote:

            But isnt there some way to make a class behave like a Thread ?

            What type of behavior are you wanting? Can you give us a code sample and describe what you want to happen?

            Skoder wrote:

            Or is the only way to create instances of a Thread and pass a delegate ?

            That's the best way (IMHO). Jared Parsons jaredp@beanseed.org http://spaces.msn.com/members/jaredp/

            J Offline
            J Offline
            Judah Gabriel Himango
            wrote on last edited by
            #5

            Actually, it's recommended you use the thread pool instead of creating threads yourself. You can use the ThreadPool to run background tasks via ThreadPool.QueueUserWorkItem(...). Optionally, if you're trying to do work on a background thread which needs to return data to the UI thread, use System.ComponentModel.BackgroundWorker (.NET 2.0), which takes care of getting the calls on the correct thread for you.

            Tech, life, family, faith: Give me a visit. I'm currently blogging about: Bought a House! Judah Himango

            J 1 Reply Last reply
            0
            • J Jared Parsons

              Skoder wrote:

              But isnt there some way to make a class behave like a Thread ?

              What type of behavior are you wanting? Can you give us a code sample and describe what you want to happen?

              Skoder wrote:

              Or is the only way to create instances of a Thread and pass a delegate ?

              That's the best way (IMHO). Jared Parsons jaredp@beanseed.org http://spaces.msn.com/members/jaredp/

              S Offline
              S Offline
              Skoder
              wrote on last edited by
              #6

              Hey again, i just want to make a class which acts like a thread. Soo i can have private varibles, lists, methods and other stuff which is only used in that single Thread. (thats how it is done in Java). Soo i can just do something like myThread t = new myThread(var1, var2); t.Run(); it is just for because i want to seperate it. But i guess i can just do the following on the class i want to be a thread: class myThread { public myThread(var1, var2) { Thread t = new Thread(new ThreadStart(Run)); } public void Run() { bla bla bla } } It will basically work the same way.

              J 1 Reply Last reply
              0
              • S Skoder

                Hey again, i just want to make a class which acts like a thread. Soo i can have private varibles, lists, methods and other stuff which is only used in that single Thread. (thats how it is done in Java). Soo i can just do something like myThread t = new myThread(var1, var2); t.Run(); it is just for because i want to seperate it. But i guess i can just do the following on the class i want to be a thread: class myThread { public myThread(var1, var2) { Thread t = new Thread(new ThreadStart(Run)); } public void Run() { bla bla bla } } It will basically work the same way.

                J Offline
                J Offline
                Judah Gabriel Himango
                wrote on last edited by
                #7

                You can use

                // I want to pass some variables to the method executing on the other thread...
                int var1, string var2;
                ThreadPool.QueueUserWorkItem(methodToCall, new object[] {var1, var2});

                void MethodToCall(object arguments)
                {
                object[] args = (object[])arguments;
                int var1 = (int)args[0];
                int var2 = (string)args[1];
                }

                Note that if you're using .NET 2.0, you can use anonymous methods (basically blocks of code inside a function) to make this a lot easier and more typesafe. For example,

                int var1, string var2;
                ThreadStart methodToExecuteWithVariables = delegate
                {
                // use var1 or var2 here!
                };
                new Thread(methodToExecuteWithVariables).Start();

                Tech, life, family, faith: Give me a visit. I'm currently blogging about: Bought a House! Judah Himango

                1 Reply Last reply
                0
                • J Judah Gabriel Himango

                  Actually, it's recommended you use the thread pool instead of creating threads yourself. You can use the ThreadPool to run background tasks via ThreadPool.QueueUserWorkItem(...). Optionally, if you're trying to do work on a background thread which needs to return data to the UI thread, use System.ComponentModel.BackgroundWorker (.NET 2.0), which takes care of getting the calls on the correct thread for you.

                  Tech, life, family, faith: Give me a visit. I'm currently blogging about: Bought a House! Judah Himango

                  J Offline
                  J Offline
                  Jared Parsons
                  wrote on last edited by
                  #8

                  That's not true. Creating a thread yourself is just fine and provides a different set of features that you can't get through the ThreadPool. For example you can't Join, Abort or check the state of items in the ThreadPool. Jared Parsons jaredp@beanseed.org http://spaces.msn.com/members/jaredp/

                  J 1 Reply Last reply
                  0
                  • J Jared Parsons

                    That's not true. Creating a thread yourself is just fine and provides a different set of features that you can't get through the ThreadPool. For example you can't Join, Abort or check the state of items in the ThreadPool. Jared Parsons jaredp@beanseed.org http://spaces.msn.com/members/jaredp/

                    J Offline
                    J Offline
                    Judah Gabriel Himango
                    wrote on last edited by
                    #9

                    I didn't say it was disallowed or discouraged. Microsoft simply recommends using the thread pool over creating your own thread for performance and efficiency reasons. See this article[^] for more information.

                    J 1 Reply Last reply
                    0
                    • J Judah Gabriel Himango

                      I didn't say it was disallowed or discouraged. Microsoft simply recommends using the thread pool over creating your own thread for performance and efficiency reasons. See this article[^] for more information.

                      J Offline
                      J Offline
                      Jared Parsons
                      wrote on last edited by
                      #10

                      Your original statement was more a blanket statement. I figured you didn't mean it to be all encompasing but I wanted to be explicit for the people who are just starting out with threads. Jared Parsons jaredp@beanseed.org http://spaces.msn.com/members/jaredp/

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups