Search Results

Search found 4 results on 1 pages for 'caldoncze'.

Page 1/1 | 1 

  • C# - calling ext. DLL function containing Delphi "variant record" parameter

    - by CaldonCZE
    Hello, In external (Delphi-created) DLL I've got the following function that I need to call from C# application. function ReadMsg(handle: longword; var Msg: TRxMsg): longword; stdcall; external 'MyDll.dll' name 'ReadMsg'; The "TRxMsg" type is variant record, defined as follows: TRxMsg = record case TypeMsg: byte of 1: (accept, mask: longword); 2: (SN: string[6]); 3: (rx_rate, tx_rate: word); 4: (rx_status, tx_status, ctl0, ctl1, rflg: byte); end; In order to call the function from C#, I declared auxiliary structure "my9Bytes" containing array of bytes and defined that it should be marshalled as 9 bytes long array (which is exactly the size of the Delphi record). private struct my9Bytes { [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.U1, SizeConst = 9)] public byte[] data; } Then I declared the imported "ReadMsg" function, using the "my9bytes" struct. [DllImport("MyDll.dll")] private static extern uint ReadMsg(uint handle, ref my9Bytes myMsg); I can call the function with no problem... Then I need to create structure corresponding to the original "TRxMsg" variant record and convert my auxiliary "myMsg" array into this structure. I don't know any C# equivalent of Delphi variant array, so I used inheritance and created the following classes. public abstract class TRxMsg { public byte typeMsg; } public class TRxMsgAcceptMask:TRxMsg { public uint accept, mask; //... } public class TRxMsgSN:TRxMsg { public string SN; //... } public class TRxMsgMRate:TRxMsg { public ushort rx_rate, tx_rate; //... } public class TRxMsgStatus:TRxMsg { public byte rx_status, tx_status, ctl0, ctl1, rflg; //... } Finally I create the appropriate object and initialize it with values manually converted from "myMsg" array (I used BitConverter for this). This does work fine, this solution seems to me a little too complicated, and that it should be possible to do this somehow more directly, without the auxiliary "my9bytes" structures or the inheritance and manual converting of individual values. So I'd like to ask you for a suggestions for the best way to do this. Thanks a lot!

    Read the article

  • C# TabControl - is it possible to "disable" individual TabPages?

    - by CaldonCZE
    Is it somehow possible to disable one (or more) tabs of tab control? At some point I need to make user stay on the active tab and prevent him from leaving... I know I can disable the whole TabControl component, but that disables also all components on active tab... I also tried to use the Selecting method of TabControl: private void TabControl_Selecting(object sender, TabControlCancelEventArgs e) { e.Cancel = PreventTabSwitch; } This works, prevents user from switching (if PreventTabSwitch==true), but since all tabs look active and just don't react it's confusing... There is no Enabled property for individual tab pages, so I don't know what else to do... Thanks a lot for in advance for all tips.

    Read the article

  • Delphi - Is there any equivalent to C# lock?

    - by CaldonCZE
    I'm writing a multi-threaded application in Delphi and need to use something to protect shared resources. In C# I'd use the "lock" keyword: private someMethod() { lock(mySharedObj) { //...do something with mySharedObj } } In Delphi I couldn't find anything similar, I found just TThread.Synchronize(someMethod) method, which prevents potential conflicts by calling someMethod in main VCL thread, but it isn't exactly what I want to do.... Edit: I'm using Delphi 6

    Read the article

  • C# - periodic data reading and Thread.Sleep()

    - by CaldonCZE
    Hello, my C# application reads data from special USB device. The data are read as so-called "messages", each of them having 24 bytes. The amount of messages that must be read per second may differ (maximal frequency is quite high, about 700 messages per second), but the application must read them all. The only way to read the messages is by calling function "ReadMessage", that returns one message read from the device. The function is from external DLL and I cannot modify it. My solution: I've got a seperate thread, that is running all the time during the program run and it's only job is to read the messages in cycle. The received messages are then processed in main application thread. The function executed in the "reading thread" is the following: private void ReadingThreadFunction() { int cycleCount; try { while (this.keepReceivingMessages) { cycleCount++; TRxMsg receivedMessage; ReadMessage(devHandle, out receivedMessage); //...do something with the message... } } catch { //... catch exception if reading failed... } } This solution works fine and all messages are correctly received. However, the application consumes too much resources, the CPU of my computer runs at more than 80%. Therefore I'd like to reduce it. Thanks to the "cycleCount" variable I know that the "cycling speed" of the thread is about 40 000 cycles per second. This is unnecessarily too much, since I need to receive maximum 700 messagges/sec. (and the device has buffer for about 100 messages, so the cycle speed can be even a little lower) I tried to reduce the cycle speed by suspending the thread for 1 ms by Thread.Sleep(1); command. Of course, this didn't work and the cycle speed became about 70 cycles/second which was not enough to read all messages. I know that this attempt was silly, that putting the thread to sleep and then waking him up takes much longer than 1 ms. However, I don't know what else to do: Is there some other way how to slow the thread execution down (to reduce CPU consumption) other than Thread.Sleep? Or am I completely wrong and should I use something different for this task instead of Thread, maybe Threading.Timer or ThreadPool? Thanks a lot in advance for all suggestions. This is my first question here and I'm a beginner at using threads, so please excuse me if it's not clear enough.

    Read the article

1