请教一个简单问题(多线程的)
先说一下我的问题:主要就是想实现(winform中)1.在时钟里更新数组,
2.在一个线程中利用这个数组的值画出曲线。
下面是我写的程序,图就画了一次不知道怎么回事?请高手指教。。。。
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsApplication3
{
public partial class Form1 : Form
{
static int[] a = new int[10];
static int[] b = new int[10];
static int count = 0;
Object obj = new Object();//
public Form1()
{
InitializeComponent();
}
private void draw()
{
Graphics g = pictureBox1.CreateGraphics();
Pen pen1 = new Pen(Color.Red, 2);
g.Clear(Color.White);
lock (a)
{
for (int i = 0; i < 9; i++)
g.DrawLine(pen1, a[i], b[i], a[i + 1], b[i + 1]);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
lock (a)
{
for (int i = 0; i < 10; i++)
{
a[i] = (50 + count * 10) * i;
if (i % 2 == 0) b[i] = 30;
else b[i] = 50;
}
if (count < 10) count++;
else count = 0;
}
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
a[i] = 50 * i;
if (i % 2 == 0)b[i] = 30;
else b[i] = 50;
}
}
private void button1_Click(object sender, EventArgs e)
{
ThreadStart ts = new ThreadStart(draw);
Thread thread = new Thread(ts);
thread.Start();
timer1.Enabled = true;
}
}
}