So I want to make a function for download a file in golang using this method below, I build this golang project into C .dll using
go build -buildmode=c-shared -o patcher.dll main.go
I managed to use this function on my C# application to get progress of file download, my current function (DownloadFfile) works if I just print it directly using DownloadFile() , however i want to get the progress asynchronously on my C# application , but i can't get the value directly, so I think I need to return integer of progress from my golang application but if I do that the function just go 1 times (the last result of progress)
The question is how to make my go func DownloadFile being called 1 times on my C# application but I can still keep track the progress ? Any help will be appreciated , thank you.
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...
", req.URL())
resp := client.Do(req)
fmt.Printf(" %v
", 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
", err)
os.Exit(1)
}
// fmt.Printf("Download saved to ./%v
", 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
}
So, after digging out Google, I found the answer, I need to make setter and getter "a like" on Go like below.
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...
", req.URL())
resp := client.Do(req)
fmt.Printf(" %v
", 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
", err)
os.Exit(1)
}
fmt.Printf("Download saved to ./%v
", resp.Filename)
fmt.Println("Completed")
}
//export ProgressValue
func ProgressValue() int {
return Progress
}
//export SetProgressValue
func SetProgressValue(val int) {
Progress = val
}
Then usage in 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;
}));
}
}
}