|
-
Feb 5th, 2006, 12:19 PM
#1
Thread Starter
Lively Member
Multi-Threading in C#
Can someone show me how to create two threads where each thread calls a different function. Thanks in advance.
-
Feb 5th, 2006, 05:45 PM
#2
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.
-
Feb 6th, 2006, 03:01 AM
#3
Thread Starter
Lively Member
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
-
Feb 6th, 2006, 03:19 AM
#4
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?
-
Feb 6th, 2006, 03:41 AM
#5
Thread Starter
Lively Member
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.
-
Feb 6th, 2006, 04:00 AM
#6
Thread Starter
Lively Member
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?
-
Feb 6th, 2006, 04:11 AM
#7
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.
-
Feb 6th, 2006, 04:18 AM
#8
Thread Starter
Lively Member
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.
-
Feb 6th, 2006, 04:36 AM
#9
Re: Multi-Threading in C#
 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.
-
Feb 6th, 2006, 04:40 AM
#10
Thread Starter
Lively Member
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
-
Feb 6th, 2006, 05:01 AM
#11
Thread Starter
Lively Member
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?
-
Feb 6th, 2006, 05:53 AM
#12
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|