Results 1 to 12 of 12

Thread: Multi-Threading in C#

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    Multi-Threading in C#

    Can someone show me how to create two threads where each thread calls a different function. Thanks in advance.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Multi-Threading in C#

    MSDN can.

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

    The example code shows you how to start a thread. You can start as many threads as you like in exactly the same way, using different methods as entry points for each if you like.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    Re: Multi-Threading in C#

    I looked at examples but could not get them to work. This is the code I have....



    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    using System.Threading;
    
    namespace ThreadingTest
    {
    
        public partial class Form1 : Form
        {
            private static int iCount = 0;
    
            ThreadStart myThreadDelegate = new ThreadStart(incrementCounter);
            Thread myThread;
    
            public Form1()
            {
                InitializeComponent();
    
            }
    
            public static void incrementCounter()
            {
                iCount++;
                Thread.Sleep(1000);
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                myThread = new Thread(myThreadDelegate);
                myThread.Start();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                label1.Text = iCount.ToString();
            }
        }
    
    }




    no matter what iCount is always == 1

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Multi-Threading in C#

    That exact code works perfectly for me. You have assigned the appropriate methods to the Click events of the Buttons, right? And you are clicking button2 to update the Label with the thread count, right?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    Re: Multi-Threading in C#

    i figured it out. I have to keep hitting button1 to have iCount increment. I was thinking it was like a timer
    Last edited by 2MuchRiceMakesMeSick; Feb 6th, 2006 at 03:45 AM.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    Re: Multi-Threading in C#

    Am I suppost to call myThread.Start(); each time I want the counter incremented right?
    or am I suppost to call it one time?

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Multi-Threading in C#

    Calling myThread.Start is what starts a thread. If you want to start 10 threads then you have to call myThread.Start 10 times, which in this case means pressing button1 10 times.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    Re: Multi-Threading in C#

    in the example above, the only time that iCount is incremented is when I click button1. If i only click button1 once iCount stays at 1. (i kept on refreshing with button2)

    In theory iCount should ++ every 1000 milisecond right?
    but why isnt it?

    Thanks for the help
    Last edited by 2MuchRiceMakesMeSick; Feb 6th, 2006 at 04:23 AM.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Multi-Threading in C#

    Quote Originally Posted by 2MuchRiceMakesMeSick
    in the example above, the only time that iCount is incremented is when I click button1. If i only click button1 once iCount stays at 1. (i kept on refreshing with button2)

    In theory iCount should ++ every 1000 milisecond right?
    but why isnt it?

    Thanks for the help
    That's not what happens. There's no loop there. The code you provided is using the 'incrementCounter method as the entry point for a new thread. That means that the lifetime of the thread that you start is as long as it takes to execute that method. When you start a new thread that method is called, it increments the counter, sleeps for 1 second and then the method completes, thus that thread is done. The whole idea of that code is the variable is supposed to be indicating how many threads you have started. All each thread does is increment the counter, pause then exit.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    Re: Multi-Threading in C#

    So if i wanted that one thread to keep incrementing iCount I would have to setup a loop in incrementCounter() like this.

    Code:
            public static void incrementCounter()
            {
                for (int a = 0; a < 100; a++)
                {
                    iCount++;
                    Thread.Sleep(1000);
                }
            }
    I understand it now. Thanks alot

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    Re: Multi-Threading in C#

    How do I stop the thread. When I used myThread.Suspend(); It tells me

    Code:
    Warning	1	'System.Threading.Thread.Suspend()' is obsolete: 
    'Thread.Suspend has been deprecated.  
    Please use other classes in System.Threading, such as Monitor, Mutex, Event, and 
    Semaphore, to synchronize Threads or protect resources.  
    http://go.microsoft.com/fwlink/?linkid=14202

    This is the ms web site http://msdn.microsoft.com/netframewo...stem.Threading


    What am I suppost to use to stop/kill the thread?

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Multi-Threading in C#

    Calling Suspend on a Thread can cause problems because there is no way of knowing what the thread was doing when you suspended it. Certain conditions could easily cause deadlock. There are numerous articles on MSDN about using threads. Here's a good starting point.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width