2
private void Module_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)      
{
   // new SolidColorBrush(Color.FromArgb(32, 0, 0, 0)) belongs to black  color

   LayoutRoot.OpacityMask =  new SolidColorBrush(Color.FromArgb(32, 0, 0, 0));

   // Some code over here...

}

First statement is to update the OpacityMask of grid named LayoutRoot. Here code after first statement is a large process which will take around one minute or more to execute. But opacity mask only changes background i.e. opacity of page after one minute once this event executed.enter code here Please help me so that opacity mask could be run, before that long code which will take around 1 or more minutes to execute.

Clemens
  • 123,504
  • 12
  • 155
  • 268
  • @Clemens yes you are correct, I could use opacity. But still its not changing the opacity of grid until it execute the whole code. However it is written in the top of thread. Please help me with a solution, so that i could change the opacity of grid in the start. – Praveen sharma Feb 15 '18 at 11:38

1 Answers1

2

The code in your event handler method blocks the UI thread.

Declare the handler method async, and call the long running code in a Task Action:

private async void Module_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)      
{
    LayoutRoot.Opacity = 0.125;

    await Task.Run(() =>
    {
        // long running code...
    });

    LayoutRoot.Opacity = 1;
}
Clemens
  • 123,504
  • 12
  • 155
  • 268
  • Thanks @Clemens for your valuable feedback, But await method is available in .Net Framework 5 and above. However we are working on .Net Framework 4.5. Please share another possible solution. – Praveen sharma Feb 15 '18 at 11:50
  • @Praveensharma: You are mixing things a little bit. `async/await` is available since C# 5 which is .net4.5. So normally you should be able to use it. As a reference you can take a look at [What are the correct version numbers for C#?](https://stackoverflow.com/a/247623/5893316) – Martin Backasch Feb 15 '18 at 12:14
  • @MartinBackasch I have another way to do the same, please correct me is this a correct way or above solution is correct way. We can call LayoutRoot.Refresh method and its also working fine – Praveen sharma Feb 15 '18 at 12:19
  • public static class ExtensionMethods { private static Action EmptyDelegate = delegate () { }; public static void Refresh(this UIElement uiElement) { uiElement.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate); } } – Praveen sharma Feb 15 '18 at 12:19
  • @Praveensharma: I can't tell which method is the best way to solve your problem, but Clemens Answer looks more like a proper way to do it. Without the context in which you are using your method it is hard to tell anything. You may update your Question or ask a new one to get more reliable answers about that. – Martin Backasch Feb 15 '18 at 15:19