C#线程帮助! [英] c# thread help!!

查看:102
本文介绍了C#线程帮助!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下是我的代码.我正在使用线程来调用一个函数.(该函数通过从日志文件中读取值来更新进度条)我需要调用5次,每次使用diff参数(2个参数)..所以我做了一个类它的对象将由两个参数初始化.因此,我在主程序中初始化了对象,并通过传递对象从那里调用了一个线程. (该类具有需要在线程上调用的功能.但是我不断收到以下错误:

非静态字段,方法或属性``System.Windows.Threading.Dispatcher.Invoke(System.Delegate,System.Windows.Threading.DispatcherPriority,params object [])''

这是我上课的代码:

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]添加了代码块以保留格式-OriginalGriff [/edit]



[edit]Code blocks added to preserve formatting - OriginalGriff[/edit]

推荐答案

也许您会发现本教程很有帮助
http://msdn.microsoft.com/en-us/library/aa645740 (v = vs.71).aspx
Maybe you could find this tutorial helpful
http://msdn.microsoft.com/en-us/library/aa645740(v=vs.71).aspx


报废,没有正确阅读

Dispatcher.Invoke不是静态方法,因此您需要对DispatcherObject的引用

http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcherobject.dispatcherobject.aspx [ ^ ]

所以你可以在这里看到

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为您提供了对Dispatcher对象的引用



http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.invoke.aspx[^]


this.Dispatcher gives you a reference to the Dispatcher object


这篇关于C#线程帮助!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆