[MainPage.xaml]


<UserControl x:Class="RiaProgressBar.MainPage"

    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"

    mc:Ignorable="d"

    d:DesignHeight="300" d:DesignWidth="400">

 

    <Grid x:Name="LayoutRoot" Background="White">

        <StackPanel>

            <TextBlock Text="시간:" FontSize="14" VerticalAlignment="Center" />

            <ProgressBar x:Name="Progress1" Minimum="0" Maximum="10" Height="30" />

            <Button Content="오래걸리는 작업 실행" FontSize="14" x:Name="btnGo" />

        </StackPanel>

    </Grid>

</UserControl>









[MainPage.xaml.cs]


using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using System.ComponentModel;

using System.Threading;

 

namespace RiaProgressBar

{

    public partial class MainPage : UserControl

    {

        public MainPage()

        {

            InitializeComponent();

 

            btnGo.Click += new RoutedEventHandler(btnGo_Click);

        }

 

        void btnGo_Click(object sender, RoutedEventArgs e)

        // 다운로드 또는 프로그레스바 진행시 현재 프로그램과 무관한 다른 스레드에서 특정 기능 실행

        {

            BackgroundWorker worker = new BackgroundWorker();

            worker.WorkerReportsProgress = true;

            worker.DoWork += new DoWorkEventHandler(worker_DoWork);

            worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);

            worker.RunWorkerAsync();

        }

 

        void worker_DoWork(object sender, DoWorkEventArgs e)

        {

            int step = 1; // 1씩 증가

            int newValue = 0;

            while (newValue < 10)

            {

                newValue += step;

                BackgroundWorker worker = sender as BackgroundWorker;

                worker.ReportProgress(newValue);

                Thread.Sleep(500); // 0.5초 대기

            }

        }

 

        void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)

        {

            this.Progress1.Value = e.ProgressPercentage; // 변경된 값 출력           

        }

    }

}

 



















Posted by holland14
: