Click here to Skip to main content
15,885,936 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
I want to do this without blocking the UI:
C#
show_something_in_WPF()
wait_x_seconds()
show_something_else_in_WPF()

I know what you're thinking. Whatever you're waiting for, do it on another thread. You're an idiot. You don't understand async programming, etc.

Let me express the problem another way.

My application is a scripting host for a Python script. I am exposing a very minimal API to the user. In this application I am expecting the person who wrote the Python script to have very little programming experience; let alone understand threading. For example, this is a scriptlet:
Python
# Show the photos.
images = [ 
    "bunny.jpg", "chick.jpg", "dog.jpg", "ducky.jpg", "elephant.jpg", 
    "fox.jpg", "hamster.jpg", "husky.jpg", "kitten.jpg", "lemur.jpg",
    "lizard.jpg", "lynx.jpg", "meercat.jpg", "monkey.jpg", "owl.jpg",
    "penguin.jpg", "piglet.jpg", "polar-bear.jpg", "seal.jpg", "turtle.jpg"
]

for image in images:
    imageView = ImageView('Cute\\images\\' + image)
    mainRegion.Clear()
    mainRegion.Add(imageView)
    UI.Wait(5)

Since the scripting host is compiling and executing the script, I don't have the ability to intercept every line of code as it is executed (do I?) and consider if I should be awaiting the next line, set up threads etc.

The UI.Wait(5) that you see there does actually set up a timer, and queues up and chains timers, (and it works) but maintaining a timer essentially for every line of code is a huge undertaking, because I don't want the user to be limited to using only my API methods.

In Windows Forms there was the ugly hack to tell the UI thread to just shut up and do its thing for a while in Application.DoEvents()

I've tried a few methods without much luck.

Any ideas on how to make a WPF application appear to run synchronously are welcome.

Meanwhile I'll dig around Scripting Host stuff to see if I can parse lines individually somehow.
Posted
Comments
Sergey Alexandrovich Kryukov 31-Jan-15 23:27pm    
Please, synchronize what with what? First of all, wait x second is not synchronization at all, this is something opposite, related to the notion of race condition. So, why do you even mention each script line (script execution does not follow lines anyway)? You need to specify what should happen in which order. (And no, ApplicationDoEvents() does not do what you described.)
—SA

1 solution

What about using the WPF Dispatcher? Something like:


C#
void AnotherThreadFunc()
{
    var disp = Application.Current.Dispatcher;

    disp.BeginInvoke(new Action(() => { show_something_in_WPF(); }));

    wait_x_seconds();

    disp.BeginInvoke(new Action(() => { show_something_else_in_WPF(); }));
}
 
Share this answer
 
Comments
Yvan Rodrigues 5-Feb-15 9:27am    
Thanks, I new a fresh mind on the problem would help. My problem was that since almost everything in the application was UI-related, I did it on the UI thread. That was fine until I needed to do something on a background thread. By inversing command i.e. running everything from the background thread, using dispatcher for all UI calls, this becomes a non-issue since I can Thread.Sleep (or whatever) on the background thread, while the UI thread just runs its message loop.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900