Search code examples
c#gitlibgit2sharp

LibGit2Sharp Unresponsiveness


I've been looking around and I'm looking for a way to make my git files pull, but not loose responsiveness of my launcher. I've done reading on asynchronously and I'm not sure if it's even possible. Right now I have

private void Install() {
       Repository.Clone("Myrepourl", "Localinstall");
}

It's a rather large bit of information to pull, so I'd like to give them updates as it moves from Rep as it pulls from 4 repos I have total. IE

private void Install() {
       Repository.Clone("url", "local");
       installstatus.Text = "Pulling next repo";
       Repository.Clone("url", "local");
}

Can this be ran asynchronously or is it only going to work synchronously?


Solution

  • Running the Clone in a Task with TransferProgress events is the way I go about it.

    Task-based Console Example:

    class MainClass
    {
        public static void Main(string[] args)
        {
            Task.Run(() =>
            {
                Repository.Clone("https://github.com/sushihangover/SVGKit.Binding", Path.Combine(Path.GetTempPath(), "foobar"),
                    new CloneOptions { OnTransferProgress = MainClass.TransferProgress });
            }).Wait();
        }
    
        public static bool TransferProgress(TransferProgress progress)
        {
            Console.WriteLine($"Objects: {progress.ReceivedObjects} of {progress.TotalObjects}, Bytes: {progress.ReceivedBytes}");
            return true;
        }
    }