Issue
I have problems with showing up a ProgressDialog:
The following snippet shows my code in an example:
public override void OnActivityCreated(Bundle savedInstanceState)
{
base.OnActivityCreated(savedInstanceState);
progressDialog = new ProgressDialog(Context);
progressDialog.SetCancelable(false);
progressDialog.SetMessage("Saving ...");
}
private void Method()
{
progressDialog.Show();
var testDialog = progressDialog.IsShowing;
SaveImage();
}
private void SaveImage()
{
// does lot
...
// especially this method takes some time
mutableBitmap.Compress(Bitmap.CompressFormat.Png, 100, stream);
}
I create a ProgressDialog, which I want to show before calling the SaveImage Method (and Cancel it when SaveImage is done).
My problem is that the ProgressDialog will not be displayed until the SaveImage Method is done.
In Debug-Mode, progressDialog.IsShowing is true, before the SaveImage-Method is called, but it is not displayed.
Solution
I found a solution that works fine for me:
private async void Method()
{
progressDialog.Show();
var thread = new System.Threading.Thread(new ThreadStart(delegate
{
SaveImage();
}));
thread.Start();
while(thread.ThreadState == ThreadState.Running)
{
await Task.Delay(100);
}
progressDialog.Dismiss();
}
Meanwhile, I outsourced this code into a public static class ThreadHelper
, that just takes an Action (in this case SaveImage()) and a ProgressDialog as parameters.
Answered By - Aiko West
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.