threading doubt

bips

Newcomer
Joined
Dec 8, 2003
Messages
3
Can i call only methods in a new thread ?
and that too methods without arguments
Is there any way i can call methods with arguments in a new thread?

Suppose i want to execute a loop n times in a new thread, where n will be userdefined .How could i do this ?

Thanks in advance
 
They share the same memory space, you might need to synchronize methods and variables though. THe Monitor class is very good for that.

Code:
using System;
using System.Threading;

namespace ConsoleApplication1
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class Class1
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			while(true)
			{
				Console.Write("Enter loop count: ");
				string loopCount = Console.ReadLine();
				try
				{
					count = int.Parse(loopCount);
					break;
				}
				catch{}
			}
			Thread thread = new Thread(new ThreadStart(Foo));
			thread.IsBackground = true;
			thread.Start();
			thread.Join();
		}

		private static void Foo()
		{
			for(int i = 0; i < count; i++)
				Console.WriteLine("Loop " + i);
		}

		private static int count;
	}
}
 
Back
Top