如何手动取消阻止UI线程

时间:2017-04-29 12:57:43

标签: android multithreading xamarin

您好我在Xamarin.Android Visual Studio中有一个项目将一些图像复制到SD卡,因为我需要在没有主UI被阻止的情况下进行循环停止,我已经在Async / await中测试了一些解决方案,但这并没有真正起作用。

这是我的代码需要等待显示进度条等等的事情。

void ExportBitmapAsPNG(Bitmap bitmap, string filename)
        {
            var sdCardPath = Android.OS.Environment.ExternalStorageDirectory.Path;
            var filePath = System.IO.Path.Combine(sdCardPath + dirPath + "/images", filename);
            var stream = new FileStream(filePath, FileMode.Create);
            bitmap.Compress(Bitmap.CompressFormat.Png, 100, stream);
            stream.Close();
            progressBar.IncrementProgressBy(5);
            txtLoading.Text = "Loop Loading : " + progressBar.Progress.ToString();
            RunOnUiThread(() => Thread.Sleep(1000));
            Console.WriteLine("Image {0} Created", filename);

        }

我该怎么做?谢谢

1 个答案:

答案 0 :(得分:0)

你的bitmap.Compress可能导致一些冻结以及你的睡眠。我会尝试这样的事情

async Task ExportBitmapAsPNG(Bitmap bitmap, string filename)
 {
        var sdCardPath = Android.OS.Environment.ExternalStorageDirectory.Path;
        var filePath = System.IO.Path.Combine(sdCardPath + dirPath + "/images", filename);
        var stream = new FileStream(filePath, FileMode.Create);
        await bitmap.CompressAsync(Bitmap.CompressFormat.Png, 100, stream);
        stream.Close();
        progressBar.IncrementProgressBy(5);
        txtLoading.Text = "Loop Loading : " + progressBar.Progress.ToString();
        await Task.Delay(1000);
        Console.WriteLine("Image {0} Created", filename);

    }