locked
Understanding Threading RRS feed

  • Question

  • I am trying to understand threading... its been hunting me ever since c and pythong.. and now c# and I can't hide from it anymore... I was wondering if some one can show me a really simple threaded snippet and explain it to me if ok... as we speak Im creaing a pdf file about threads.. but It's being a pain. I need some baby steps here.. and this is one of the only place to look for help.. besides other forums that I know.
    Saturday, November 18, 2006 12:12 AM

Answers

All replies

  • Threading is basically spinning up another.."Thread" which does some time consuming function. It is ideal for long running processes or processes that may "hang" the main UI thread. Things like TCP socket communication - incoming data is listened to for on the specified port/socket. Now this will hog the main thread (UI) and act as if the application is not responding but it is however its "hogging" up the resources. So its best to execute this method on a seperate thread so it can run its processes there without the main thread being non responsive.

    http://msdn2.microsoft.com/en-us/library/system.threading.threadstart.aspx

    http://msdn2.microsoft.com/en-us/library/system.threading.aspx

    http://www.codeproject.com/csharp/threader.asp

    http://msdn2.microsoft.com/en-us/library/5xt1dysy(VS.80).aspx

    Threading is expensive so be sure to only use it when you have to. I hope this gets you started mate! :-)

    Saturday, November 18, 2006 12:21 AM
  • You can try reviewing a pdf written by Joseph Albahari titled Threading in C#. Its a wonderfully written tutorial on threading as it covers alot of the threading aspects offered in .NET 2.0. Here is the link http://www.albahari.com/. For me this pdf is a must have threading reference.

    Saturday, November 18, 2006 1:32 AM
  • hmm downloaded pdf... but.. Just to clear my head out a bit more.. how would you use threading with this snippet.

    using System;
    using System.Threading;
    using System.Net;

    class ThreadTest {
    static void Main() {
    string[] ipList = { "127.x.x.x", "192.x.x.x" };

    for( int x = 0; x < ipList.Length; x++ )
    {
    IPHostEntry iphostentry = Dns.GetHostEntry( ipList[x] );
    Console.WriteLine( iphostentry.HostName );
    }
    }
    }

    also do you mind teling me what you meant by "expensive"?
    Saturday, November 18, 2006 4:07 AM
  • Hi,

           First to make u clear, threading is a mechanism by which many processes run simultaneously. For example in windows we can run word,excel and many more programs at the same time. This is possible because of threading. But in your case i dont see any necessity of threading.

    Saturday, November 18, 2006 6:53 AM
  • I think the OP simply asked to illustrated how threading can be incorporated into their example not if threading is feasible or not in the given context.

    The term expensive is used because starting up a thread does use resources hence expensive operation. If you read that pdf it explains everything in it.

    One of the ways you could do it will be to use ParameterizedThreadStart.

    Hope this makes sense.

    using System;
    using System.Threading;
    using System.Net;

    class ThreadTest
    {
    static void Main()
    {
    string[] ipList = { "127.0.0.1", "192.1.1.1",
    "192.1.1.2"};
    for( int x = 0; x < ipList.Length; x++ )
    {
    Thread t = new Thread(new ParameterizedThreadStart(
    FetchHostName);

    t.Start(ipList[x]);
    }
    }
    static
    void FetchHostName(object ipAddress)

    {
    IPHostEntry iphostentry = Dns.GetHostEntry(
    ipAddress);
    Console.WriteLine( iphostentry.HostName );
    }
    }



    edit: no idea why the fonts are so messed up.
    Sunday, November 19, 2006 2:31 AM
  •  ekynox wrote:
    You can try reviewing a pdf written by Joseph Albahari titled Threading in C#. Its a wonderfully written tutorial on threading as it covers alot of the threading aspects offered in .NET 2.0. Here is the link http://www.albahari.com/. For me this pdf is a must have threading reference.

    Thanks for the excellent tip! No doubt, that was the best article I've seen on the threading topic!

    Sunday, November 19, 2006 1:14 PM
  •  write2mtg wrote:

    Hi,

    First to make u clear, threading is a mechanism by which many processes run simultaneously. For example in windows we can run word,excel and many more programs at the same time. This is possible because of threading. But in your case i dont see any necessity of threading.

    I know what threading is.. I read about threading.. I know what it does.. I just cant implement it in to my programming yet... also.. I will need it in the program that Im creating.. becaue when I scan ip's or etc.. the program halts.. yet when I try it out with threading it works faster.
    Tuesday, November 21, 2006 3:20 AM
  • Ok.. I know that I would probably not need threading here.. but Just for knowledge purps how would I implement threading in something like....

    using System;
    using System.Windows.Forms;
    using System.Threading;

    class Threading {
      static void Main() {
       Application.Run( new MainAppl( ));
      }
    }

    class MainAppl : Form {
      Label lbl;

      public MainAppl() {
        Thread t = new Thread( new ThreadStart( ShowLabels ));
        t.Start();
      }

      void ShowLabels()
      {
         for( int x = 0; x < 5; x++ )
         {
           lbl = new Label();
           lbl.Text = x.ToString();
           this.Controls.Add(lbl);
         }
      }
    }

    Im I have been reading about it.. Im alot more clear on it when it comes down to console apps.. but.. gui apps im still abit off.
    Tuesday, November 21, 2006 4:28 AM
  • firstly, i recommend that you go buy this book:
    Windows Forms Programming by Chris Sells and Michael Wienhardt it gives you a good introductory text on threaded win forms.

    Going to your sample code. The way you are updating a label using thread is a big no no as it can result in a cross threaded exception. Since the UI thread is responsible for updating the control. You need to have a mechanisms by which the updating of the label control is passed from the UI thread to the worker thread you have created. You can easily achieve this using Asynchronous Delegates.  At the moment I dont have an example on me but will put one up later on tonight.

    Alternatively, you use the background worker class to do the same thing, follow this example:
    http://msdn2.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

    From my experience its a lot easier to understand using background worker class in understanding how you can do interactive updating of windows forms. However, others may disagree with this.

     hope this helps

    Wednesday, November 22, 2006 2:09 AM
  • Nice! :) thanx.. I will wait for your example as well.. thanx and remember Im just learning... going to see if my boss lets me purchase that book.     
    Wednesday, November 22, 2006 1:23 PM
  • One good article that I would refer you to is this one:

    http://msdn.microsoft.com/msdnmag/issues/03/02/Multithreading/

    It starts off with the basics (why multi threading) and then addresses some of the main issues when dealing with multiple threads e.g. accessing windows controls from the UI thread and locking.

    Keep in mind that .NET provides you with multiple ways of working with threads. Asynchronous delegate invocation (discussed in the article above) is one way for simpler scenarios. You can also use the Thread class for more advanced scenarios.
    Wednesday, November 22, 2006 6:21 PM