1 回答
TA贡献1827条经验 获得超8个赞
所以,在谷歌搜索之后,我找到了答案,我需要像下面这样在Go上使 setter 和 getter “类似” 。
var Progress int
var DownloadSpeed int
//export DownloadFile
func DownloadFile(){
// create client
client := grab.NewClient()
req, _ := grab.NewRequest(".", "https://upload.wikimedia.org/wikipedia/commons/d/d6/Wp-w4-big.jpg")
// start download
fmt.Printf("Downloading %v...\n", req.URL())
resp := client.Do(req)
fmt.Printf(" %v\n", resp.HTTPResponse.Status)
// start UI loop
t := time.NewTicker(500 * time.Millisecond)
defer t.Stop()
Loop:
for {
select {
case <-t.C:
//progress = 100*(resp.Progress())
SetProgressValue(int(resp.Progress() * 100))
SetDownloadSpeedValue(int(resp.BytesPerSecond()))
//fmt.Println(progress)
case <-resp.Done:
// download is complete
SetProgressValue(100)
//fmt.Println(Progress)
break Loop
}
}
// check for errors
if err := resp.Err(); err != nil {
fmt.Fprintf(os.Stderr, "Download failed: %v\n", err)
os.Exit(1)
}
fmt.Printf("Download saved to ./%v \n", resp.Filename)
fmt.Println("Completed")
}
//export ProgressValue
func ProgressValue() int {
return Progress
}
//export SetProgressValue
func SetProgressValue(val int) {
Progress = val
}
然后在C#中使用:
void worker_DoWork(object sender, DoWorkEventArgs e) {
[DllImport(@"M:\GolangProjects\PatcherDLL\patcher.dll", EntryPoint = "ProgressValue")]
static extern int ProgressValue();
public partial class MainWindow : Window {
var task = Task.Factory.StartNew(() => {
DownloadFile();
});
while (!task.IsCompleted)
{
Thread.Sleep(100);
string downloadSpeedFormatted = "";
if (DownloadSpeedValue()/1000 > 999)
{
downloadSpeedFormatted = Math.Round((double) DownloadSpeedValue() / 1000000, 2) + " MB/s";
} else
{
downloadSpeedFormatted = DownloadSpeedValue() / 1000 + " kb/s";
}
Dispatcher.BeginInvoke(new Action(delegate {
progressbar1.Value = ProgressValue();
progressPercent1.Text = ProgressValue() + "%";
downloadSpeeds.Content = downloadSpeedFormatted;
}));
}
}
}
- 1 回答
- 0 关注
- 99 浏览
添加回答
举报