Created
September 4, 2018 09:31
-
-
Save fearofcode/d7e0d3b3699ce5332b772de7a4cffa67 to your computer and use it in GitHub Desktop.
evaluating python code by shelling out and piping its output to a wpf window
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Window x:Class="databinding.MainWindow" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | |
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | |
xmlns:local="clr-namespace:databinding" | |
mc:Ignorable="d" | |
Title="MainWindow" | |
Height="800" | |
Width="800"> | |
<Grid> | |
<StackPanel Orientation="Vertical" | |
Margin="10"> | |
<Button Name="BackgroundWorkerButton" | |
Height="25" | |
Content="Run Command" | |
Margin="0 5 0 5" | |
Click="BackgroundWorkerButton_Click" /> | |
<TextBox Height="500" | |
IsReadOnly="True" | |
TextWrapping="Wrap" | |
AcceptsReturn="True" | |
SelectionChanged="progressBox_SelectionChanged" | |
ScrollViewer.VerticalScrollBarVisibility="Auto" | |
Text="Output will go here" | |
Name="progressBox" /> | |
<Button Name="UIResponsiveButton" | |
Height="25" | |
Content="UI Still Works" | |
Margin="0 5 0 5" | |
Click="UIResponsiveButton_Click" /> | |
</StackPanel> | |
</Grid> | |
</Window> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.ComponentModel; | |
using System.Diagnostics; | |
using System.Text; | |
using System.Windows; | |
namespace databinding | |
{ | |
public partial class MainWindow : Window | |
{ | |
private StringBuilder stringBuilder; | |
private BackgroundWorker backgroundWorker = new BackgroundWorker() | |
{ | |
WorkerReportsProgress = true | |
}; | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
stringBuilder = new StringBuilder(1000); | |
backgroundWorker.DoWork += (s, args) => | |
{ | |
backgroundWorker.ReportProgress(0); | |
ProcessStartInfo startInfo = new ProcessStartInfo(); | |
Process p = new Process(); | |
startInfo.CreateNoWindow = true; | |
startInfo.RedirectStandardOutput = true; | |
startInfo.RedirectStandardInput = true; | |
startInfo.UseShellExecute = false; | |
startInfo.Arguments = @"C:\Users\Warren\AppData\Local\Temp\code_eval10496228199991351318\arete18400851628017276959.py"; | |
startInfo.FileName = @"C:\Program Files\Python36\python.exe"; | |
p.StartInfo = startInfo; | |
p.Start(); | |
backgroundWorker.ReportProgress(100, p.StandardOutput.ReadToEnd()); | |
p.WaitForExit(); | |
}; | |
backgroundWorker.ProgressChanged += (s, args) => | |
{ | |
stringBuilder.AppendLine((string)args.UserState); | |
progressBox.Text = stringBuilder.ToString(); | |
progressBox.ScrollToEnd(); | |
}; | |
backgroundWorker.RunWorkerCompleted += (s, args) => | |
{ | |
BackgroundWorkerButton.IsEnabled = true; | |
stringBuilder = new StringBuilder(1000); | |
}; | |
} | |
private void BackgroundWorkerButton_Click(object sender, RoutedEventArgs e) | |
{ | |
BackgroundWorkerButton.IsEnabled = false; | |
progressBox.Text = ""; | |
backgroundWorker.RunWorkerAsync(); | |
} | |
private void UIResponsiveButton_Click(object sender, RoutedEventArgs e) | |
{ | |
MessageBox.Show("UI still works"); | |
} | |
private void progressBox_SelectionChanged(object sender, RoutedEventArgs e) | |
{ | |
progressBox.ScrollToEnd(); | |
e.Handled = true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment