Problem with C# Forms and Invalidate
I am writing a program that reads data on a card automatically and then displays it on a C# form. I'm using a label to display the information. I am using a thread to pull the information off the card constantly, but my problem is that even though i am calling Invalidate and Update the text won't update unless I click on the screen or move the mouse.
Please help.
Thanks.
Sorry about the delay in posting. I had a bit of an emergency. Hopefully someone can help me with this still. Here is my code. Thanks.
pCardScanThread.Suspend();
while (!pCardScanThread.pThreadStopped)
System.Threading.Thread.Sleep(10);
currentPlayer.serviceAmt = pCardScanThread.Balance;
currentPlayer.gameRoom = pCardScanThread.GameRoom;
currentPlayer.cardNum = pCardScanThread.CardNum;
CashRegCardNumTextLabel.Text = currentPlayer.cardNum.ToString();
decimal dispBalance = currentPlayer.serviceAmt / CONVERSION_AMOUNT;
CashRegCardBalanceTxtLabel.Text = dispBalance.ToString("N2");
pCardScanThread.Resume();
CashRegCardBalanceLabel.Invalidate();
CashRegCardBalanceLabel.Update();
CashierRegisterPanel.Invalidate();
CashierRegisterPanel.Update();
pCardScanThread.Suspend();
while (!pCardScanThread.pThreadStopped)
System.Threading.Thread.Sleep(10);
currentPlayer.serviceAmt = pCardScanThread.Balance;
currentPlayer.gameRoom = pCardScanThread.GameRoom;
currentPlayer.cardNum = pCardScanThread.CardNum;
CashRegCardNumTextLabel.Text = currentPlayer.cardNum.ToString();
decimal dispBalance = currentPlayer.serviceAmt / CONVERSION_AMOUNT;
CashRegCardBalanceTxtLabel.Text = dispBalance.ToString("N2");
pCardScanThread.Resume();
CashRegCardBalanceLabel.Invalidate();
CashRegCardBalanceLabel.Update();
CashierRegisterPanel.Invalidate();
CashierRegisterPanel.Update();
This is being called inside of Wndproc also. I had to override it. So I call that, and then call the base Wndproc. When I step through the code in debug as soon as it hits Update it redraws the screen with the new values, but if I let it just run through automatically it will not redraw.
I think I figured out the problem. But now the solution is perplexing. I've figured out that Wndproc doesn't get called when the form is idle. So its not getting called until there is a button press or a mouse movement or mouse press. So if anyone can help me come up with a solution so that the Wndproc will be called constantly that would be great. Thanks.
Quote:Original post by benryves
I would not put that code inside WndProc; take a look at the Application.Idle event.
Wouldn't this just flip the situation and only call during the application idle event?
I put in that Application_Idle and its working nicely for the most part. I have multiple panels that I switch between in this program, but I only pull the value in a certain screen. SO when I switch screens and return it no longer goes. I'm going to check it out. Thanks again for your help.
Application.Idle only fires once, when the application becomes idle. Keep that in mind. Likely you're seeing it fired repeatedly because you are also forcing repaints with Invalidate().
A common method is to p/invoke PeekMessage and use it to run a "while app still idle" (e.g., "while no messages in the queue") loop in the Idle handler.
A common method is to p/invoke PeekMessage and use it to run a "while app still idle" (e.g., "while no messages in the queue") loop in the Idle handler.
Quote:Original post by jpetrie
Application.Idle only fires once, when the application becomes idle. Keep that in mind. Likely you're seeing it fired repeatedly because you are also forcing repaints with Invalidate().
A common method is to p/invoke PeekMessage and use it to run a "while app still idle" (e.g., "while no messages in the queue") loop in the Idle handler.
I'm sort of new to C#. Can you point me to a good tutorial on how to use this P/Invoke in order to get this to work. Thanks.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement