Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.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# 螺纹为';t起动_C#_Multithreading - Fatal编程技术网

C# 螺纹为';t起动

C# 螺纹为';t起动,c#,multithreading,C#,Multithreading,我不熟悉c#和多线程。我有这段代码来开始使用多线程,但时钟滴答没有开始。这个代码怎么了?没有发生错误,因为我想这是一个逻辑错误。任何帮助都将不胜感激 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Thread

我不熟悉c#和多线程。我有这段代码来开始使用多线程,但时钟滴答没有开始。这个代码怎么了?没有发生错误,因为我想这是一个逻辑错误。任何帮助都将不胜感激

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace Implementing_Databases
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            picturebox1.Location=new Point(0,20);
            pictureBox2.Location = new Point(0, 60);
        }
        int B1 = 0;
        int B2 = 0;
        private void Form1_Load(object sender, EventArgs e)
        {
            Thread Th1 = new Thread(Go1);
            Thread Th2 = new Thread(Go2);
            Th1.Start();
            Th2.Start();

        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            picturebox1.Left = B1;
            B1 += 5;

        }

        private void timer2_Tick(object sender, EventArgs e)
        {

            pictureBox2.Left = B2;
            B2 += 5;

        }
        void Go1()
        {
            timer1.Start();
        }
        void Go2()
        {
            timer2.Start();
        }




    }
}

首先,尝试将线程声明为窗体的属性,而不是将其声明为局部函数变量。因为否则,在加载处理程序退出后,GC可能会立即收集它们


其次,UI不更新可能是因为您无法从非GUI线程更新UI数据。请参阅WinForms编程的InvokeRequired/Invoke功能。有关更多详细信息,请参见

我没有看到任何声明的计时器?我使用了工具箱中的计时器。我猜这是在后台声明的?为什么不从
Form1\u Load
方法调用
timer1.Start()
。在这种情况下,将其包装在
线程中
不会产生任何效果。这看起来很奇怪。你能解释一下你想要实现什么吗?不是在多线程环境中工作的设计器。要使其工作,您必须调用
Start
。但你为什么要这样?再说一遍,为什么要创建两个线程,每个线程依次启动计时器?我知道我可以这样做,这将同时移动这两个PictureBox,但我想使用多线程来实现这一点。计时器线程将调用Tick回调。我已经尝试将线程声明为窗体的属性。仍然没有收获。如果您尝试调试解决方案。你能看到值是递增的吗?另外,你能告诉我定时器是如何声明和配置的吗。例如,您可能会将计时器时间段设置为无穷大。这就是为什么它没有滴答作响