Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Updated 26-Jan-11 4:05am
v2
Comments
Manfred Rudolf Bihy 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

Maybe you could find this tutorial helpful
http://msdn.microsoft.com/en-us/library/aa645740(v=vs.71).aspx
 
Share this answer
 
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,

C#
// 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
 
Share this answer
 
v2
Comments
roshini.johri 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 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)



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