如何以共享库的形式异步返回函数的进度

时间:2018-10-13 10:42:09

标签: c# asynchronous go shared-libraries

所以我想使用下面的方法创建一个功能,以使用golang下载文件,我使用

将该golang项目构建到C .dll中
go build -buildmode=c-shared -o patcher.dll main.go

我设法在C#应用程序上使用此函数来获取文件下载的进度,如果我仅使用DownloadFile()直接打印它,则当前函数(DownloadFfile)可以工作,但是我想在C#应用程序上异步获取进度,但我无法直接获取该值,所以我想我需要从golang应用程序返回进度的整数,但是如果我这样做,该函数只会执行1次(进度的最后结果)

问题是如何使我的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
}

1 个答案:

答案 0 :(得分:0)

因此,在挖出Google后,我找到了答案,我需要在 Go 上使设置者和获取者“像”,如下所示。

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;
                    }));
                }
    }
}