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);
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 });
} 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
{
public partial class MainWindow : Window
{
bool test = true;
public MainWindow()
{
this.InitializeComponent();
}
private void start(object sender, System.Windows.RoutedEventArgs e)
{
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();
}
}
private void stop(object sender, System.Windows.RoutedEventArgs e)
{
test = false;
}
private void exit(object sender, System.Windows.RoutedEventArgs e)
{
Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
Application.Current.Shutdown();
}
}
}
[edit]Code blocks added to preserve formatting - OriginalGriff[/edit]