|
Hello All,
I'm having real memory issues trying to marshal and unpack an unmanaged structure in C#. Any help would be appreciated.
The structure is defined as:
typedef struct
{
WORD BaudGen;
WORD Timer0Reload;
BYTE Prescaler;
DWORD BaudRate;
} BAUD_CONFIG;
I tried a few ways to marshal and had the most success with:
[StructLayout(LayoutKind.Sequential)]
public struct BAUD_CONFIG
{
public System.UInt16 BaudGen;
public System.UInt16 Timer0Reload;
public Byte Prescaler;
public System.UInt32 BaudRate;
}
[DllImport("cp210xmanufacturing.dll")]
public static extern int CP210x_GetBaudRateConfig(
IntPtr cyHandle,
IntPtr baudConfigData);
I tried to access it with the following C# code:
int myStatus = 0;
UsbToCom.BAUD_CONFIG myBaudConfig = new UsbToCom.BAUD_CONFIG();
IntPtr SlHandle = new IntPtr();
IntPtr myBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(myBaudConfig));
Marshal.StructureToPtr(myBaudConfig, myBuffer, true);
myStatus = UsbToCom.CP210x_Open(0, ref SlHandle);
myStatus = UsbToCom.CP210x_GetBaudRateConfig(SlHandle, myBuffer);
myBaudConfig = (UsbToCom.BAUD_CONFIG)Marshal.PtrToStructure(myBuffer,
typeof(UsbToCom.BAUD_CONFIG));
Marshal.FreeHGlobal(myBuffer);
UsbToCom.CP210x_Close(SlHandle);
The problem is that the results are good but NUnit reports an MDA memory problem probably due to the way I'm marshalling.
Thanks in advance for any help!
|
|
|
|
|
ahfong wrote: The problem is that the results are good but NUnit reports an MDA memory problem probably due to the way I'm marshalling.
Never heard of that!
|
|
|
|
|
I have a Generic Queue<privateType> object, myGenericQueue.
My application receives events from another application. In my event handler, I asynchronously (and therefore on a different thread) enqueue data to myGenericQueue, using a delegate (myDelegate.beginInvoke).
I use a callback method to determine if the Enqueue operation succeeded. Both the asynch method and the callback produce debug output.
In my app's main(), a Form Timer periodically pings, at which time I LOCK myGenericQueue, enumerate and dequeue it. Again, more debug output.
To test for synchronization problems, I put a long sleep in my dequeue loop, so that myGenericQueue will be locked when my event handlers try to Enqueue data.
Here's what I'm seeing, that I don't understand: while myGenericQueue is theoretically LOCKed, my event handlers are still having no problems Enqueuing data. The callbacks fire right away, suggesting no delay, and all of the debug output matches order/timing expectations. All data is making it to myGenericQueue.
Is this expected? Why is there no collision with the lock?
The documentation for the Generic Queue object says:
A Queue<T> can support multiple readers concurrently, as long as the collection is not modified. Even so, enumerating through a collection is intrinsically not a thread-safe procedure. To guarantee thread safety during enumeration, you can lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.
Doesn't this suggest I should be seeing collisions?
Here's my dequeuing code:
lock (myGenericQueue)
{
while (myGenericQueue.Count > 0)
{
myList.Add((privateType)myGenericQueue.Dequeue());
System.Diagnostics.Debugger.Log(0, "", "Starting 5 sec sleep at: " + DateTime.Now.Ticks + Environment.NewLine);
System.Threading.Thread.Sleep(5000);
System.Diagnostics.Debugger.Log(0, "", "Coming out of 5 sec sleep at: " + DateTime.Now.Ticks + Environment.NewLine);
}
}
|
|
|
|
|
You only mention locking in one place. You have to lock it in both places. Basically, the lock is asking "can I aquire a lock?" and if it can it gets one. If it can't it blocks until it can get one.
|
|
|
|
|
How... odd. The documentation of "lock" at http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx[^] makes no mention at all about having to use lock on both sides. In fact, the example quite clearly shows the lock being applied in only one place, and the lock still preventing another thread from accessing that code...
|
|
|
|
|
JoeRip wrote: In fact, the example quite clearly shows the lock being applied in only one place,
That is because the one place is being used by multiple threads. It is being used on ALL (10) sides, in that example.
|
|
|
|
|
Yeah, I figured out my error in thinking. I was so used to using lock (myQueue.SyncRoot) that I had come to think of it as locking the Queue itself, not setting a lock flag on one of the queue's members.
Turns out all I needed to do for my Generic Queues - which I thought didn't have a SyncRoot member, since it didn't show up in VS 2008's Intellisense , was to use
lock (((ICollection)myGenericQueue).SyncRoot)
{
}
It was good to be reminded of what the lock statement was actually doing, though. Thanks!
|
|
|
|
|
No, that doesn't protect the queue. You still have to use a lock around every piece of code that uses the queue.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Agreed, I should have made that clear. I was just explaining why I thought the lock (object) call was actually protecting the object.
And, I was just noting that the lock (((ICollection)myGenericQueue).SyncRoot) statement gives me a nice shared object to lock that can be used by my library code and its callers. I was happy to find I could still do this with Generic Queues, the way I had with... uh, non-generic queues. Specific queues?
|
|
|
|
|
JoeRip wrote: Is this expected?
Yes.
JoeRip wrote: Why is there no collision with the lock?
Because you are not using the lock correctly. The lock doesn't protect the object that you are referring to in any way, the object is only used as an identifier. What the lock does is to prevent another code block locking with the same identifier to be entered (or the same code block to be reentered by another thread).
So, to protect your queue, you need a lock around every piece of code that uses the queue.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
|
JoeRip wrote: (I can't figure out how to show the angle braces).
Try reading the page you use to post the code into. There is a check box that says something about ignoring HTML or something.
led mike
|
|
|
|
|
I'm following the code here http://www.developerfusion.co.uk/show/4071/1/ to check Verizon email.
On this page the code is started http://www.developerfusion.co.uk/show/4071/7/. I set the server to 'incoming.verizon.net', and my username and password.
When I run it, I get the error "An existing connection was forcibly closed by the remote host".
I'm not sure what this means, or what I'm doing wrong. I checked the code, and it's all entered as exactly as in the article.
Anyone know what this message means?
Everything Makes Sense In Someones Mind
|
|
|
|
|
Just a tip...if you get an error, copy the error message and Google it. There are many chances that you will find the answer there itself without waiting for someone's comment here.
Like this[^]
KMAROIS wrote: I'm not sure what this means, or what I'm doing wrong. I checked the code, and it's all entered as exactly as in the article.
Well, in your case I believe either your username or password is incorrect.
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|
|
I am not sure what I am missing, pls point me out
public class X{
private MyPage mypage;
public MyPage MainPage { get { return mypage;} set { mypage;= value; } }
ArrayList listedApplication = new ArrayList();
listedApplication.Add("A");
listedApplication.Add("B");
listedApplication.Add("C");
}
I need to assign this Array list to a property
class MyPage
{
private ArrayList applicationList = new ArrayList();
public ArrayList ApplicationList { get { return applicationList; } set { applicationList = value; } }
}
I tried assigning using
MyPage.ApplicationList = listedApplication ;
I get a NullReferenceException, asking me to use new. i tried using
public class X{
private MyPage mypage;
public MyPage MainPage { get { return mypage;} set { mypage;= value; } }
ArrayList listedApplication = new ArrayList();
listedApplication.Add("A");
listedApplication.Add("B");
listedApplication.Add("C");
MyPage.ApplicationList.Add(new ArrayList(listedApplication)) ;
}
but still get the same error, pls help
|
|
|
|
|
There is an incorrect semicolon in your setters. I do not know if that is your problem however.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
|
|
|
|
|
maybe you need
public class X{
private MyPage mypage = new MyPage();
... ?
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
AnilUnni wrote: I tried assigning using
MyPage.ApplicationList = listedApplication ;
I get a NullReferenceException, asking me to use new.
You can't possibly have gotten a NullReferenceException with that code. It's a runtime error, and you can't run the code as it doesn't even compile.
MyPage is a class, and ApplicationList is not a static member of the class, so you can't access it that way.
AnilUnni wrote: MyPage.ApplicationList.Add(new ArrayList(listedApplication)) ;
Using "new ArrayList(listedApplication)" would only create a new ArrayList that is a copy of the first ArrayList, so that doesn't serve any purpose in your case. Adding the ArrayList to the ApplicationList property would create an ArrayList containing an ArrayList, which would work so far, but is surely not what you intended.
The problem is still that you are accessing the ApplicationList property as if it was a static member of the MyPage class.
Unless you are stuck with framework 1.x you should not use the ArrayList class at all. Use a type safe generic list instead, like List<string>.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
hi
i am working on a project that neede to use multi threading
in a method i use Monitor.Enter(Object) for define a critical area.
now if few thread wants to execute the method , monitor.Enter avoid to run Synchronize the method by all thread at the same time, so few threads will be on wait state.
now my problem is may be user wants to cancell the execution of a thread that is on wait state
how can i kill the thread.???
if i call Thread.CurrentThread..Interrupt() below error appears:
"cannot kill a thread that was in wait state" this is not complete error, this is just shortest of error.
thank you
nobody help you...
you have to help you yourself
and this is success way.
|
|
|
|
|
The thread that currently owns the lock should use some mechanism
to indicate a "cancelled" state and then release the lock.
After acquiring a lock, waiting threads should first look at the
cancelled state before proceeding.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
thanks alot mark but whats that mechanism?
i tray alot but cannot find a solution.
please tell me completely
nobody help you...
you have to help you yourself
and this is success way.
|
|
|
|
|
Add a boolean property to the class perhaps?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
You might try to use Monitor.TryEnter . This does not block, however you have to do this in a loop until you finally get the lock. This is called polling/busy waiting and can cost some CPU cycles, but maybe it works better in your case.
while(!Monitor.TryEnter(obj) || !shouldStop)
{
Thread.Sleep(1000);
}
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
I am not sure about the article but even MSDN says that you cannot take full control of a thread. the mostr you can do is to make it wait. I am not sure about article but I remember these things.
Hope it helps.
"If you had to identify, in one word, the reason why the human race has not achieved, and never will achieve, its full potential, that word would be 'meetings'." - Dave Barry
|
|
|
|
|
hi friends?
my question is just the title:
can we retrieve source code of an exe file that compiled with c#.net 2005????
i am working on a project that contain a several code lines that keep few top secure codes.
i affraid that someone can retrieve the code of my application by exe file and got the top secure code lines.
if this subject is possible, please tell me how it is happened? and if it is not impossible please tell me a solution for more safety in my work.
thank you.
nobody help you...
you have to help you yourself
and this is success way.
|
|
|
|