Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将线程名称添加到线程列表中_C#_Multithreading - Fatal编程技术网

C# 将线程名称添加到线程列表中

C# 将线程名称添加到线程列表中,c#,multithreading,C#,Multithreading,我目前正在做这个练习,我必须将线程的名称添加到列表中。此练习基于表单,因此请使用windows窗体应用程序。每次用户按下开始按钮时,一个新线程将同时启动。该线程的名称应添加到列表中,其中运行的每个线程的名称分配为1,2,3,4。。。等等 但每次我使用for循环时,它都会给我一个错误“索引越界”,我做错了什么?有人能帮忙吗 更新 基本上,这个练习是参数化的,每次用户按下开始按钮时都需要画圆圈。我把我的名单列在了全球 List <Thread> _threadName = new Lis

我目前正在做这个练习,我必须将线程的名称添加到列表中。此练习基于表单,因此请使用windows窗体应用程序。每次用户按下开始按钮时,一个新线程将同时启动。该线程的名称应添加到列表中,其中运行的每个线程的名称分配为1,2,3,4。。。等等

但每次我使用for循环时,它都会给我一个错误“索引越界”,我做错了什么?有人能帮忙吗

更新

基本上,这个练习是参数化的,每次用户按下开始按钮时都需要画圆圈。我把我的名单列在了全球

List <Thread> _threadName = new List <Thread>();
private void UI_btnStartThread_Click(object sender, EventArgs e)
{
      //create new thread and the referenced the method
      Thread circleThread = new Thread(new ParameterizedThreadStart(DrawCircle));

      //the data is passed to the thread as an argument
     circleThread.Start(new AddCircle(_cDrawer, Color.FromArgb(_mainRandom.Next(0, 256), _mainRandom.Next(0, 256), _mainRandom.Next(0, 256)), _circleSize));

     for(int i=1;i<5;i++)
     {
          circleThread.Name = i.ToString();
          //assigning to list
          _threadName[i] = circleThread;
     }
}
List\u threadName=newlist();
私有void UI\u btnStartThread\u单击(对象发送者,事件参数e)
{
//创建新线程并引用该方法
Thread circleThread=新线程(新参数化的ThreadStart(DrawCircle));
//数据作为参数传递给线程
circleThread.Start(新的AddCircle(_cDrawer,Color.FromArgb(_mainRandom.Next(0,256),_mainRandom.Next(0,256),_mainRandom.Next(0,256)),_circleSize));

对于(int i=1;i基本上,此练习是参数化的,每次用户按下开始按钮时都需要画圆圈。我将列表设置为全局。

private void UI_btnStartThread_Click(object sender, EventArgs e)
        {

            //create new thread and the referenced the method
            Thread circleThread = new Thread(new ParameterizedThreadStart(DrawCircle));

            //the data is passed to the thread as an argument
            circleThread.Start(new AddCircle(_cDrawer, Color.FromArgb(_mainRandom.Next(0, 256), _mainRandom.Next(0, 256), _mainRandom.Next(0, 256)), _circleSize));

            for(int i=1;i<5;i++)
            {
                circleThread.Name = i.ToString();
                 //assigning to list
                _threadName[i] = circleThread;
            }
        }
private void UI\u btnStartThread\u单击(对象发送方,事件参数e)
{
//创建新线程并引用该方法
Thread circleThread=新线程(新参数化的ThreadStart(DrawCircle));
//数据作为参数传递给线程
circleThread.Start(新的AddCircle(_cDrawer,Color.FromArgb(_mainRandom.Next(0,256),_mainRandom.Next(0,256),_mainRandom.Next(0,256)),_circleSize));

对于(int i=1;i听起来似乎有一些问题。首先,听起来好像
\u threadName
没有足够的元素来处理
i
的范围。您可以使用
列表来避免这个问题,而不是
Thread[]
,因为它会根据需要增长

其次,您正在使用最新线程信息更新列表中的每个元素。我认为您只想附加到列表中,保留所有现有值并添加新线程

private List<Thread> _threads = new List<Thread>();

private void UI_btnStartThread_Click(object sender, EventArgs e)
{
    //create new thread and the referenced the method
   Thread newThread = new Thread(new ParameterizedThreadStart(DrawCircle));

   //the data is passed to the thread as an argument
   newThread.Start(new AddCircle(_cDrawer, Color.FromArgb(_mainRandom.Next(0, 256), _mainRandom.Next(0, 256), _mainRandom.Next(0, 256)), _circleSize));

    _threads.Add(newThread);
}

你们做错的事情很少。点击按钮——你们会产生一个线程

Thread circleThread = new Thread(new ParameterizedThreadStart(DrawCircle));

            //the data is passed to the thread as an argument
            circleThread.Start(new AddCircle(_cDrawer, Color.FromArgb(_mainRandom.Next(0, 256), _mainRandom.Next(0, 256), _mainRandom.Next(0, 256)), _circleSize));
然后循环4次,重命名同一个线程并将其添加到_threadName集合(数组)


您可以共享您的代码吗?是的,共享为Ans什么数据类型是
\u threadName
?它是
字符串[]
?数据类型为Thread否不是。您的代码无法编译。它必须是集合、数组或具有索引属性的其他内容。请修改示例,以包含声明变量的代码
\u threadName
。我尝试使用数组,但它给了我一个异常“索引越界”@denise-索引越界当您访问数组大小以外的索引时将发生。也就是说,如果数组大小为10,并且您尝试访问arr[10]——这是第11项。请记住数组从索引0开始。
Thread circleThread = new Thread(new ParameterizedThreadStart(DrawCircle));

            //the data is passed to the thread as an argument
            circleThread.Start(new AddCircle(_cDrawer, Color.FromArgb(_mainRandom.Next(0, 256), _mainRandom.Next(0, 256), _mainRandom.Next(0, 256)), _circleSize));
for(int i=1;i<5;i++)
{
  circleThread.Name = i.ToString();
  //assigning to list
 _threadName[i] = circleThread;
}
 List<Thread> _AllThreads = new List<Thread>(); // This will hold ref to all threads you create
private void UI_btnStartThread_Click(object sender, EventArgs e)
{
     Thread myThrd = new Thread(......)
     myThrd.Name = _AllThreads.Count + 1;
     // Start the Thread here

     _AllThreads.Add(myThrd);
}