简体   繁体   中英

c# backgroundworker reportProgress return percentage and Struct

my backgroundworker should return the percentage and also an object. Its a struct.

serialPort.DiscardInBuffer(); // clear InBuffer
serialPort.Write(adv_request, 0, 3); // Write byte array to serial port, with no offset, all 3 bytes
process_DoWork(serialPort);
worker.ReportProgress(0, pfc_parsedData); //return parsed data to main thread

Here I get an exception "System.InvalidCastException"

public void request_ProgressChanged(object sender, ProgressChangedEventArgs e) //this function is need update mainthread controls
{
    if (e.ProgressPercentage == 0)
    {
         sbStatus.Text = "Logging active...";
         Pfc_parsedData pfc_parsedData = (Pfc_parsedData)sender;
    }
    else
    {
         sbStatus.Text = "Offline";
    }
}

The sender is the background worker, not the data that you provided when reporting progress. That data is in e.UserState .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM