test2 是一个文本框 显示测试数字的.
这样子完全跑不起来,运行会卡死.
去掉 Thread.Sleep(1000); 后能跑,但是跑2秒左右就会变成一直显示99999 好像是变成最后一个建立的线程在跑 其它的都没了.
本人新手,能帮看下吗?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
System.Threading.Timer threadTimer;
delegate void qianggou(int id);
public void ThreadMethod(Object state)
{
this.Invoke(new qianggou(jdordertotal), state);
}
private void qiang_Click(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
threadTimer = new System.Threading.Timer(new System.Threading.TimerCallback(ThreadMethod), i, 0, 100);
Thread.Sleep(100);
}
}
public void jdordertotal(int id)
{
TimeSpan ts = Convert.ToDateTime("2014-3-9 11:47:00") - DateTime.Now;
double zms = ts.TotalMilliseconds;
double zs = ts.TotalMilliseconds / 1000;
string tt = zs.ToString("0.0");
timestatus.Text = tt;
if (zs < 60)
{
testbig(id);
}
}
public void testbig(int id)
{
test2.Text = test2.Text + id.ToString();
Thread.Sleep(1000);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
3 个解决方案
#1
#2
多线程 要用委托 调用
#3
#1
#2
多线程 要用委托 调用