C#:设置线程和获取函数返回值

C#:设置线程和获取函数返回值,c#,multithreading,return-value,C#,Multithreading,Return Value,我正在尝试创建一个控制台应用程序,它将为四个函数创建四个线程,然后获取函数将返回的内容。线程1必须计算充满随机数的数组的和并返回它,线程2计算同一数组的乘积并返回它,线程3创建并返回一个介于-1000、1000和第四个之间的随机整数,取和、积和x(随机数),比较它们并显示谁比谁大 using System; using System.Collections.Generic; using System.Linq; using System.Text; usi

我正在尝试创建一个控制台应用程序,它将为四个函数创建四个线程,然后获取函数将返回的内容。线程1必须计算充满随机数的数组的和并返回它,线程2计算同一数组的乘积并返回它,线程3创建并返回一个介于-1000、1000和第四个之间的随机整数,取和、积和x(随机数),比较它们并显示谁比谁大

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

    namespace threadsproject
    {
        class Program
        {
            public int thread1(int [] a, int n)
            {
                int i = n;
                int sum = 0;
            for (int j = 0; j < i; j++)
            {
                sum = a[j] + sum;
            }
            Console.WriteLine("\nThe sum is: ");
            return sum;
        }
            public int thread2(int[] a, int n)
            {
        int prod = 1;
        for (int j = 0; j < n; j++)
        {
            prod = a[j] * prod;
        }
        Console.WriteLine("\nThe product is: ");
        return prod;
    }
    public int thread3()
    {
        Random rnd = new Random();
        int x;
        x = rnd.Next(-1000, 1000);
        Console.WriteLine("\nYour random number is: {0}", x);
        return x;
    }
    public void thread4(int sum, int prod, int x)
    {
        Console.WriteLine("\n");
        if (sum < prod && prod < x)
        {
            Console.WriteLine("T1,T2,T3");
        }
        else if (sum < x && x < prod)
        {
            Console.WriteLine("T1,T3,T2");
        }
        else if (x < sum && sum < prod)
        {
            Console.WriteLine("T2,T1,T3");
        }
        else if (x < prod && prod < sum)
        {
            Console.WriteLine("T2,T3,T1");
        }
        else if (prod < sum && sum < x)
        {
            Console.WriteLine("T3,T1,T2");
        }
        else if(prod < sum && sum == x)
        {
            Console.WriteLine("T3,T1=T2");
        }
        if (sum < prod && prod == x)
        {
            Console.WriteLine("T1,T2==T3");
        }
        else
            Console.WriteLine("T3,T2,T1");
    }

    static void Main(string[] args)
    {
        Random rnd = new Random();
        string s;
        int n;
        int [] numbers = null;
        Console.WriteLine("Give the size of the array: ");
        s = Console.ReadLine();
        n = int.Parse(s);
        for(int i=0; i<n; i++)
        {
            numbers[i] = rnd.Next(-100, 100);
        }

        int sum, prod, x;

        Thread mythread1 = new Thread(delegate() { thread1(numbers, n); });
        Thread mythread2 = new Thread(delegate() { thread2(numbers, n); });
        Thread mythread3 = new Thread(() => thread3());
        Thread mythread4 = new Thread(delegate() { thread4(sum, prod, x); });

        mythread1.Start();
        mythread2.Start();
        mythread3.Start();
        mythread4.Start();
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统线程;
命名空间线程项目
{
班级计划
{
公共整数线程1(整数[]a,整数n)
{
int i=n;
整数和=0;
对于(int j=0;j
}


因此,当我创建线程时,所有线程都会遇到相同的错误:非静态字段、方法或属性“threadsproject.Program.thread(int)”需要对象引用。我在visual studio 2013.net 4.6上工作。由于没有很好的线程编程经验,我希望得到一些帮助来了解我做错了什么。提前感谢大家

您需要将线程函数声明为静态函数。您的线程在静态主方法中调用非静态函数

您需要将线程函数声明为静态函数。您的线程在静态主方法中调用非静态函数

您需要学习C#task TPL

using System;
using System.Threading;

 namespace StackOverflowConsole
{
    class Program
   {
     private static int SUM, PROD, x;

    public static void thread1( int[] a, int n )
    {
        int i = n;
        int sum = 0;
        for ( int j = 0; j < i; j++ )
        {
            sum = a[ j ] + sum;
        }
        Console.WriteLine( "\nThe sum is: " + sum );
        SUM = sum;
    }
    public static void thread2( int[] a, int n )
    {
        int prod = 1;
        for ( int j = 0; j < n; j++ )
        {
            prod = a[ j ] * prod;
        }
        Console.WriteLine( "\nThe product is: " + prod );
        PROD = prod;
    }
    public static void thread3()
    {
        Random rnd = new Random();
        int x;
        x = rnd.Next( -1000, 1000 );
        Console.WriteLine( "\nYour random number is: {0}", x );
        Program.x = x;
    }
    public static void thread4( int sum, int prod, int x )
    {
        Console.WriteLine( "\n" );
        if ( sum < prod && prod < x )
        {
            Console.WriteLine( "T1,T2,T3" );
        }
        else if ( sum < x && x < prod )
        {
            Console.WriteLine( "T1,T3,T2" );
        }
        else if ( x < sum && sum < prod )
        {
            Console.WriteLine( "T2,T1,T3" );
        }
        else if ( x < prod && prod < sum )
        {
            Console.WriteLine( "T2,T3,T1" );
        }
        else if ( prod < sum && sum < x )
        {
            Console.WriteLine( "T3,T1,T2" );
        }
        else if ( prod < sum && sum == x )
        {
            Console.WriteLine( "T3,T1=T2" );
        }
        if ( sum < prod && prod == x )
        {
            Console.WriteLine( "T1,T2==T3" );
        }
        else
            Console.WriteLine( "T3,T2,T1" );
    }

    static void Main( string[] args )
    {
        Random rnd = new Random();
        string s;
        int n;

        Console.WriteLine( "Give the size of the array: " );
        s = Console.ReadLine();
        n = int.Parse( s );

        int[] numbers = new int[ n ];

        for ( int i = 0; i < n; i++ )
        {
            numbers[ i ] = rnd.Next( -100, 100 );
        }

        Thread mythread1 = new Thread( delegate () { thread1( numbers, n ); } );
        mythread1.Start();

        Thread mythread2 = new Thread( delegate () { thread2( numbers, n ); } );
        mythread2.Start();

        Thread mythread3 = new Thread( () => thread3() );
        mythread3.Start();

        mythread1.Join();
        mythread2.Join();
        mythread3.Join();

        Thread mythread4 = new Thread( delegate () { thread4( SUM, PROD, x ); } );
        mythread4.Start();
        mythread4.Join();

        Console.ReadLine();
    }
}
}
使用系统;
使用系统线程;
命名空间StackOverflowConsole
{
班级计划
{
私有静态整数和,PROD,x;
公共静态无效线程1(int[]a,int n)
{
int i=n;
整数和=0;
对于(int j=0;jthread3());
mythread3.Start();
myt