C# 在多个线程上执行访问全局数组的函数

C# 在多个线程上执行访问全局数组的函数,c#,multithreading,.net-4.0,C#,Multithreading,.net 4.0,我有一个函数“graph1”,它调用另一个函数,如下所示。当我打电话时 graph1threader(copy, date); 我得到了我想要的答案,但是这需要30秒,所以我尝试使用多线程。但是当我使用 thread[copy] = new Thread(() => graph1threader(copy, date));//pass date,copy thread[copy].Start(); 我没有得到任何结果,即全局变量只包含0。为什么会这样?我该如何纠正这一点 void gr

我有一个函数“graph1”,它调用另一个函数,如下所示。当我打电话时

graph1threader(copy, date);
我得到了我想要的答案,但是这需要30秒,所以我尝试使用多线程。但是当我使用

thread[copy] = new Thread(() => graph1threader(copy, date));//pass date,copy
thread[copy].Start();
我没有得到任何结果,即全局变量只包含0。为什么会这样?我该如何纠正这一点

void graph1()
    {

        chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
        chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = false;

        for (int k = 0; k < 5; k++)
        {                
            int copy = k;
            DateTime date = G.AddDays(copy);
            refValues[copy] = 0;
            threshValues[copy] = 0;
            y_Values[copy] = 0;
            y__Values[copy] = 0;
            yValues[copy] = 0;

            //task[copy] = new Task(() => graph1threader(copy, date));//pass date,copy
            //ask[copy].Start();
            graph1threader(copy, date);

        }



            for (int j = 0; j < 5; j++)
            {
                DateTime temp = G.AddDays(j);
                string temper = temp.ToShortDateString();
                y__Values[j] = y__Values[j] - y_Values[j];
                xNames[j] = temper;
            }

        chart1.Series[1].Points.DataBindXY(xNames, y_Values);

        chart1.Series[2].Points.DataBindXY(xNames, y__Values);

        chart1.Series[3].Points.DataBindXY(xNames, refValues);

        chart1.Series[4].Points.DataBindXY(xNames, threshValues);

        chart1.Series[5].Points.DataBindXY(xNames, yValues);

    }

void graph1threader(int x, DateTime date)
    {
        DBConnect A = new DBConnect();
        List<string>[] list = new List<string>[4];
        list = A.mandetselect();
        int numberofmen = A.Countmandet();
        string[] man_name = list[0].ToArray();
        string[] trade = list[1].ToArray();
        string[] license = list[2].ToArray();
        string[] training = list[3].ToArray();
        string[] display_status = list[4].ToArray();
        //string abc;
        List<string>[] lista = new List<string>[5];
        List<string>[] listc = new List<string>[14];


        for (int j = 0; j < numberofmen; j++)
        {
            int flag = 0;
            if (!display_status[j].Equals("NO") && (selection == "ALL" || (selection == "LAE" && license[j] != "") || (selection == "NON LAE" && license[j] == "") || (selection == "ALL AVIONICS" && trade[j] == "Avionics") || (selection == "NON LAE AVIONICS" && trade[j] == "Avionics" && license[j] == "") || (selection == "LAE AVIONICS" && trade[j] == "Avionics" && license[j] != "") || (selection == "ALL AIRFRAMES" && trade[j] == "Airframes") || (selection == "NON LAE AIRFRAMES" && trade[j] == "Airframes" && license[j] == "") || (selection == "LAE AIRFRAMES" && trade[j] == "Airframes" && license[j] != "")))
            {
                refValues[x]++;
                threshValues[x] = 0.8 * refValues[x];
                string abc = man_name[j].Replace(" ", "_");
                int no_of_proj = A.Countproj(abc);//required
                lista = A.manprojselect(abc);//required
                string[] projname = lista[0].ToArray();
                string[] country = lista[2].ToArray();
                string[] startofproj = lista[3].ToArray();
                string[] endofproj = lista[4].ToArray();
                string Status = "";
                listc = A.Select();
                int numberc = A.Count();//number of projects, not required
                string[] nameofproj = listc[0].ToArray();
                string[] status = listc[13].ToArray();

                for (int l = 0; l < A.Countproj(abc); l++)
                {

                    for (int m = 0; m < numberc; m++)
                    {
                        if (nameofproj[m] == projname[l])
                        {
                            Status = status[m];
                        }
                    }


                    DateTime shuru = DateTime.ParseExact(startofproj[l],
                                   "dd-MM-yyyy hh:mm:ss",
                                   CultureInfo.InvariantCulture);
                    DateTime anth = DateTime.ParseExact(endofproj[l],
                                   "dd-MM-yyyy hh:mm:ss",
                                   CultureInfo.InvariantCulture);
                    if (date >= shuru && date <= anth)
                    {


                        if (Status != "PLANNED" && Status != "LO" && flag == 0)
                        {
                            y_Values[x]++;//BASIC UTILISATION
                            flag = 1;
                        }
                        if (Status == "IP" || Status == "OTD")
                            y__Values[x]++;//EXCESS
                        if (Status == "PLANNED")
                        {
                            yValues[x]++;//UNUTILISED

                        }

                    }

                }
            }
        }

    }
void graph1()
{
chart1.ChartAreas[0].AxisX.MajorGrid.Enabled=false;
chart1.ChartAreas[0]。AxisY.MajorGrid.Enabled=false;
对于(int k=0;k<5;k++)
{                
int copy=k;
DateTime日期=G.AddDays(副本);
refValues[copy]=0;
阈值[复制]=0;
y_值[复制]=0;
y__值[复制]=0;
Y值[复制]=0;
//任务[复制]=新任务(()=>graph1threader(复制,日期));//传递日期,复制
//询问[copy].Start();
图1螺纹机(副本,日期);
}
对于(int j=0;j<5;j++)
{
日期时间温度=G.AddDays(j);
字符串状态=临时ToSortDateString();
y__值[j]=y_值[j]-y_值[j];
xNames[j]=回火;
}
图表1.系列[1].点.DataBindXY(X名称,y_值);
图表1.系列[2].点.DataBindXY(X名称,y值);
图表1.系列[3].点.DataBindXY(xNames,refValues);
图表1.系列[4].点.DataBindXY(xNames,threshValues);
图表1.系列[5].点.DataBindXY(X名称,Y值);
}
void graph1threader(int x,DateTime日期)
{
DBConnect A=新的DBConnect();
列表[]列表=新列表[4];
list=A.mandetselect();
int numberofmen=A.Countmandet();
字符串[]man_name=列表[0]。ToArray();
字符串[]trade=list[1]。ToArray();
字符串[]许可证=列表[2]。ToArray();
字符串[]训练=列表[3]。ToArray();
字符串[]display_status=list[4]。ToArray();
//字符串abc;
List[]lista=新列表[5];
List[]listc=新列表[14];
对于(int j=0;j如果(date>=shru&&date多线程不能自动使程序运行得更快,而
Thread.Join
只是等待线程完成

基本上,如果主线程在线程完成之前无法继续,则不应使用多线程

适用于多线程的示例:

  • 当尝试从多个位置下载信息时,可以让多个线程分别从一个位置等待数据
  • 当您希望在保持用户界面响应的同时执行耗时的任务时
第二个例子可能是这里的情况,但是
Thread.Join
是一个阻塞操作:它仍然会在线程工作时阻止用户界面的更新。在这种情况下,您必须让线程通知主线程它的完成

我不知道您在什么平台上工作,但例如在windows窗体上,
Form
类有一个
Invoke
方法,允许您在窗体线程上调用方法:

class TheForm
{
    // the method running in a separate thread
    void myThread()
    {
        // do the time consuming work
        byte[] myData = ProcessData();

        // send data to the main thread 
        Invoke(new Action<byte[]>(ThreadCompleted), myData);
    }

    // will be executed in the main thread
    void ThreadCompleted(byte[] data)
    {
        // process the data
    }
}
对表单进行分类
{
//在单独线程中运行的方法
void myThread()
{
//做耗时的工作
字节[]myData=ProcessData();
//将数据发送到主线程
调用(新操作(ThreadCompleted),myData);
}
//将在主线程中执行
无效线程已完成(字节[]数据)
{
//处理数据
}
}
关于全局变量和多线程:任何线程都可以访问它们,但是不知道哪个线程何时访问它们。如果可以,应该避免让多线程访问它们,或者使用