Got It! Many thanks Estys. Solved it. Really appreciate your help. Cheers Bruce :)
Bruce Coward
Posts
-
Hashtable with Structure or Class Object -
Hashtable with Structure or Class ObjectWoops! - Wrong thanks. Thanks Estys. Chers Bruce :thumbsup:
-
Hashtable with Structure or Class ObjectThanks Richard - that has fixed the records being the same on the hashtable. Can you see what I am doing wrong in getting the records off the hashtable? Many thanks, Bruce :thumbsup:
-
Hashtable with Structure or Class ObjectHi Richard, Thank you for your continuing patience... I have spent quite a time with that link yesterday and am happy with it's basic message. I appreciate that string is only a class but my problem comes when I try to build my small class and store it in the Hashtable. I can do that and by single stepping I can see my two data records be added to the hashtable. I notice that the hashtable seems to add both records but put the second records data over the first hash position as well as the second hash posotion but I haven't chased that problem yet. My real problem comes when I try to recover the records when I am not getting the data back. Here is my code:
namespace ConsoleApplication7
{
class Program
{
class csFPRec
{
public UInt32 uiPGN;
public UInt32 uiFrameCtr;
}static void Main(string\[\] args) { // Instantiate Class csFPRec FPRec = new csFPRec(); Hashtable HT = new Hashtable(); Console.WriteLine("Writing Initial Records"); Console.WriteLine("Writing 0x1234"); FPRec.uiPGN = 0x1234; FPRec.uiFrameCtr = 0x01; HT.Add(FPRec.uiPGN, FPRec); Console.WriteLine("Writing 0x5678"); FPRec.uiPGN = 0x5678; FPRec.uiFrameCtr = 0x02; HT.Add(FPRec.uiPGN, FPRec); Console.WriteLine(); Console.WriteLine("Reading Records"); // Get record back from Hashtable object pgnobj = new object(); pgnobj = HT\[0x1234\]; Console.ReadLine(); } }
}
As you can tell I am a newbie with C# having been writing embedded C for more than 20 years. Best regards
-
Hashtable with Structure or Class ObjectI looked at those examples but all I could see were bog standard string keys and single string data. This I have working. My problem is lifting the data up into a structure or class... Cheers Bruce
-
Hashtable with Structure or Class ObjectThat is what I thought was possible but I must be missing a trick because everyway I come at this I hit problems that I haven't solved yet. Can anyone point this comparative newbie in the right direction with a simple example based on the data in my first email please? Many thanks, Bruce
-
Hashtable with Structure or Class ObjectI am trying to use a hashtable (or equivalent) to store and recover a number of small structures or classes. The hashtable record will need to have a UInst32 key and an associated data structure or class that looks some thing like this: bool bRecUsedFlag; UInt32 uiPGN; UInt32 uiRecCtr; byte[] bPGNData; I can see odd references that indicate that this is possible but every attempt I have to code it is failing. Can anyone give me a small example of how to create this table, add records to it and get records from it please? Many thanks, Bruce :)
-
Array of Structs overwrite problemHi Luc, The circular array handling in point 0 and 3 is an old standard technique I have used for years that runs great but requires an unused buffer address. The second message in the code sample was just test code so I could see the problem without leaving the function. Point 4 - Only ever calling from 1 thread but your suggestion is well taken. I am sure you are on the right lines with point 2 but how do I change this to store real individual values in a real circular array? Thanks for your input. After 3 days it sure is appreciated. Cheers, Bruce :thumbsup:
-
Array of Structs overwrite problemI have been chasing this bug for three days now and wondered if anybody out there could point me in the right direction. I have a circular array generated from a structure. We are writing data to this array and found that the second write was overwriting part of the first record. To simplify debugging we have put some test code that changes what is to be written and then does a second write. Here is the code:
class CAN { // Members public struct CANTXSTRUCT { public int PGN; public byte DLC; public byte\[\] TxData; } private const int CANArraySize = 20; static private int CANFront = 1; static private int CANRear = 0; // Create static private circular array of CAN Tx messages static private CANTXSTRUCT\[\] arCANTx = new CANTXSTRUCT\[CANArraySize + 1\]; // Methods //\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* // static public void QueueCANTxMsg() // This method is called to place a CAN Tx message on the rear of the // arCANTx circular array. //\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* static public void QueueCANTxMsg(int PGN, byte DLC, byte\[\]TxData) { // Just warn and return if arCANTx message array is full if ((CANRear + 2) % CANArraySize == CANFront) { MessageBox.Show("CAN Tx Array Overflow, Press OK to continue", "Error Warning", MessageBoxButtons.OK); return; } // Modulus Increment rear pointer CANRear = (CANRear + 1) % CANArraySize; // Store data in array CAN.arCANTx\[CANRear\].PGN = PGN; CAN.arCANTx\[CANRear\].DLC = DLC; CAN.arCANTx\[CANRear\].TxData = TxData; // Test Code TxData\[0\] = 0xFF; CANRear = (CANRear + 1) % CANArraySize; // Store test data in array CAN.arCANTx\[CANRear\].PGN = PGN; CAN.arCANTx\[CANRear\].DLC = DLC; CAN.arCANTx\[CANRear\].TxData = TxData; // <---- Failing line } // End of QueueCANTxMsg()
When the failing line is executed it overwrites the first written CAN.arCANTx[CANRear].TxData value. The CANRear value appears to beworking correctly and I think the problem is associated with the
-
Writing to ListBox from multiple threads"The problems caused by making log() static are that log() itself will not be able to see the listbox." That makes sense. The compiler errors when the method is static could very well be caused by that. I will try Nick's suggestion and let you know. Many, many thanks Bruce
-
Writing to ListBox from multiple threadsThanks Henry. That is still throwing compile errors. I am beginning to suspect that the problem is because the two background threads are each in their own class file and that they cannot see the log(message) method in Form1 unless I make that method a static. Having made it a static the compiler is having trouble with pretty much every line of the method as follows: Error 2 An object reference is required for the non-static field, method, or property 'System.Windows.Forms.Control.Invoke(System.Delegate, params object[])' \\Server1\srvr\Code\PC Programs\SYSTest\SYSTest\Form1.cs 60 17 SYSTest Can anybody confirm whether I can make the log(message) a static or do I have to restructure my solution to put the background threads inside the Form1 class so I do not have to make the function into a static? Many thanks for all your valuable input, Bruce
-
Writing to ListBox from multiple threadsHi Nick, Thanks for your response. The thread will see Form1.log() if I make the log method a static. However if I make it a static the compiler kicks on just about every line of log() with "Object refernece required for ..." I just am not sure what it is looking for that I haven't provided. Cheers, Bruce
-
Writing to ListBox from multiple threadsI have a fairly standard Form1.cs with a listbox to display comms traffic. The comms traffic comes from two different interfaces each of which is handled by it's own background thread. I want to log the traffic in the listbox on Form1 and have set up a delegate / Invoke routine as below in the Form1 class:
//\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* // public void log(string message) // This function appends the message to the list box //\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* public delegate void LogDelegate(string s); public void log(string message) { if (InvokeRequired) { Invoke(new LogDelegate(log), new object\[\] { message }); } else { lb.Items.Add(message); lb.TopIndex = lb.Items.Count - 1; } } // End of void log()
When I try to call log(string) from either of the backgound threads the compiler isn't finding this function and I cant for the life of me see why not. I did try making it a static but that threw lots of errors so I took that away. What am I doing wrong please? Many thanks, Bruce :confused:
-
Hashtable key typeSo if I make it an int for example do I have to overide any hashing functions? In my ap the key would be a uint32 and the value an int. Cheers, Bruce :thumbsup:
-
Hashtable key typeDoes a C# hashtable key have to be a string or could it be an int? Cheers, Bruce :confused:
-
reading a checkbox from another threadMany thanks. Typical! me trying to overcomplicate things again. Cheers Bruce:thumbsup:
-
reading a checkbox from another threadI have spent some hours trying to read a checkbox enabled status on form1 from a different thread without any joy. Lots of research about Invoke etc has shown me how to write to the form but no clue as to how to read from the form. Can anyone here point me in the right direction please? Cheers, Bruce :confused:
-
Most efficient switch statement variable to use?When I am using switch statements in C# what is the most efficient type of switch variable to use? Normally I only have less than 10 cases which makes an Int32 seem rather overkill but am I correct in assuming that as the machine is running 32 bits that may be more efficient than trimming the switch variable down to 16 or even 8 bits which may cause more code steps? Cheers, Bruce :cool:
-
Error using an array of structs that contain an arrayThanks David, I am still thinking C rather than C# ! Setting it up as a class rather than a struct looks like it will fix my problems. Cheers, Bruce
-
Error using an array of structs that contain an arrayHi Luc, Thanks for your response. We have no requirement to "fix" any storage location but when incorporating the array within the struct in order to identify there are 8 bytes within the array the compiler wanted it to be "fixed". We just need a circular queued array of strucures that can be acted upon within this class and also added to from an external class. Cheers, Bruce