Click here to Skip to main content
15,915,842 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: ComboBox and Binding Pin
User 27100929-Jun-09 15:39
User 27100929-Jun-09 15:39 
QuestionFloating control at the bottom of the web page?? Pin
Sunil P V29-Jun-09 7:44
Sunil P V29-Jun-09 7:44 
AnswerRe: Floating control at the bottom of the web page?? Pin
Mark Salsbery29-Jun-09 9:25
Mark Salsbery29-Jun-09 9:25 
GeneralRe: Floating control at the bottom of the web page?? Pin
Sunil P V30-Jun-09 5:53
Sunil P V30-Jun-09 5:53 
GeneralRe: Floating control at the bottom of the web page?? Pin
Mark Salsbery30-Jun-09 7:08
Mark Salsbery30-Jun-09 7:08 
GeneralRe: Floating control at the bottom of the web page?? Pin
Sunil P V30-Jun-09 22:15
Sunil P V30-Jun-09 22:15 
QuestionProgressBar Pin
Ranger4929-Jun-09 7:33
Ranger4929-Jun-09 7:33 
AnswerRe: ProgressBar [modified] Pin
Mark Salsbery29-Jun-09 9:00
Mark Salsbery29-Jun-09 9:00 
Ranger49 wrote:
tried my code in a sample program that only had an i-loop that did nothing and there my progressBar worked fine...


Maybe it just appeared to work fine - a loop from 0 to 99 is pretty fast on modern machines. Smile | :)

When I ran your code, even the lines didn't show up until the end of the loop.

This is because you're hogging the UI thread for the duration of the loop.

It may be a better idea to do busy work on a separate thread. Here's an example
using a BackgroundWorker thread (I added a Sleep() to slow things down a bit since on
my machine this loop finishes faster than I can see):
BackgroundWorker bw = new BackgroundWorker();

public Window1()
{
    InitializeComponent();

    bw.WorkerReportsProgress = true;
    bw.DoWork += new DoWorkEventHandler(bw_DoWork);
    bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
}

private void button1_Click(object sender, RoutedEventArgs e)
{
    if (!bw.IsBusy)
        bw.RunWorkerAsync();
}

void bw_DoWork(object sender, DoWorkEventArgs e)
{
    for (int i = 0; i < 100; i++)
    {
        bw.ReportProgress(i);

        Thread.Sleep(50);
    }
}

void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    Random rnd = new Random();
    Pen p = new Pen();
    byte red, green, blue;

    SolidColorBrush brush = new SolidColorBrush();

    Line line = new Line();
    red = (byte)rnd.Next(0, 255);
    green = (byte)rnd.Next(0, 255);
    blue = (byte)rnd.Next(0, 255);
    Color col = new Color();
    col.R = red;
    col.G = green;
    col.B = blue;
    col.A = 255;
    brush.Color = col;

    line.X2 = rnd.Next(0, (int)canvas1.ActualWidth);
    line.Y2 = rnd.Next(0, (int)canvas1.ActualHeight);
    line.X1 = rnd.Next(0, (int)canvas1.ActualWidth);
    line.Y1 = rnd.Next(0, (int)canvas1.ActualHeight);
    line.Stroke = brush;
    line.StrokeThickness = 1.0;

    canvas1.Children.Add(line);

    progressbar1.Value = e.ProgressPercentage;
}


*edit*  Funny side note:  Taking the Sleep() call out of my example makes it<br />
run too fast to get a newly seeded Random object so only a few lines appear<br />
(because there's so many duplicate lines :)).  I suppose without the Sleep() call,<br />
the Random object should be created only once outside the loop.  That change could <br />
look something like this:

void bw_DoWork(object sender, DoWorkEventArgs e)
 {
     Random rnd = new Random();

     for (int i = 0; i < 100; i++)
     {
         bw.ReportProgress(i, rnd);

         //Thread.Sleep(25);
     }
 }

 void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
 {
     Random rnd = e.UserState as Random;
  ...


Mark Salsbery
Microsoft MVP - Visual C++

Java | [Coffee]

modified on Monday, June 29, 2009 3:12 PM

GeneralRe: ProgressBar [modified] Pin
Ranger4929-Jun-09 9:33
Ranger4929-Jun-09 9:33 
GeneralRe: ProgressBar Pin
Mark Salsbery29-Jun-09 10:00
Mark Salsbery29-Jun-09 10:00 
GeneralRe: ProgressBar Pin
Ranger4929-Jun-09 10:15
Ranger4929-Jun-09 10:15 
GeneralRe: ProgressBar Pin
Pete O'Hanlon29-Jun-09 10:05
mvePete O'Hanlon29-Jun-09 10:05 
GeneralRe: ProgressBar [modified] Pin
Mark Salsbery29-Jun-09 10:16
Mark Salsbery29-Jun-09 10:16 
GeneralRe: ProgressBar Pin
Ranger4929-Jun-09 10:26
Ranger4929-Jun-09 10:26 
QuestionManually firing WPF RoutedCommand Pin
DahrkDaiz29-Jun-09 5:16
DahrkDaiz29-Jun-09 5:16 
AnswerRe: Manually firing WPF RoutedCommand Pin
Pete O'Hanlon29-Jun-09 6:06
mvePete O'Hanlon29-Jun-09 6:06 
AnswerRe: Manually firing WPF RoutedCommand Pin
User 27100929-Jun-09 15:48
User 27100929-Jun-09 15:48 
QuestionMessage Removed Pin
29-Jun-09 4:54
professionalN_tro_P29-Jun-09 4:54 
AnswerRe: Images and Namespaces Pin
Gideon Engelberth29-Jun-09 6:29
Gideon Engelberth29-Jun-09 6:29 
QuestionWPF custom control Pin
Vinod C S29-Jun-09 4:21
Vinod C S29-Jun-09 4:21 
AnswerRe: WPF custom control Pin
Christian Graus29-Jun-09 11:13
protectorChristian Graus29-Jun-09 11:13 
GeneralRe: WPF custom control Pin
Vinod C S29-Jun-09 19:42
Vinod C S29-Jun-09 19:42 
QuestionEliminating The standard shape of a bitmap(actually xaml based) button, How to :)? Pin
cppwxwidgetsss29-Jun-09 1:12
cppwxwidgetsss29-Jun-09 1:12 
AnswerRe: Eliminating The standard shape of a bitmap(actually xaml based) button, How to :)? Pin
Mark Salsbery29-Jun-09 6:41
Mark Salsbery29-Jun-09 6:41 
QuestionHow can I use Application Idle Event in WPF? Pin
Kunal Chowdhury «IN»28-Jun-09 23:00
professionalKunal Chowdhury «IN»28-Jun-09 23:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.