如何使用C语言编写的DLL引用属性 [英] how ref a property using a DLL writed by c language

查看:74
本文介绍了如何使用C语言编写的DLL引用属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用WPF编写我的界面.我将ListView用作任务列表.任务列表包含两列FileName,Progress.每行绑定到TaskInfo:

I use WPF to write my interface. and I use ListView as my task list.Task list contains two columns,FileName,Progress.every row binding to a TaskInfo:

public class TaskInfo : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public TaskInfo(string fp)
    {
        FileSystemInfo fsi= new DirectoryInfo(fp);
        FileName = fsi.Name;   
        FilePath = fp;     
        PbValue = 0;

    }
    private int pbvalue;
    public int PbValue
    {
        get { return pbvalue; }
        set
        {
            pbvalue = value;
            onPropertyChanged("PbValue");
        }
    }
    private string filename;
    public string FileName
    {
        get { return filename;}
        set
        {
            filename = value;
            onPropertyChanged("FileName");
        }
    }
    private string filepath;
    public string FilePath
    {
        get { return filepath;}
        set
        {
            filepath = value;
            onPropertyChanged("FilePath");
        }
    }

    private void onPropertyChanged(string name)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(name));
        }
    }
}


在进度"列中,它包含一个ProgressBar,将其值绑定到PbValue.但由于某些原因,我应该使用C语言编写的Dll.我使用的功能是:


In Progress column,it contain a ProgressBar,binding its Value to PbValue. but But For Some Reason,I should use a Dll writed by C language. the function I use is:

WaterErase(char * filepath, int * pbvalue)


我在c#中定义它:


I definite it in c#:

public extern static void WaterErase(string filepath, ref int pbvalue)


要执行多任务,我写了一个线程:


to perform multitask, I write a Thread:

class TaskThread
{
    private TaskInfo taskinfo = null; //task information
    private Thread thread;
    public TaskThread(TaskInfo _taskinfo)
    {
        taskinfo = _taskinfo;
    }
    private void run()
    {   

        WaterErase(taskinfo.FilePath, ref taskinfo.PbValue);

    }       
    public void start()
    {
        if (thread == null)
        {
            thread = new Thread(run);
            thread.Start();
        }
    }
}


但是这行:


but the line:

WaterErase(taskinfo.FilePath, ref taskinfo.PbValue);


有问题:


have problem:

A property, indexer or dynamic member access may not be passed as an out or ref parameter


我知道为什么会出现此问题,但是如何解决此问题以实现此功能实时更改PbValue.这样我就可以在ProgressBar中直观地执行任务进度.
还是在DLL中有一些建议?


I know why this problem comes up,but how to solve this problem to achieve this function that change the PbValue in real time. So I can perform the task progress in ProgressBar intuitively.
Or do you have some suggestion in DLL?

推荐答案

当然,您不能将属性用作refout参数.如果您考虑属性如何工作,您将理解原因.解决方法代码可能如下所示:

Of course you cannot use properties as ref or out parameters. If you think at how properties work, you will understand why. The work-around code may look like this:

int tempPbValue = taskinfo.PbValue;
WaterErase(taskinfo.FilePath, ref tempPbValue);
taskinfo.PbValue = tempPbValue;



—SA



—SA


这篇关于如何使用C语言编写的DLL引用属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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