views:

93

answers:

6
+2  Q: 

run threads in c#

here is code for running threads in c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.Threading;

namespace create_thread
{
       class Program
    {
         public  delegate void ThreadStart();

        static void Main(string[] args)
        {
            Thread t=new Thread(new ThreadStart(Go));
            t.Start();
            Go();


        }
        static void Go()
        {
            Console.WriteLine("hello");

        }
    }
}

but here is errors

------ Build started: Project: create_thread, Configuration: Debug x86 ------
c:\users\7\documents\visual studio 2010\Projects\create_thread\create_thread\Program.cs(5,7): warning CS0105: The using directive for 'System' appeared previously in this namespace
c:\users\7\documents\visual studio 2010\Projects\create_thread\create_thread\Program.cs(17,22): error CS1502: The best overloaded method match for 'System.Threading.Thread.Thread(System.Threading.ThreadStart)' has some invalid arguments
c:\users\7\documents\visual studio 2010\Projects\create_thread\create_thread\Program.cs(17,33): error CS1503: Argument 1: cannot convert from 'create_thread.Program.ThreadStart' to 'System.Threading.ThreadStart'

Compile complete -- 2 errors, 1 warnings
Build started 10/25/2010 10:25:40 PM.
ResolveAssemblyReferences:
  A TargetFramework profile exclusion list will be generated.
GenerateTargetFrameworkMonikerAttribute:
Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-to-date with respect to the input files.
CoreCompile:
  C:\Windows\Microsoft.NET\Framework\v4.0.30319\Csc.exe /noconfig /nowarn:1701,1702 /nostdlib+ /platform:x86 /errorreport:prompt /warn:4 /define:DEBUG;TRACE /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\Microsoft.CSharp.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\mscorlib.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Core.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Data.DataSetExtensions.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Data.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Xml.dll" /reference:"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\System.Xml.Linq.dll" /debug+ /debug:full /filealign:512 /optimize- /out:obj\x86\Debug\create_thread.exe /target:exe Program.cs Properties\AssemblyInfo.cs "C:\Users\7\AppData\Local\Temp\.NETFramework,Version=v4.0,Profile=Client.AssemblyAttributes.cs"

Build FAILED.

Time Elapsed 00:00:00.30
========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped ==========

please help

A: 

Don't know what is the purpose, but try to remove the line: public delegate void ThreadStart();

Jan Suvak
+5  A: 

The problem here is you're shadowing an existing type used by the Thread constructor.

public  delegate void ThreadStart(); 

This causes your code to bind to your ThreadStart instead of the version in the System.Threading namespace and hence leads to a copmilation error. Remove this definition.

JaredPar
+1  A: 

get rid of the declaration of the ThreadStart delegate. You also have one too many using System statements:

using System;
using System.Threading;

namespace create_thread
{
    class Program
    {        
        static void Main(string[] args)
        {
            Thread t = new Thread(new ThreadStart(Go));
            t.Start();
            Go();
        }
        static void Go()
        {
            Console.WriteLine("hello");
        }
    }
}

You should also be aware that your code will run the Go method twice - once on the main thread, and once on a background thread - so you will see "hello" twice in the console.

Mark Heath
A: 

double using System; and remove your delegate ThreadStart

lukas
+2  A: 

The problem is that you declared your own ThreadStart delegate instead of using .NET one.

Andrew Bezzub
+1  A: 

after fixing your error then delete your own definition of ThreadStart Delegate and use .net Framework version with correct signature

Jahangir Zinedine