62,046
社区成员
发帖
与我相关
我的任务
分享
while(true)
{
if (ret == -32)
textBox1.Text = "请放入手指!";
else if (ret == -33)
textBox1.Text = "请拿开手指!";
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
namespace test
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
//public delegate void deleTest();
private TextBox tb;
public MainWindow()
{
InitializeComponent();
//FileType = "nihao";
this.tb = textBox1;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
int a=4;
// FileType = "hello";
while (a >= 0)
{
if (a == 4)
{
Thread thread1 = new Thread(new ThreadStart(this.ForFun2));
thread1.Start();
}
if (a == 3)
{
Thread thread1 = new Thread(new ThreadStart(this.ForFun3));
thread1.Start();
}
if (a == 2)
//textBox1.Text = "你在哪";
if (a == 1)
this.tb.Text = "大爷成功了";
a--;
}
}
private void ForFun2()
{
this.tb.Text = "原来如此!";
Thread.Sleep(3000);
}
private void ForFun3()
{
this.tb.Text = "大爷郁闷!";
Thread.Sleep(3000);
}
}
}
public delegate void UpdateTextCallback(string message);
private void TestThread()
{
for (int i = 0; i <= 1000000000; i++)
{
Thread.Sleep(1000);
textBox1.Dispatcher.Invoke(
new UpdateTextCallback(this.UpdateText),
new object[] { i.ToString() }
);
}
}
private void UpdateText(string message)
{
richTextBox1.AppendText(message + "\n");
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Thread test = new Thread(new ThreadStart(TestThread));
test.Start();
}
代码来自:
http://stackoverflow.com/questions/4253088/c-updating-gui-wpf-using-a-different-thread