线程未启动

时间:2015-07-13 13:08:09

标签: 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.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();
        }




    }
}

1 个答案:

答案 0 :(得分:0)

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

其次,UI未更新可能是由于您无法从非GUI线程更新UI数据。请参阅WinForms编程的InvokeRequired / Invoke功能。有关详细信息,请参阅https://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired(v=vs.110).aspx

相关问题