请教关于C#创建带参数线程的问题

我现在想创建一个多个线程来求两个矩阵的相乘,其中我把计算用的函数CalMatrix封装在类中方便创建线程时调用,类如下;

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

namespace Read
{
    class MatrixMultiply
    {

        public long[,] matrixC = new long[1024, 1024];

        static int i, j, t;
        public void  CalMatrix(MM m1)
        {

            for (i = 0; i < 1024; i++)
            {
                for (j = 0; j < 1024; j++)
                {
                    for (t = 0; t < 1024; t++)
                    {
                        matrixC[i, j] = matrixC[i, j] + m1.matrixA[i, t] * m1.matrixB[t, j];
                    }
                }
            }
        }
    }
}

又因为该函数需要两个参数,于是我把需要的两个数组封装到另外一个名为MM的类中,如下;

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

namespace Read
{
    class MM
    {
        public long[,] matrixA = new long[1024, 1024];
        public long[,] matrixB = new long[1024, 1024];
        static int i, j, t;
        public void BuildMatrix()
        {
            string fileAddress = @"C:\Users\asus\Desktop\random.txt";
            FileStream fs = new FileStream(fileAddress, FileMode.Open);
            StreamReader sr = new StreamReader(fs);
            for (i = 0; i < 1024; i++)
            {
                for (j = 0; j < 1024; j++)
                {
                    string dataStr = null;
                    if ((dataStr = sr.ReadLine()) != null)
                    {
                        long dataVal = Convert.ToInt64(dataStr);
                        matrixA[i, j] = dataVal;
                    }
                }
            }
            fs.Close();
            Console.WriteLine("************************");
            FileStream fs1 = new FileStream(fileAddress, FileMode.Open);
            StreamReader sr1 = new StreamReader(fs1);
            for (i = 1023; i >= 0; i--)
            {
                for (j = 0; j < 1024; j++)
                {
                    string dataStr = null;
                    if ((dataStr = sr1.ReadLine()) != null)
                    {
                        long dataVal = Convert.ToInt64(dataStr);
                        matrixB[i, j] = dataVal;
                    }
                }
            }
            fs1.Close();
            Console.WriteLine("************************");
        }
        public void ResultMatrixPrint(long[,] tx)
        {
            string fileAddress1 = @"C:\Users\asus\Desktop\result.txt";
            StreamWriter wr = new StreamWriter(new FileStream(fileAddress1, FileMode.Append));
            for (i = 0; i < 1024; i++)
            {
                for (j = 0; j < 1024; j++)
                {
                    wr.WriteLine(tx[i, j]);
                }
            }
            wr.Flush();
            wr.Close();
        }
    }
}

于是我在主函数中这样创建线程

    MatrixMultiply ma1 = new MatrixMultiply();
    ma1.CalMatrix(mm1);
    ParameterizedThreadStart threadStart = new ParameterizedThreadStart();
    Thread t1 = new Thread(threadStart);
     t1.Start(mm1);

结果出现了这样的错误提示;
主函数出现错误的代码段
于是我把创建的方式改成了这样!也出现了错误提示,其中mm1是MM的实例,ma1是MatrixMultiply的实例;
图片说明
请教各位大牛应该怎么解决这个问题呢?先谢过!

1个回答

ParameterizedThreadStart这里面要传一个参数啊,调用格式为ParameterizedThreadStart(CalMatrix),CalMatrix方法申明的时候参数只能定义为object类。
像这样public void CalMatrix(object m1),在方法体内转换一下类型好了matrixC[i, j] = matrixC[i, j] + (MM)m1.matrixA[i, t] * (MM)m1.matrixB[t, j];

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问