Hey all,
I'm very new to programming (erm day 3 today) so i apologize if my terminology is wrong.
I've been learning from a very good book. But i decided to go and learn a few things on my own. One of the main things that intrested me is multi threading.
I seemed to get how the basics worked. Untill i tried to start a method with paramiters. After some fiddleing and some googling i managed to get it working by changing my int value on the method to an object value.
My question is simply WHY do i need to define my variable as an object to make it work? I just cant seem to get my head around it. Why can i not do something like Thread tid1 = new Thread(bob.thead1(1000));
Code:
using System;
using
System.Collections.Generic;using
System.Linq;using
System.Text;using
System.Threading;namespace
ConsoleApplication1{
class Program
{
static void Main(string[] args){
doit bob = new doit(); Thread tid1 = new Thread(bob.thead1); Thread tid2 = new Thread(bob.thead1); Thread tid3 = new Thread(bob.thead1); Thread tid4 = new Thread(bob.thead1);tid1.Start(10);
tid2.Start(30);
tid3.Start(500);
tid4.Start(254);
}
}
class doit{
public void thead1(Object val){
for (int i = 0; i < 50; i++){
Console.WriteLine("Processing " + i); Thread.Sleep(Convert.ToInt32(val));}
}
}
}
Thanks in advance.
James
Ryan AlfordPosted Jul 23, 2008, 12:37 PM
if the designers would have created the Thread class to use any other datatype, they would have had to create a Thread method for every datatype. so instead, they used the "object" datatype which will handle all other datatypes.
JamesPosted Jul 23, 2008, 10:03 AM
Thanks for your reply!
Here is where my lack of knowledge shows.
So it was just the way that the Thread class was created? With only the possibility to use objects? So would it have been possible for the creator(s) of the Thread class to allow for other data types. But they just didn’t?
Also thanks for the advice on not getting in to deep! I do have a habit of just running with things I’m trying to learn. Multi Threading just looked interesting.
Thanks,
James
EDIT: I should read the object brower more. Loads of good info there! (See Below)
public void Start(object parameter)
Member of System.Threading.Thread
Summary:
Causes the operating system to change the state of the current instance to System.Threading.ThreadState.Running, and optionally supplies an object containing data to be used by the method the thread executes.Ryan AlfordPosted Jul 23, 2008, 9:17 AM
Mahesh ChandPosted Jul 23, 2008, 9:16 AM
It's because of the syntax defined in the language classes. If an Object type is required in a method, it is required. Language compiler can understand object only in that case, your your class.
It's like if you start talking french to a person who knows english only.
Good luck !!!