Click here to Skip to main content
15,941,828 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
Using VS2010 c# for windows phone, how do I make process iteration (method for or while or repeat) not like crash and showing progress finish if this method run iteration very long.

private void button4_Click(object sender, RoutedEventArgs e)
        {
            progressBar1.Maximum = 10000;
            for (int i = 1; i <= 10000; i++)
            {
                textBox1.Text = Convert.ToString(i);
                progressBar1.Value = i;
            }
        }



While the process is running, I want progress bar show the progress step by step.
thanks
Posted
Updated 22-Jun-11 21:57pm
v2
Comments
Dalek Dave 23-Jun-11 4:01am    
Edited for Clarity, but still unclear.

The code makes no sense, but probably you need to process some real data and notify the progress in UI later on.

You need an extra thread, nothing else (don't repeat common fallacy, don't use timers)!

Create a separate thread. This is a code sample for a very robust way:
How to pass ref parameter to the thread[^].

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
 
Share this answer
 
v2
Comments
thatraja 22-Jun-11 22:11pm    
Complete answer, 5!
Sergey Alexandrovich Kryukov 22-Jun-11 22:32pm    
Thank you, Raja.
--SA
Richard MacCutchan 23-Jun-11 3:51am    
Some really useful information to get to grips with this issue; +5.
Sergey Alexandrovich Kryukov 23-Jun-11 3:52am    
Thank you, Richard.
--SA
What are you trying to measure here? Your loop will not take long to reach 10000 and so it's unlikely that your user will see the interim displays. You need to synchronise your code with whatever background activity you are trying to monitor.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Jun-11 14:50pm    
Good point. Something which makes no sense; but maybe this is just experimenting.
--SA
Sergey Alexandrovich Kryukov 22-Jun-11 14:54pm    
Please see also my solution.
--SA
Pete O'Hanlon 23-Jun-11 4:57am    
Balanced out the rating imbalance. It looks correct now.
For starters, you should never set progressBar1.Value = i with out first ensuring that i is not greater than your maximum value.
i.e. prograssBar1.Value = Math.Min( progressBar1.Maximum, i ); or something of that nature. Then you also need to take into account Richard's answer as well.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Jun-11 14:50pm    
Good point, my 5.
--SA
Sergey Alexandrovich Kryukov 22-Jun-11 14:54pm    
See also my solution.
--SA
Dalek Dave 23-Jun-11 4:01am    
Good Advice.
For any processing which takes 'a long time', you should not run it on the UI thread. 'A long time' has various definitions but anything over a second and you are probably into it. I'm not sure if the mobile framework supports the Task Parallel library that's part of desktop .Net 4, but I'm pretty sure it does support BackgroundWorkerThreads and normal threading, thread pool etc from .Net 2. You should start your long running process as a Task or BackgroundWorkerThread, and the long running code should fire status update events which can be hooked by the main application to update a UI control (don't forget to use Invoke or Begin/EndInvoke to do that part as status events will be raised on the background thread).
 
Share this answer
 
Just simply add a windows timer, when the timer tick, add one to the process bar value.

Besides, you need start a thread to handle the task. When the task ends, invoke the UI thread to remove the process bar and let user know the task is done.

From Athena Solution, Singapore (Please No advertisement)
 
Share this answer
 
v2
Comments
Richard MacCutchan 23-Jun-11 7:31am    
Adding a timer is not the answer here; the update of the progress bar needs to be synchronised with whatever background task is being measured. See the suggestions of SAKryukov.
Hello,

What about this ?

C#
private int counter;
System.Timers.Timer timer = new System.Timers.Timer();

public MYForm()
{
     InitializeComponent();
     timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
}

void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    counter++;
}


public void MyMethod()
{

    progressBar1.Maximum = 10000;
    timer.Interval = 500;
    timer.Start();
    counter = 0;
    for (int i = 1; i <= 100000000; i++)
    {
        if (counter == 10)
            break;
        textBox1.Text = Convert.ToString(i);
        progressBar1.Value = i;
    }
}
 
Share this answer
 
Comments
Dalek Dave 23-Jun-11 4:02am    
Fair enough.

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