Click here to Skip to main content
15,888,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a windows form application project with me.
I have a submit button in it. And in its method there is a long code.
I want that when i click on the submit button the progress bar should start and end when the code in the button's method is completed completely.

What I have tried:

i tried using timer and started timer in star of button's method
and incremented progress bar by 1 in timer_click method.
but the progress bar is stuck in the starting.

i tried backgroundworker but couldn't use it.
can anyone help me achive this easily? without use of threading and all
Posted
Updated 13-Jul-17 2:18am
Comments
Ralf Meier 12-Jul-17 13:38pm    
You should show us the code where you tried to do it with the Timer.
Without seeing any code I'm not able to give you any advice ...
sameer549 13-Jul-17 1:13am    
whats problem you are facing with background worker ??
I did many such long running tasks using background worker.
checkout the simple example with background worker and try to implement.
https://www.codeproject.com/Articles/99143/BackgroundWorker-Class-Sample-for-Beginners
Gabriel Szabo 13-Aug-17 13:37pm    
UI controls are not updating because your long running method blocks the application event pump. Quick (but dirty) solution is to call Refresh() on the progress bar after advancing it (e.g. after PerformStep()). You can also call Application.DoEvents(), which will process all queued events. Background worker is the cleanest solution.

I experienced this problem too when starting with WPF, you probably need something like this:
C#
progressbar1.Dispatcher.Invoke(() => progressbar1.Value = i, DispatcherPriority.Background);
See: c# - Making a progress bar update in real time in wpf - Stack Overflow[^]
 
Share this answer
 
Please have a look at this example:

MainWindow.xaml
<Window x:Class="WpfApplication3.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"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ProgressBar Name="ProgressBar" Height="20"></ProgressBar>
    </Grid>
</Window>

MainWindow.xaml.cs
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApplication3
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();

            ProgressBar.Minimum = 0;
            ProgressBar.Maximum = 100;
            ProgressBar.Value = 0;

            DoIt();
        }

        public void DoIt()
        {
            var task = new Task(() =>
            {
                for (var i = 0; i <= 100; i++)
                {
                    SetProgressBarValue(i);

                    // Simulate some work...
                    Thread.Sleep(100);
                }
            });

            task.Start();
        }

        public void SetProgressBarValue(int value)
        {
            Application.Current.Dispatcher.Invoke(() =>
            {
                ProgressBar.Value = value;
            });
        }
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900