Main.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Windows.Threading;
namespace thread_exam
{
/// <summary>
/// MainWindow.xaml에 대한 상호 작용 논리
/// </summary>
public partial class MainWindow : Window
{
bool runok = false;
delegate void Work();
public MainWindow()
{
InitializeComponent();
}
private void Btn1_Click(object sender, RoutedEventArgs e)
{
runok = !runok;
if (runok)
{
Btn1.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.SystemIdle, new Work(Display));
Btn1.Content = "Stop";
}
else
{
Btn1.Content = "Resume";
}
}
public void Display()
{
int k = int.Parse(TxBx1.Text);
k++;
TxBx1.Text = k.ToString();
Label1.Content = k.ToString();
if (runok)
{
Btn1.Dispatcher.BeginInvoke(DispatcherPriority.SystemIdle, new Work(Display));
}
}
}
}
xaml
<Window x:Class="thread_exam.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:thread_exam"
mc:Ignorable="d"
Title="MainWindow" Height="301" Width="498">
<Grid>
<TextBox x:Name="TxBx1" HorizontalAlignment="Left" Height="23" Margin="107,60,0,0" TextWrapping="Wrap" Text="1" VerticalAlignment="Top" Width="101" TextAlignment="Right"/>
<Button x:Name="Btn1" Content="Button" HorizontalAlignment="Left" Margin="130,143,0,0" VerticalAlignment="Top" Width="75" Click="Btn1_Click"/>
<Label x:Name="Label1" Content="Label" HorizontalAlignment="Left" Margin="144,106,0,0" VerticalAlignment="Top"/>
</Grid>
</Window>
실행화면