p/invoke, peekmessage help

Started by
1 comment, last by hammet1982 16 years ago
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]
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 resultsCashRegCardNumTextLabel.Invoke(SetText, new object[] { CardNum.ToString() });...void SetText(string text){    CashRegCardNumTextLabel.Text = text;}
Mike Popoloski | Journal | SlimDX
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.

This topic is closed to new replies.

Advertisement