1

I just wrote a program in C# to make a ui and integrate ironpython in it to do some calculation. But when I call the function the UI Freezes till the function is over ( even the progress bar freezes )

Pardon me if the question is silly as this is my first question on StackOverFlow.com

The function is :

private void ValidateB_Click(object sender, RoutedEventArgs e)
{
    string txt = proInp.Text;
    var eng = Python.CreateEngine();
    var searchPaths = eng.GetSearchPaths();
    searchPaths.Add("F:\\Python27\\Lib");
    searchPaths.Add("F:\\Python 3.6\\Lib");
    eng.SetSearchPaths(searchPaths);
    var scope = eng.CreateScope();
    scope.SetVariable("key", txt);
    eng.Execute("import os\nkey="os.getcwd()", scope);
}
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Priyansh Anand
  • 178
  • 1
  • 16
  • 2
    The issue is everything by default runs in a single thread including UI elements. So, while it's waiting to process the python code it can't repaint the screen. You'll need to use either multi-threading or a background worker for that code so your UI can continue to run asynchronously. – Nathan Champion Dec 12 '18 at 14:58
  • WPF ui controls have thread affinity. Get whatever values you use and pass them to a task you run in a separate thread. – Andy Dec 12 '18 at 14:59
  • How can I implement multithreading with yield ( using IronPython ) – Priyansh Anand Dec 15 '18 at 18:31

2 Answers2

2

A simple way to solve this is to make the Click Handler async and run a new Task (But if you have any GUI interaction that should not be done inside the Task):

private async void ValidateB_Click(object sender, RoutedEventArgs e)
{
   await Task.Factory.StartNew(() => {
       string txt = proInp.Text;
       var eng = Python.CreateEngine();
       var searchPaths = eng.GetSearchPaths();
       searchPaths.Add("F:\\Python27\\Lib");
       searchPaths.Add("F:\\Python 3.6\\Lib");
       eng.SetSearchPaths(searchPaths);
       var scope = eng.CreateScope();
       scope.SetVariable("key", txt);
       eng.Execute("import os\nkey="os.getcwd()", scope);
   }
}

But better would be to use with a async Command and a Binding. May be read about WPF and MMVM to see how this works.

Daniel W.
  • 938
  • 8
  • 21
  • I just want to know how can I use "YIELD" method from Iron-python using this way (running separate threads ) – Priyansh Anand Dec 13 '18 at 03:41
  • I'm not sure if this accords also to ironpython but with C# yield have a look at:https://stackoverflow.com/questions/5061761/is-it-possible-to-await-yield-return-dosomethingasync – Daniel W. Dec 13 '18 at 07:05
  • It didn't work and I'm still finding a way to implement "yield" but didn't found anything important – Priyansh Anand Dec 15 '18 at 18:30
1

I recomend using the TAP Pattern (Task-based Asynchronous Pattern) without await (since you want to get responsive UI immediately) as follows:

    private void ValidateB_Click(object sender, RoutedEventArgs e)
    {
        Task t = Task.Factory.StartNew(() =>
        {
            string txt = proInp.Text;
            var eng = Python.CreateEngine();
            var searchPaths = eng.GetSearchPaths();
            searchPaths.Add("F:\\Python27\\Lib");
            searchPaths.Add("F:\\Python 3.6\\Lib");
            eng.SetSearchPaths(searchPaths);
            var scope = eng.CreateScope();
            scope.SetVariable("key", txt);
            eng.Execute("import os\nkey="os.getcwd()", scope);
        });
    }
techron
  • 17
  • 3
  • What is the difference b/w TAP vs Await Method – Priyansh Anand Dec 16 '18 at 10:36
  • Notice that the await keyword is necessary for a method that uses async. Sometimes you actually want to wait for the async method/lambda function to complete and sometimes you want to get on with your responsive application. It depends on the context. If you just call an ...Async function it will run asynchronously. – techron Dec 16 '18 at 11:12
  • I find this not that good, because if you want to return something after the action has finished you need to invoke, what produces less readable code. – Daniel W. Dec 17 '18 at 07:15