所以我想使用下面的方法在 golang 中创建一个下载文件的函数,我使用
将这个 golang 项目构建到 C .dll 中go build -buildmode=c-shared -o patcher.dll main.go
我设法在我的 C# 应用程序上使用此函数来获取文件下载进度,如果我只是使用 DownloadFile() 直接打印它,我当前的函数 (DownloadFfile) 就可以工作,但是我想在我的 C# 应用程序上异步获取进度,但我无法直接获取值,所以我想我需要从我的 golang 应用程序返回进度的整数,但如果我这样做,该函数只执行 1 次(进度的最后结果)
问题是如何让我的 go func DownloadFile 在我的 C# 应用程序上被调用 1 次,但我仍然可以跟踪进度? 任何帮助将不胜感激,谢谢。
func DownloadFile(){
// create client
client := grab.NewClient()
req, _ := grab.NewRequest(".", "http://www.golang-book.com/public/pdf/gobook.pdf")
// 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:
fmt.Printf("%.2f%",
//resp.BytesComplete(),
//resp.Size,
100*resp.Progress())
case <-resp.Done:
// download is complete
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)
// Output:
// Downloading http://www.golang-book.com/public/pdf/gobook.pdf...
// 200 OK
// transferred 42970 / 2893557 bytes (1.49%)
// transferred 1207474 / 2893557 bytes (41.73%)
// transferred 2758210 / 2893557 bytes (95.32%)
// Download saved to ./gobook.pdf
}
最佳答案
所以,在搜索谷歌之后,我找到了答案,我需要像下面这样在 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;
}));
}
}
}
关于c# - 如何作为共享库异步返回函数的进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52792071/