后台工作返回值

本文关键字:返回值 工作 后台 | 更新日期: 2023-09-27 18:16:30

private float bw_DoWork(object sender, DoWorkEventArgs e)

我试图让线程方法返回一个值,但我得到

Assets/Scripts/test.cs(574,71): error CS0407: A method or delegate float test.bw_DoWork(object, System.ComponentModel.DoWorkEventArgs)' return type does not match delegate `void System.ComponentModel.DoWorkEventHandler(object, System.ComponentModel.DoWorkEventArgs)' return type

怎么做呢?

后台工作返回值

This should do the trick. Keep in mind you will need to return your values differently you cannot return in a thread. The best way is to Invoke inside a separate thread.
private string  ReturnObject {get;set;}
DoWork(){
UpdateValues("My Value");
}     
UpdateValues(string _value){
MethodInvoker action = delegate
                {
                   //Controls Mehtods HERE
ReturnObject = _value;
                };
     this.Invoke(action)
}