第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何作為共享庫異步返回函數(shù)的進(jìn)度

如何作為共享庫異步返回函數(shù)的進(jìn)度

Go
夢里花落0921 2023-04-24 15:59:18
所以我想使用下面的方法在 golang 中創(chuàng)建一個下載文件的函數(shù),我將這個 golang 項目構(gòu)建到 C .dll 中使用go build -buildmode=c-shared -o patcher.dll main.go我設(shè)法在我的 C# 應(yīng)用程序上使用這個函數(shù)來獲取文件下載的進(jìn)度,如果我只是使用 DownloadFile() 直接打印它,我當(dāng)前的函數(shù) (DownloadFfile) 就可以工作,但是我想在我的 C# 應(yīng)用程序上異步獲取進(jìn)度,但是我無法直接獲取值,所以我想我需要從我的 golang 應(yīng)用程序返回進(jìn)度的整數(shù),但如果我這樣做,函數(shù)只執(zhí)行 1 次(進(jìn)度的最后結(jié)果)問題是如何讓我的 go func DownloadFile 在我的 C# 應(yīng)用程序上被調(diào)用 1 次,但我仍然可以跟蹤進(jìn)度?任何幫助將不勝感激,謝謝。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 回答

?
斯蒂芬大帝

TA貢獻(xiàn)1827條經(jīng)驗 獲得超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;

                    }));

                }

    }

}


查看完整回答
反對 回復(fù) 2023-04-24
  • 1 回答
  • 0 關(guān)注
  • 132 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號