Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF progress threads
Hey
 
the following is my code. I am using threads to call upon a function.(the function updates a progress bar by reading in values from a log file) I need to call it 5 times, each time with diff parameters(2 parameters)..so i made a class where its object would get initialized by the two parameters. So i initialize the object in my main program and call a thread from there by passing the object. (the class has the function i need to call on the thread. But i keep getting the following error:
 
An object reference is required for the non-static field, method, or property 'System.Windows.Threading.Dispatcher.Invoke(System.Delegate, System.Windows.Threading.DispatcherPriority, params object[])'
 
this is my code for the class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Shapes;
using System.Windows.Threading;
using System.Threading;
 
namespace EzmrSense1
{
   public class progressClass
    {
        public string file;
        public ProgressBar pbar = new ProgressBar();
        bool test = true;
        public delegate void UpdateProgressBarDelegate(System.Windows.DependencyProperty dp, object Value);
        public progressClass(string file1, ProgressBar pbar1)
        {
            file = file1;
            pbar = pbar1;
        }
        public void progressThreadFunction()
        {
 
            double value = 0;
            System.IO.StreamReader reader = new System.IO.StreamReader(file);
            string readerLine = reader.ReadLine();
            UpdateProgressBarDelegate UpdatePbDelegate = new UpdateProgressBarDelegate(pbar.SetValue);//delegate object
            do
            {
                readerLine = reader.ReadLine();
                value = Convert.ToInt32(readerLine);
                System.Threading.Thread.Sleep(100);
                //Dispatcher.Invoke(UpdatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });
                Dispatcher.Invoke(UpdatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new Object[] { ProgressBar.ValueProperty, value });
                
            } while (readerLine != null && test == true);
        }
    }
}
 
------and the main program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Shapes;
using System.Threading;
 
namespace EzmrSense1
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		bool test = true;
		//private delegate void UpdateProgressBarDelegate(System.Windows.DependencyProperty dp,object Value); //necc for updating progress bar
		public MainWindow()
		{
			this.InitializeComponent();
           
		}
		
        //start button code
       
        private void start(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
			while (test == true)
            {
            progressClass myBar1 = new progressClass("C:\\Users\\Roshini\\Desktop\\emotionbar\\log2.txt",Pbar1);
            Thread newThread = new Thread(new ThreadStart(myBar1.progressThreadFunction));
            newThread.Start();   
            }
        }
        //stop button code
        private void stop(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
			 test = false;
        }
        //exit button code
        private void exit(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
			 Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
             Application.Current.Shutdown();
        }
	}
}
 
[edit]Code blocks added to preserve formatting - OriginalGriff[/edit]
Posted 26-Jan-11 5:01am
Edited 26-Jan-11 5:05am
v2
Comments
Manfred R. Bihy at 26-Jan-11 15:39pm
   
Moved from OP's "answer"
Hey,
 
thanks for ur answer. However the problem was in the while loop in the main program. Because I used a while it kept spawning the threads! When i changed my while loop to a if it started working. Also I wasn't using my stop condition properly. The stop button in the main program and the stop in the class are two different objects! When i put conditions on both the objects it seemed ok. However there is still some lag in the progress bars..no idea what to do about that...is there some way to start all threads at the same time?? I think the lag might be because the threads start one at a time!
 
cheers,Roshini
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Maybe you could find this tutorial helpful
http://msdn.microsoft.com/en-us/library/aa645740(v=vs.71).aspx
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Scrap that, didn't read it properly
 
Dispatcher.Invoke is not a static method, so you need a reference to a DispatcherObject
 
http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcherobject.dispatcherobject.aspx[^]
 
So you can see here,
 
// Places the delegate onto the UI Thread's Dispatcher
private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
    // Place delegate on the Dispatcher.
    this.Dispatcher.Invoke(DispatcherPriority.Normal,
        new TimerDispatcherDelegate(TimerWorkItem));
}
 
http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.invoke.aspx[^]
 

this.Dispatcher gives you a reference to the Dispatcher object
  Permalink  
v2
Comments
roshini.johri at 26-Jan-11 10:45am
   
no.. it didn't work..that gives an error. also the tutorials mentioned that i have to use ProgressBar.ValueProperty :(
Dylan Morley at 26-Jan-11 10:53am
   
Sorry, see updated

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



Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 26 Jan 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100