Jump to content
  • Advertisement
Sign in to follow this  
hammet1982

p/invoke, peekmessage help

This topic is 3823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Sorry if this is a dumb question, but I'm new to C#. I'm trying to use C# forms in order to make a program that will automatically read a card and display the card number on screen. I have it reading from a thread, and I created an idle event to fill in the information that my thread had pulled. My problem is that anytime I have multiple screens in this program that do different functions, and I only need it to fill in on one screen. However, when I change screens and return to the one that needs to pull the data it won't do it unless I click the screen or move the mouse. This is a bit frustrating since it works as I wanted it to in the beginning, and it will only cease working once I change screens. I wanted to use PeekMessage because it looked like the idle event stopped triggering, but I am not familiar enough with C# to know how it works. I'd appreciate someone's knowledge on the subject. Please help! Here is a sample of my code: public Form1() { Application.Idle += new EventHandler(Application_Idle); } public void Application_Idle(object sender, EventArgs e) { 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(); CashRegCardNumTextLabel.Invalidate(); CashRegCardNumTextLabel.Update(); CashRegCardBalanceLabel.Invalidate(); CashRegCardBalanceLabel.Update(); } [Edited by - hammet1982 on April 2, 2008 12:37:17 PM]

Share this post


Link to post
Share on other sites
Advertisement
Is there a reason why you don't just have the card scanning thread update the user interface whenever it gets more data? That will save you the trouble of trying to make the two threads synch to each other.

Each class that derives from control provides the InvokeRequired property and the Invoke method, which allows you to invoke any method on the interface thread, which gets you around those problems.

For example:


CardScanThread:

...
// got results
CashRegCardNumTextLabel.Invoke(SetText, new object[] { CardNum.ToString() });
...

void SetText(string text)
{
CashRegCardNumTextLabel.Text = text;
}



Share this post


Link to post
Share on other sites
Quote:
Original post by Mike.Popoloski
Is there a reason why you don't just have the card scanning thread update the user interface whenever it gets more data? That will save you the trouble of trying to make the two threads synch to each other.

Each class that derives from control provides the InvokeRequired property and the Invoke method, which allows you to invoke any method on the interface thread, which gets you around those problems.

For example:

*** Source Snippet Removed ***


Right now the way it works is the CardScanThread pulls the info from the card, and sets it to variables, and all I do inside the Idle is set the text label to the value the thread pulled and invalidate. I don't know why it wouldn't work like that. I tried setting the text directly in the thread, but it wouldn't let me modify the Form objects because it was read only.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!