• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Understanding Thread

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I am trying to learn advanced C# and stuck at thread async and await. Please explain my questions:



a) When I define a thread what does this mean?

             - Does the method in which it is defined becomes the thread?

             - Or Does the parameter in ThreadStart becomes the Thread?



Thanks
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch Micro Dev.

Micro Dev wrote:When I define a thread what does this mean?

  • Does the method in which it is defined becomes the thread?
  • Or Does the parameter in ThreadStart becomes the Thread?

  • I think that the parameter becomes the method/object that you want the thread to run, but this does not necessarily become the thread.

    However I could be wrong and here is some more information that I was able to find:

    https://www.dotnetperls.com/threadstart wrote:We use the ThreadStart type. We create an instance of it and pass that to the Thread constructor. We also use ParameterizedThreadStart.


    From the dotnetperls.com site it would appear as though ThreadStart is a type or an object which you pass to a thread.

    This StackOverFlow posting seems to have a lot to say on the subject matter of ThreadStart with some samples and other links:
    https://stackoverflow.com/questions/1195896/threadstart-with-parameters

    Hope this was helpful.
     
    Ranch Hand
    Posts: 82
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hello,
    Understanding Threading
    Basically Threading is used for multitasking within a single application
    The base class used for threading is System.Threading.

    There is often a need of passing data from the main thread of a program to a worker threads.
    The Thread.Start method has an overloaded form that allows code to pass an object from main thread to a new thread.
    The object can be a simple data type or it can be a complex data type.

    Thread Life Cycle
    The life cycle of a thread starts when an object of the System.Threading.Thread class is created and ends when the thread is terminated or completes execution.

    Following are the various states in the life cycle of a thread:

    The Unstarted State : It is the situation when the instance of the thread is created but the Start method is not called.

    The Ready State : It is the situation when the thread is ready to execute and waiting CPU cycle.

    The Not Runnable State : a thread is not runnable, when:

    Sleep method has been called
    Wait method has been called
    Blocked by I/O operations

    The Dead State : It is the situation when the thread has completed execution or has been aborted.

    ex :
    using System.Threading;

    namespace threaddemo
    {
      public partial class _Default : System.Web.UI.Page
      {
         protected void Page_Load(object sender, EventArgs e)
         {
            ThreadStart childthread = new ThreadStart(childthreadcall);
            Response.Write("Child Thread Started <br/>");
            Thread child = new Thread(childthreat);
           
            child.Start();
           
            Response.Write("Main sleeping  for 2 seconds.......<br/>");
            Thread.Sleep(2000);
            Response.Write("<br/>Main aborting child thread<br/>");
           
            child.Abort();
         }
         
         public void childthreadcall()
         {
            try{
               lblmessage.Text = "<br />Child thread started <br/>";
               lblmessage.Text += "Child Thread: Counting to 10";
               
               for( int i =0; i<10; i++)
               {
                  Thread.Sleep(500);
                  lblmessage.Text += "<br/> in Child thread </br>";
               }
               
               lblmessage.Text += "<br/> child thread finished";
               
            }catch(ThreadAbortException e){
           
               lblmessage.Text += "<br /> child thread - exception";
               
            }finally{
               lblmessage.Text += "<br /> child thread - unable to catch the  exception";
            }
         }
      }
     
    Just let me do the talking. Ahem ... so ... you see ... we have this tiny ad...
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic