|
Are you looking at this in the debugger? Which line throws the exception?
If you can't look at it in the debugger for some reason, wrap each line in a try/catch and figure out which line is throwing the exception.
|
|
|
|
|
Thanks for reply
JoeRip wrote: Which line throws the exception?
this line :
XElement newXmlNode = (XElement)Clipboard.GetData("XElement");
|
|
|
|
|
In the following struct:
public struct myStruct
{
public static int CumulativeCountOfInstances;
public myStruct(int SomeUnusedData)
{
CumulativeCountOfInstances++;
}
}
If concurrent processes are creating myStruct structs, what prevents collisions as each instance tries to increment CumulativeCountOfInstances ?
|
|
|
|
|
Nothing prevents it.
Maybe a good place for an interlocked increment
How are different processes sharing the same variable?
Or did you mean concurrent threads?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I definitely mean concurrent threads. Friday afternoons are very hard on my concentration.
So I should use Interlocked increments from my constructor, AND I should should use Interlocked exchange any time I want to read this value?
Dang, static isn't as static as I thought it was
|
|
|
|
|
If you use Interlocked.Increment() / Interlocked.Decrement()
then the new value will be returned (an increment and a read
are combined in one atomic operation).
Reading (for integer types) is atomic (except for 64-bit integers
on a 32-bit processor) so there's no Interlocked.Read except for
longs.
You could use Interlocked.Exchange() if that's what you need to do,
but it's not appropriate for just reading the value (what value would
you exchange?).
To write/modify the variable beyond the atomic operations provided by the
Interlocked class, you'll need to use some other type critical section,
like lock.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks again. I wondered why there was no Read for Int32.
|
|
|
|
|
if I do this:
Queue SynchedMessageQueue = Queue.Synchronized(new Queue());
then when I access SynchedMessageQueue, do I still need to use LOCK statements?
Or is that now being handled automatically?
|
|
|
|
|
I believe that this answers the question (from MSDN Queue.Synchronized Method[^]):
To guarantee the thread safety of the Queue, all operations must be done through this wrapper only.
Enumerating through a collection is intrinsically not a thread-safe procedure.
Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception.
To guarantee thread safety during enumeration,
you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
Mika
|
|
|
|
|
Thanks. I had read this, but for some reason I cannot now remember, I questioned it. Seems pretty straightforward now!
|
|
|
|
|
You're welcome.
Mika
P.s. Really glad to hear that you thought about this instead if just asking
|
|
|
|
|
I think what I blew off was that SPEFICICALLY, enumeration wasn't thread safe. In my mind I thought "well, if getting things from a synchronized queue isn't thread safe, what's the point?"
My brain didn't translate the part where "Only the enumeration itself needs LOCK, the rest of the Queue methods do not."
|
|
|
|
|
Hi, everyone...
I have a stream and put in a GZipStream, save in SQL 2005, after that I load this stream from sql 2005 and try to deserialize from BinaryFormatter. This will be the GOLD, for now a simple serializable-Compress-Decompress-Deserializable is very fine...
But, I´m getting a error:
"Binary stream '0' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization."
Any help?
My Code:
public static void Main()
{
List<string> p3;
List<string> p1 = new List<string>();
p1.Add("Brazil");
p1.Add("Error");
p1.Add("Serializable");
Stream Serial = new MemoryStream();
FileStream fileStream = new FileStream("Teste.bin", FileMode.Create, FileAccess.ReadWrite, FileShare.None);
Processo _Retorno = new Processo();
BinaryFormatter teste = new BinaryFormatter();
teste.Serialize(Serial, p1);
byte[] buffer = new byte[Serial.Length];
Serial.Read(buffer, 0, buffer.Length);
fileStream.Write(buffer, 0, buffer.Length);
fileStream.Close();
byte[] Compremido = Compressor.Compress(buffer);
byte[] Descompremido = Compressor.Decompress(Compremido);
MemoryStream msUnZip = new MemoryStream();
msUnZip.Write(Descompremido, 0, Descompremido.Length);
msUnZip.Seek(0, SeekOrigin.Begin);
BinaryFormatter Serializador = new BinaryFormatter();
p3 = (List<string>)Serializador.Deserialize(msUnZip);:mad:
}
</string></string></string></string>
public static class Compressor
{
public static byte[] Compress(byte[] data)
{
MemoryStream output = new MemoryStream();
GZipStream gzip = new GZipStream(output,
CompressionMode.Compress, true);
gzip.Write(data, 0, data.Length);
gzip.Close();
return output.ToArray();
}
public static byte[] Decompress(byte[] data)
{
MemoryStream input = new MemoryStream();
input.Write(data, 0, data.Length);
input.Position = 0;
GZipStream gzip = new GZipStream(input,
CompressionMode.Decompress, true);
MemoryStream output = new MemoryStream();
byte[] buff = new byte[64];
int read = -1;
read = gzip.Read(buff, 0, buff.Length);
while (read > 0)
{
output.Write(buff, 0, read);
read = gzip.Read(buff, 0, buff.Length);
}
gzip.Close();
return output.ToArray();
}
} Font: http://www.codeproject.com/KB/viewstate/ViewStateCompression.aspx[^]
|
|
|
|
|
JUST for awnser this...
It´s necessary to put
Serial.Seek(0, SeekOrigin.Begin);
Between:
byte[] buffer = new byte[Serial.Length];
Serial.Read(buffer, 0, buffer.Length);
|
|
|
|
|
I have a piece of javascript code that writes to a local file. When this happens, how can I notify a running C# WinForm application so C# app can process the file.
Thanks.
|
|
|
|
|
You could always use a FileSystemWatcher to keep an eye on this file.
|
|
|
|
|
I'm trying to change the system date and time on XP Pro:
In the form's class header I have:
[DllImport("kernel32.dll", SetLastError=true)]
public static extern bool SetSystemTime( [In] ref structSystemTime st );
public struct structSystemTime
{
public short wYear;
public short wMonth;
public short wDayOfWeek;
public short wDay;
public short wHour;
public short wMinute;
public short wSecond;
public short wMilliseconds;
}
Then in Form_Load I have:
structSystemTime st = new structSystemTime ();
st.wYear = 2003;
st.wMonth = 5;
st.wDay = 22;
st.wHour = 22;
st.wMinute = 15;
st.wSecond = 08;
SetSystemTime(ref st);
It compiles and runs, but the date/time doesn't change. Anyone know what's wrong?
Everything Makes Sense In Someones Mind
|
|
|
|
|
KMAROIS wrote: It compiles and runs, but the date/time doesn't change. Anyone know what's wrong?
Change the definitions of the fields from short to ushort (unsigned short).
|
|
|
|
|
If fixing the structure doesn't help, does
GetLastWin32Error() return a useful error code?
if (!SetSystemTime(ref st))
{
int errcode = Marshal.GetLastWin32Error();
}
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I changed the struct and it did not work. It returned 1314, which is:
if (!SetSystemTime(ref st))
{
int errcode = Marshal.GetLastWin32Error();
string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;
MessageBox.Show(errorMessage);
}
Turns out the Sys Admin has restricted users from chaning the date/time.
Now to go find the sys admin....
Everything Makes Sense In Someones Mind
|
|
|
|
|
There is usually a good reason, good luck with the Admin, take your flame proof suit!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Whenever I use WinAPI, the equivelant of any integer (short or int) in API documentation, I always have to use Int32.
Regards,
Thomas Stockwell
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Visit my Blog
|
|
|
|
|
I have the same problem but the marshal tells me that error 57
Have you managed to solve it?
|
|
|
|
|
How can i mask "Internet IP Address" with "C#"
|
|
|
|
|
maliaslam wrote: How can i mask "Internet IP Address" with "C#"
Before you get an answer to this question, you're going to have to do some serious convincing that this is for a legitimate purpose. Why do you need to do this?
|
|
|
|