Click here to Skip to main content
15,894,410 members

c# thread help!!

roshini.johri asked:

Open original thread
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]
Tags: WPF, Threads

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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