PDA

Click to See Complete Forum and Search --> : Multi-Threading in C#


2MuchRiceMakesMeSick
Feb 5th, 2006, 11:19 AM
Can someone show me how to create two threads where each thread calls a different function. Thanks in advance.

jmcilhinney
Feb 5th, 2006, 04:45 PM
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.

2MuchRiceMakesMeSick
Feb 6th, 2006, 02:01 AM
I looked at examples but could not get them to work. This is the code I have....



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

jmcilhinney
Feb 6th, 2006, 02:19 AM
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?

2MuchRiceMakesMeSick
Feb 6th, 2006, 02:41 AM
i figured it out. I have to keep hitting button1 to have iCount increment. I was thinking it was like a timer

2MuchRiceMakesMeSick
Feb 6th, 2006, 03:00 AM
Am I suppost to call myThread.Start(); each time I want the counter incremented right?
or am I suppost to call it one time?

jmcilhinney
Feb 6th, 2006, 03:11 AM
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.

2MuchRiceMakesMeSick
Feb 6th, 2006, 03:18 AM
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

jmcilhinney
Feb 6th, 2006, 03:36 AM
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 helpThat'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.

2MuchRiceMakesMeSick
Feb 6th, 2006, 03:40 AM
So if i wanted that one thread to keep incrementing iCount I would have to setup a loop in incrementCounter() like this.


public static void incrementCounter()
{
for (int a = 0; a < 100; a++)
{
iCount++;
Thread.Sleep(1000);
}
}


I understand it now. Thanks alot :thumb:

2MuchRiceMakesMeSick
Feb 6th, 2006, 04:01 AM
How do I stop the thread. When I used myThread.Suspend(); It tells me

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/netframework/programming/obsoleteapi/ObsByNamespace.aspx#System.Threading


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

jmcilhinney
Feb 6th, 2006, 04:53 AM
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 (http://msdn2.microsoft.com/en-us/library/3e8s7xdd.aspx)'s a good starting point.