作者xa9277178 (枫曦)
看板Programming
标题[请益] C# thread sleep 使用for回圈问题
时间Mon Aug 22 22:05:04 2016
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;
using System.Diagnostics;
namespace WindowsFormsApplication11
{
public partial class Form1 : Form
{
Thread sample1;
Thread sample2;
Thread sample3;
Thread sample4;
Thread sample5;
Thread sample6;
public Form1()
{
InitializeComponent();
}
private void _ThreadFunction()
{
Thread.Sleep(8000); //延
迟几秒
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 1; i <= 2; i++)
{
string mm = DateTime.Now.ToString("H-m-s");
textBox1.Text = (mm);
sample1 = new Thread(_ThreadFunction);
sample1.Start();
Process.Start(@"C:\Users\wind\Desktop\春泥.mp3");
}
}
上面这段程式码我撷取到第一个button,我是想做一个计时器,可以每8秒提醒我一次,
为什麽会使用thread来做是因为我一次设计了6个计时器,尴尬地发现sleep的时候
我不能按其他几个计时器按钮,原先这样尝试是OK的。
当我加入了for之後,我自己的想法里,经过Start()的时候,不是应该要静止8秒吗?
可是我却发现...他连开两次春泥(process.start的歌),可是我是希望每8秒循环一次,
放歌出来提醒我,请问我有忽略到哪个部分,恳请大神指导。
顺带一问,其实我只是..想要有类似按键精灵那种wait的方法,不要让我整个
程式卡住,可是网路上的方法千奇百怪,有些根本看不懂,这个方式已经是最简单
我能理解的了...
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 1.175.47.235
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Programming/M.1471874708.A.5AA.html
1F:→ Expsun: UI Thread通常只有一条112.105.222.108 08/23 06:12
2F:→ fo40225: 你开了新的执行绪 在新执行绪设定暂停 61.230.213.158 08/23 09:35
3F:→ fo40225: UI thread还是继续执行 程式流程在新 61.230.213.158 08/23 09:36
4F:→ fo40225: thread.Start()时就分岔了 61.230.213.158 08/23 09:37
5F:推 joe791023: 可以试试看 await Task.Delay(8000), 220.133.169.73 09/21 23:32
6F:→ joe791023: 但是方法前要加上 async 220.133.169.73 09/21 23:32