Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows XAML Metro
I am trying to create a Windows 8 Metro version of my Huo Chess, by using Visual Studio Express 2012 for Windows 8 and XAML/C#. I seem to have a problem with refreshing the chessboard.
 
When the user enters a move, the program redraws the chessboard and then calls the ComputerMove function to make the HY think its own move. The problem is that because the ComputerMove function does a lot of things, the program does not refresh the chessboard until the computer stops thinking. In that way it seems that the application did not successfully accepted the user's move.
 
It seems to me that the DoEvents function does not work.
 
I read this article, but again it isn't very helpful.
 
http://web3.codeproject.com/Articles/271598/Application-DoEvents-in-WPF[^]
 
Any suggestions?
Posted 22-Sep-12 13:27pm
Palavos23.1K
Comments
Sergey Alexandrovich Kryukov at 22-Sep-12 22:39pm
   
Using DoEvents is a bad idea in most cases (and this article has little value, if any). You need to use separate thread(s) and System.Windows.Threading.Dispatcher.
--SA
Palavos at 23-Sep-12 4:48am
   
But I do not want to do anything fancy here. There is only one thread that has to run and that is the computer thinking thread. I just want to update a textBox showing the words "Thinking..." before the computer starts thinking (or after the human player has played, I just want to update an image with the piece the human player moved, before calling the ComputerMove).
 
I have read articles stating that DoEvents is a bad idea, but they refer to having many chuncks of code doing things that could interfere. Not to having to update a textBox or an image just before you call one (just one) function. Surely I do not have to start a whole new thread just to update a textBox, do I? :)
 
I just hoped a presentation-oriented technology like Metro would have some inherent mechanisms to cover that simple case.
 
I did study some more and found out that you are right in the threading part: from the advent of Silverlight and WPF asynchronous calling of threads is the way to go for updating the UI.
 
Will try this way and tell you what happened.
Sergey Alexandrovich Kryukov at 23-Sep-12 12:08pm
   
DoEvent is overly fancy, if you thing about it, too fancy to be practical. If I don't recommend it, I know what I say. First, clearly recognize what DoEvent does, instead of just using it as a trick.
 
And threads is straightforward, if, again, you think about it. If you don't want to follow my very practical advice, don't. But if you change your mind and need some explanations, I would gladly help you.
--SA
Palavos at 23-Sep-12 20:05pm
   
But I did. Take a look at the Solution I found and posted below.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Solution found!
 
See the code below:
 
RedrawTheChessboard(HuoChess_main.ChessBoard);
textBox_main.Text = "Thinking..."
 
//Application.DoEvents();
await Task.Run(() = HuoChess_main.ComputerMove(HuoChess_main.ChessBoard));
 
I call the RedrawChessboard function and then call the ComputerMove function in an asynchronous manner (await keyword used). In that way the UI remains responsive and updates as it should, while the computer AI is working...
 
In order to do that, I should declare the function which contained the abovementioned code with the "async" keyword, so as to allow it to conduct asynchronous callings.
 
The MSDN article Keep the UI thread responsive (Windows Store apps using C#/VB/C++ and XAML) proved really useful!
 
Thanks also to Sergey for the answer which pointed me to the right direction.
 
Will post the updated "Huo Chess" article soon enough!
  Permalink  
v3

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

  Print Answers RSS
0 CPallini 335
1 BillWoodruff 309
2 Sergey Alexandrovich Kryukov 290
3 George Jonsson 274
4 Gihan Liyanage 230
0 OriginalGriff 4,895
1 CPallini 4,010
2 Sergey Alexandrovich Kryukov 3,514
3 George Jonsson 2,826
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 23 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100