Running IronPython Script in C# Asynchronously - c#

In C# 4.0 and IronPython 2.6, is it possible to execute a python script in it's own thread?
I would like to spawn off the script after passing in some event handler objects so that it can update the GUI as it runs.

I would use a Task:
ScriptEngine engine = ...;
// initialize your script and events
Task.Factory.StartNew(() => engine.Execute(...));
The IronPython script will then run on a separate thread. Make sure your event handlers use the appropriate synchronization mechanism when updating the GUI.

You could use a background worker to run the script on a separate thread. Then use the ProgressChanged and RunWorkerCompleted event handlers to update the ui.
BackgroundWorker worker;
private void RunScriptBackground()
{
string path = "c:\\myscript.py";
if (File.Exists(path))
{
worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(bw_DoWork);
worker.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
worker.RunWorkerAsync();
}
}
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// handle completion here
}
private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// handle progress updates here
}
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
// following assumes you have setup IPy engine and scope already
ScriptSource source = engine.CreateScriptSourceFromFile(path);
var result = source.Execute(scope);
}

Related

Trouble with UI Threads and Backgroundworker

What I'm trying to achieve is simple. I have a dynamic timer (one that can be changed by the user) which calls on background worker to go and fetch the user's external IP address. The combination of Timer and BackgroundWorker is causing some problems. Here's the code:
namespace IPdevices
{
/// <summary>
/// Interaction logic for Main.xaml
/// </summary>
public partial class Main : Window
{
private readonly BackgroundWorker worker;
private IPret iprep;
private Timer timer;
public Main(Client client)
{
InitializeComponent();
iprep = new IPret();
startClock();
worker = new BackgroundWorker();
worker.DoWork += worker_DoWork;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
worker.WorkerReportsProgress = true;
worker.ProgressChanged += worker_ProgressChanged;
}
private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
ipAdd.Content = e.UserState;
}
private void startClock()
{
timer = new Timer();
timer.Interval = 2000;
timer.Elapsed += new ElapsedEventHandler(clockTimer_Tick);
timer.Start();
}
private void clockTimer_Tick(object sender, ElapsedEventArgs e)
{
timer.Stop();
worker.RunWorkerAsync();
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
Console.WriteLine("Checking ip");
iprep.refresh();
worker.ReportProgress(0, iprep.getExternalIp());
Console.WriteLine("Found ip");
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
timer.Start();
}
}
}
Essentially, once the timer fires, I wish to fetch the ip address and output on a label in the application. However, I get an exception in the ProgressChanged method saying that it can't be changed because another thread owns it. Which thread is that? Is it the iprep that is owned by another thread? In fact, RunWorkerCompleted never gets fired. I'm having trouble understanding which threads own what and how objects are locked...Any insight would be appreciated.
This appears to fix it in my test of it
private void clockTimer_Tick(object sender, ElapsedEventArgs e)
{
timer.Stop();
Action a = () =>
{
worker.RunWorkerAsync();
};
Application.Current.Dispatcher.BeginInvoke(a);
}
Also, I'll note this is consistent behavior for Timer in WPF (I hadn't used it in WPF before); trying ipAdd.Content = "Tick"; in the clockTimer_Tick causes the same error. System.Timers.Timer's tick event does not happen on the UI thread.
Replace all your code by the few lines shown below. The Tick handler is executed in the UI thread. Still it asynchronously runs a background operation and does not block the UI thread.
private void StartClock()
{
var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(2) };
timer.Tick += async (o, e) => await GetIP();
timer.Start();
}
private async Task GetIP()
{
Debug.WriteLine("Checking ip");
await Task.Run(() =>
{
// Get the IP asynchronously here
});
Debug.WriteLine("Found ip");
// Update the UI here
}
ipAdd is an UI element if I am not mistaken. If it is then the problem lies on cross threading.
What happened is that Background worker is going to be running on a different thread than the UI thread. If you want to modify UI element's property you need to do it on the UI thread. One option is to use Dispatcher.Invoke but since you are using WPF, there is a better way to do it.
Do a search about MVVM design patter and move the background code into View Model. Then you could do something like
string _XXContent
public string XXContent
{
get
{
return _XXContent;
}
set
{
_XXContent = value;
OnPropertyChanged("XXContent");
}
}
private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
XXContent = e.UserState;
}
xaml :
<TextBox Content={Binding XXContent}/>
Edit:
If you are on c# 5 then you should look into async/IProgress as well an get rid of Background worker.

How do I run task like `object = ClassA.GetObject()` in a separate thread using BackgroundWorker?

I'm trying to make this line to work with BackgroundWorker:
map = Map.LoadMap(mapname);
…like this:
bw.DoWork += (map = Map.LoadMap(mapname));
It causes the error Cannot implicitly convert type 'game.Map' to 'System.ComponentModel.DoWorkEventHandler'.
I just started using BackgroundWorker as threading component for my game, but it doesn't look like it will be easy to convert all existing methods to work with it. Is there a simple way to make this work or is it better to switch to some other threading mechanism?
Note: from the threading base I need to be able to poll for progress percentage and not messing up my existing method calls.
You can leverage anonymous delegates like this:
bw.DoWork += (sender, args) => { map = Map.LoadMap(mapname); };
As I understand the type of variable map and the return type of method Map.LoadMap - are game.Map.
In your code in line
bw.DoWork += (map = Map.LoadMap(mapname));
you are doing next: get the result from Map.LoadMap(mapname), set it to variable map and after that try to use this value as a handler for DoWork event. And the type of variable map and property bw.DoWork are different.
So you just need to change this line to:
bw.DoWork += (sender, eventArgs) => { map = Map.LoadMap(mapname); }
Which will mean that you are trying to create new Delegate "(sender, eventArgs) => ..." and use it as a handler for property bw.DoWork.
Backgroundworker is good because you can use the option WorkerReportsProgress = true
this can be used to pool for a percentage
you can report progress inside the DoWork method like this
bw.ReportProgress(percentage);
I use to associate BackgroundWorker as a wrapper for what Threads would do. So I use BackgroundWorker on GUI works, and Threads on more specialized or dirty jobs (Windows Services, etc)
you dowork method has to be written like this
bw.DoWork += (sender, args) => { map = Map.LoadMap(mapname); };
You can use the BackgroundWorker like this:
var worker = new System.ComponentModel.BackgroundWorker();
worker.DoWork += delegate
{
map = Map.LoadMap(mapname);
};
worker.RunWorkerAsync();
Keep in mind that the program will continue execution immediatly after the RunWorkerAsync() method so if you use the map variable afterwards it will probably not be a loaded map.
To continue execution after the map has been loaded you need to subscribe to the RunWorkerCompleted also:
var worker = new System.ComponentModel.BackgroundWorker();
worker.DoWork += delegate
{
map = Map.LoadMap(mapname);
};
worker.RunWorkerCompleted += delegate
{
MapComplete(); // contiune with stuff here
};
worker.RunWorkerAsync();
The += operator indicates that you are attaching an event handler (DoWork is an event).
Here is an example usage:
Create an instance of the backgroundworker(in this case it will be at the class level), call the function that attaches the events SetupBackgroundWorker()
private BackgroundWorker bw = new BackgroundWorker();
private void SetupBackgroundWorker()
{
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
bw.ReportProgress = true;
}
These are sample event handlers, should give you an idea
private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{//Just as an example, I don't ever call the functions to trigger this event
int ProgressPercent = e.ProgressPercentage;
object AnyOtherDataReported = e.UserState;
}
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//Do something when the work has been completed
//Note: You should always check e.Cancelled and e.Error before attempting to touch the e.Result. I did not put that protection in this example.
object TheResultFrom_DoWork = e.Result;//This is your "map" object
}
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
//object PassedInObject=e.Argument; //This is the argument you sent to RunWorkerAsync
//Type cast PassedInObject to your correct Type
WhateverTypeItIs_YouDidntSay mapname=(WhateverTypeItIs_YouDidntSay)e.Argument
//Perform your task
object returnvalue=Map.LoadMap(mapname);//This was your varriable called "map"
//Assign the result of your task to the return value
e.Result=returnvalue;
}
Pass this function the value for mapname and if the backgroundworker is not busy doing a previous task, it should start the process.
private void ProcessTheMap_InBackground(WhateverTypeItIs_YouDidntSay mapname)
{
if (!bw.IsBusy)
{
bw.RunWorkerAsync(mapname);
}
else
{//You are already loading something in the background
}
}

How to call web service without blocking execution of client?

I have a Windows Forms application which makes calls to web services via proxies generated with SvcUtil from WSDL descriptors. These calls can last for minutes, and during this time I don't want the client app to 'freeze out'. What do I have to do to achieve this? I guess something Threading related, but I'm not sure how to manage return values and parameters in that case.
You could use a BackgroundWorker.
private void wrk_DoWork(object sender, DoWorkEventArgs e)
{
// Do your work here
}
private void wrk_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// Executed when worker completed its execution
}
private void StartIt()
{
BackgroundWorker wrk1 = new BackgroundWorker();
wrk1.DoWork += wrk_DoWork;
wrk1.RunWorkerCompleted += wrk_RunWorkerCompleted;
wrk1.RunWorkerAsync();
}
I'd go for a background worker.
Set the RunWorkerCompleted event and DoWork, run it and when you get your result in DoWork, set the event argument to your result (e.Result).
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
bw.RunWorkerAsync();
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
// Do your processing
e.Result = result;
}
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
ResultLabel.Text = (string)e.Result;
}
The examples aren't tested, but your IDE should help you out. Also you will have to resolve the BackgroundWorker, or just add
using System.ComponentModel;
More information here: http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx
Hope it helps!
You can use methods that start with Begin......
e.g, use BeginAbc() instead of Abc()
I would recommend looking into BackgroundWorkers..
BackgroundWorker proxyWorker = new BackgroundWorker();
proxyWorker.DoWork +=
(sender, args) =>
{
//make proxy call here
};
proxyWorker.RunWorkerAsync();

Passing object from backgroundworker to main thread

I have a WPF application that executes external programs to process media files, and so that the GUI doesn't freeze when the media files are being processed, I execute the process on a separate thread through backgroundworker.
private void BackgroundWorkerExecProcess(Process process)
{
BackgroundWorker worker = new BackgroundWorker();
worker.WorkerReportsProgress = false;
worker.DoWork += DoWork;
worker.RunWorkerCompleted += WorkerCompleted;
worker.RunWorkerAsync(process);
}
void DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
Process process = e.Argument as Process;
process.Start();
string stderr = process.StandardError.ReadToEnd();
//I want to display stderr on main thread
process.WaitForExit();
}
void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//some code to update gui telling user that process has finished
}
so, if there is something printed to stderr, I can see it in the debugger, but if I try to do anything with the string stderr, such as if I have a textbox called "_tbLog" and did
_tbLog.Text+=stderr;
I get an error from the compiler about them being on separate threads. is there a way to pass the object from the worker thread to the main thread?
In DoWork, set e.Result to your object. In the WorkerCompleted you can get that object back out... it will once again be e.Result of type object. Just cast it to the object it was. The WorkerCompleted should be on the correct thread.
Here is one of mine:
private void workerUpdateBuildHistory_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
UpdateStatusModel model = (UpdateStatusModel)e.Argument;
BuildService buildService = new BuildService(model.TFSUrl);
e.Result = buildService.UpdateBuildHistoryList(model);
}
private void workerUpdateBuildHistory_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
BuildHistoryListModel model = (BuildHistoryListModel)e.Result;
if (model != null)
{
listViewPastBuilds.Items.Clear();
foreach (var item in model.Builds)
{
listViewPastBuilds.Items.Add(item);
}
}
}
Use your WorkerCompleted event handler to make changes the UI, it runs on the right thread. All you have to do is pass the string to the event handler. Which is what DoWorkEventArgs.Result was designed to do. You'll retrieve it in the event handler from e.Result. Thus:
void DoWork(object sender, DoWorkEventArgs e)
{
//...
e.Result = stderr;
}
void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null) DisplayError(e.Error);
else _tbLog.Text += (string)e.Result;
}
First you need to place whatever result object (in this example, a list of strings) in the DoWorkEventArgs.Result property, then retrieve this object via the RunWorkerCompletedArgs.Result property
Then, hook up an event handler RunWorkedCompleted event of the Background worker and have it pass back whatever object you want in the RunWorkerCompletedEventArgs.Result property.
Example:
void DoWork(object sender, DoWorkEventArgs arg)
{
List<string> results = new List<string>();
results.Add("one");
results.Add("two");
results.Add("three");
arg.Results = results;
}
void WorkComplete(object sender, runWorkerCompelteEventArgs arg)
{
//Get our result back as a list of strings
List<string> results = (List<string>)arg.Result;
PrintResults(results);
}
Note: I have not tested this code, but I believe it should compile.
http://msdn.microsoft.com/en-us/library/system.componentmodel.runworkercompletedeventargs.result.aspx
http://msdn.microsoft.com/en-us/library/system.componentmodel.doworkeventargs.aspx
you can also use the dispatcher as #Zembi mentiones:
this.Dispatcher.Invoke( new Action( () => {
_tbLog.Text+=stderr;
} ) );
you can also use TPL to make sure things get run on the right thread
-edit-
Here is a good article on diffrent ways to do ui updates, including using TPL

Load objects into cache from new thread

I'm trying to use the BackgroundWorker class to start a new thread which loads a large number of objects into the cache when the website is started.
My code so far:
private void PreLoadCachedSearches()
{
var worker = new BackgroundWorker() { WorkerReportsProgress = false, WorkerSupportsCancellation = true };
worker.DoWork += new DoWorkEventHandler(DoWork);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(WorkerCompleted);
worker.RunWorkerAsync();
}
private static void DoWork(object sender, DoWorkEventArgs e)
{
// Do the cache loading...
var x = HttpContext.Current.Cache; // BUT the Cache is now null!!!!
}
private static void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// Logging?
}
I put the code in Global.asax.cs and call PreLoadCachedSearches during the Application_Start event: The new thread is started, but it fails whenever it tries to access the cache via HttpContext.Current.Cache which is null. I assume HttpContext doesn't exist/isn't available in the new thread I'm kicking off with the BackgroundWorker.
I've also tried moving the code to a separate page and start the thread manually rather than via the Application_Start event - same problem.
If I call my cache-loading code in the context of the web application (i.e. no threading) it works just fine.
How do I work around this? Pass in a reference to the cache of the main thread or access it somehow?
This question is a continuation of this previous question, Asynchronous task in ASP.NET.
You don't have an HttpContext because the thread isn't involved in servicing an Http Request.
Try HttpRuntime.Cache
You can do it by passing HttpContex.Current as parameter;
private void PreLoadCachedSearches()
{
var worker = new BackgroundWorker() { WorkerReportsProgress = false, WorkerSupportsCancellation = true };
worker.DoWork += new DoWorkEventHandler(DoWork);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(WorkerCompleted);
worker.RunWorkerAsync(HttpContext.Current);
}
private static void DoWork(object sender, DoWorkEventArgs e)
{
HttpContext.Current = (HttpContext)e.Argument;
var x = HttpContext.Current.Cache;
}
private static void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// Logging?
}

Categories