Click here to Skip to main content
15,889,851 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
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
Comments
Sergey Alexandrovich Kryukov 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 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 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 23-Sep-12 20:05pm    
But I did. Take a look at the Solution I found and posted below.

1 solution

Solution found!

See the code below:

XML
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!
 
Share this answer
 
v3

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