0

I started programming just few month ago so I am a pure beginner but I needed a math app with Heavy probability calculation and an UI to view/interact so I searched around and found C#.net was the best way... fantastic power...

It will help me a lot to progress if I am able do this :

Wpf window with textbox, in textbox a number is printed each time the calculation is done but the Ui should respond everytime

I tried to do it using latest net 4.5 because the app need to be fast (so use the most recent tech and not timer or background task)

and it work but the Ui is stuck and cannot move (because of my bad access to the text box I think)

If guys could help me it will be great and I thank you all because I learned a lot with your posts!

Here is my wrong newbie code

private Random rng; public MainWindow() { rng = new Random(); InitializeComponent(); Task.Run((Func<Task>) Calc); } private async Task Calc() { while (true) { textBox1.Dispatcher.Invoke (DispatcherPriority.Normal , new Action(delegate() { textBox1.Text = rng.NextDouble().ToString(); } ) ); } } 

1 Answer 1

4

You're tightlooping, admittedly in a non-UI-thread, but adding a bazillion delegates to invoke in the UI thread... so your UI is just too busy.

All your work is basically going on in the UI thread at the moment - you're not even computing rng.NextDouble() in the background thread.

Also note that you've got an async method without an await expression, which should have triggered a compiler warning - you should take heed of that.

You say you don't want to use a "background task" - but that's exactly what you are doing by calling Task.Run.

Now I'm assuming your real code doesn't actually just need to create random numbers. What does your real calculation code look like, and which thread does it occur in?

Sign up to request clarification or add additional context in comments.

9 Comments

maybe I misspoke I just dont want to use the old background task and timer available in old windows form to be able to get all the power of new 4.5 framework the calculation code should run in a separate thread than the Ui and each time a new number is available send it to the Ui which should never be stuck the code is just a csv download (one thread) and the calculation
@analystLp: If you're already running the calculation code in a separate thread, then I'm not sure async/await is really what you need. So long as you use Dispatcher.Invoke, you should already be fine - if you're not overloading the UI thread as your sample does.
ok then I will continue to learn how to create a true background calculation thread which send number with dispatcher thank you !
Ok I think I have be able to do it the right way with the following code because start() method works while RunSynchronously() doesn't... too bad I am not good enough to use the new c#5.0 async and await keyword for now :p Task task = new Task(new Action(Calc)); task.Start(); private void Calc() { rng = new Random(); while (true) { textBox1.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate() { textBox1.Text = rng.NextDouble().ToString(); })); } }
@analystLp: That code is still tightlooping. How do you expect the UI to do anything when you're sending it a constant stream of random numbers? It doesn't sound like async is really a good fit for what you're doing here, although you haven't really explained enough about what you're doing to say for sure.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.