|
I don't know if your particular application is subject to this but it is always wise to use stored procedures or at the very least parameterization of your query.
SELECT sum(price) as sumOfPrice FROM table WHERE city = @city;
A man said to the universe:
"Sir I exist!"
"However," replied the Universe, "The fact has not created in me A sense of obligation."
-- Stephen Crane
|
|
|
|
|
thanks for ur patient ,
I'm very ,
Crystal Report simply provide this .
s_mostafa_h
|
|
|
|
|
Hello Devs,
I'm developing a Chat application and I'm facing a problem. I have the following Method which is called from multiple threads. And when I start sending too much messages in speed (which invokes this method so fast) then client is disconnected. Let me show you the method and then continue my discussion:
public void SendMessage(TextMessage messageToSend)<br />
{<br />
try<br />
{<br />
lock (this.dataStream)<br />
{<br />
byte[] serializedBytes = messageToSend.GetBytes();
byte[] encMessageBytes = encryptorDecryptor.Encrypt(serializedBytes);
serializedBytes = null;<br />
int messageSize = encMessageBytes.Length;<br />
byte[] messageSizeBuffer = new byte[this.messageDataLengthBufferSize];<br />
messageSizeBuffer = BitConverter.GetBytes(messageSize);<br />
this.dataStream.Write(messageSizeBuffer, 0, this.messageDataLengthBufferSize);
this.dataStream.Write(encMessageBytes, 0, messageSize);
this.dataStream.Flush();<br />
messageSizeBuffer = null;<br />
encMessageBytes = null;<br />
}<br />
<br />
}<br />
catch (Exception ex)<br />
{<br />
string str = ex.Message + "\n" + ex.StackTrace;<br />
Disconnect();<br />
if (Disconnected != null)<br />
{<br />
Disconnected(this, EventArgs.Empty);<br />
}<br />
}<br />
}
In my opnion, the cause of this problem is multi threaded environment or it overflows data when i start sending to fast. for example if it is already working to send data on NetworkStream then a new call to this method is made which passes a new object of TextMessage to it which over writes the existing one so it is all messed up.
Note: Actual exception I get on the receiving end when length of data is not one which is supposed to be. See the code for Receiving:
int messageSize = BitConverter.ToInt32(messageDataLengthBuffer, 0);<br />
MemoryStream memstrm = new MemoryStream();<br />
<br />
readByteCount = 0;<br />
while (messageSize > 0)<br />
{<br />
readByteCount = dataStream.Read(this.messageDataBuffer, 0, messageDataBuffer.Length);<br />
if (readByteCount < 1)<br />
{<br />
Disconnect();<br />
return;<br />
}<br />
memstrm.Write(this.messageDataBuffer, 0, readByteCount);<br />
messageSize -= readByteCount;<br />
}<br />
memstrm.Position = 0;<br />
<br />
byte[] decryptedData = encryptorDecryptor.Decrypt(memstrm.ToArray());<br />
receivedMessage = new TextMessage(decryptedData);<br />
<br />
memstrm.Close();<br />
memstrm = null;
On the receving end I read 4 bytes as a size of incoming message then loop the piece of code to get all data untill complete message is received as of the message size. So when i start messages too fast then at the end of the loop "messageSize" usually gets in -ve which must never be less than 0 and when i try to get data decrypted again, it throughs exception "Length of data is invalid".
Is there any way that i can implement a mechanism that if SendMessage is doing its work then it should not be called again untill its done with its existing work? Or any other way to solve this issue?
Its really urgent. Any help will be really appreciated!!!
Best Regards
|
|
|
|
|
I assume that this.dataStream is the stream you are sending? If so better to create a queue of messages to send and then have a thread loop through the queue. Best part is that you will have a minimum of fuss.
private void AThreadedMethod(){
while(go){
if(queue.Count >0){
someObject = queue.Dequeue();
... do your stuff here
}
}
}
A man said to the universe:
"Sir I exist!"
"However," replied the Universe, "The fact has not created in me A sense of obligation."
-- Stephen Crane
|
|
|
|
|
Thanks for reply, Yes dataStream is an object of NetworkStream. I can understand the mechanism of Queing the messages but creating 100 threads for 50 Clients(50 on client end and 50 on server end) will seriously do a rape with CPU;P. One easiest way to solve this problem is to put Thread.Sleep(50); above each call of this method. Which will also not consume CPU but its cheap and will hit performance of application.
Best Regards,
|
|
|
|
|
And queue objects with message and connection details?
A man said to the universe:
"Sir I exist!"
"However," replied the Universe, "The fact has not created in me A sense of obligation."
-- Stephen Crane
|
|
|
|
|
Did not get it
|
|
|
|
|
Thread.Sleep(50) is VERY WRONG synchronization.
In fact it's no syncrhonization at all, it just decrease bug frequency, making them even more difficult to debug.
Beside you only need 2 thread and a queue.
1 reader thread (or num CPU/2)
1 writer thread (or num CPU/2)
and then a queue of
{
Stream dest;
byte[] data
}
|
|
|
|
|
Just to be sure, you're reading from and writing to a dataStream variable. Are those two different data streams in two different classes or are they the same stream?
Logifusion[^]
|
|
|
|
|
yes dataStream is an object of NetworkStream which is used for both reading and writing messages on the stream. Any solution?
Best Regards
|
|
|
|
|
The situation you have matches what you would use a ReaderWriterLock[^] for. A reader/writer lock allows only one writer at a time, but several readers at a time.
The only concern I would have after that is if you can only allow one reader at a time. In that case, I would stick with the lock you have, but apply the lock to readers as well. In the code you have above, it looks like you obtain a lock to write to the stream, but not to read. If a call to the read method happens in the middle of your write method, there's nothing in that read method checking if there's a lock or not. So it's entirely possible reads are still happening during your writes. You have to use the lock on both sides.
Logifusion[^]
|
|
|
|
|
First of all yes! I'm maintainging locks on both read and write that is not problem with my case. The problem in my case is when
A piece of code calls
SendMessage(TextMessage textMessage)
{
// Doing work here
}
then an other piece of code calls this Method with a new object of TextMessage
Send(TextMessage textMessage)
{
// Doing work here
}
the second class overwrite the previous object of TextMessage Class and then the some part of data written to the stream is from the last object and one from the new object.
Its different from the scenerio you are pointing that stream is misbehaved. Stream is already locked and that is safe the only thing is grabbled is TextMessage Objec that is passed to the method.
Do you think I'm right?
If so then please suggest something. I'm really stucked at this problem.
Best Regards,
Rizwan
|
|
|
|
|
There's not much information for me to go off of here. It looks like you have two different methods that write a TextMessage out to a stream. Are these methods in the same class or in different classes? Do they work with the same stream and obtain the same lock? Do you have two instances of the class that are talking to the same underlying network stream? I guess what I'm looking for here is if you have a singleton that has a monopoly over this network stream. Everything that goes to that stream therefore has to go through the same object (or through static methods of the same class). Then it's down to a syncronization issue on the methods reading and writing to that stream.
Logifusion[^]
|
|
|
|
|
Forget about all the things and focus only on this code:
public void SendMessage(TextMessage messageToSend)<br />
{<br />
try<br />
{<br />
lock (this.dataStream)<br />
{<br />
byte[] serializedBytes = messageToSend.GetBytes();
byte[] encMessageBytes = encryptorDecryptor.Encrypt(serializedBytes);
serializedBytes = null;<br />
int messageSize = encMessageBytes.Length;<br />
byte[] messageSizeBuffer = new byte[this.messageDataLengthBufferSize];<br />
messageSizeBuffer = BitConverter.GetBytes(messageSize);<br />
this.dataStream.Write(messageSizeBuffer, 0, this.messageDataLengthBufferSize);
this.dataStream.Write(encMessageBytes, 0, messageSize);
this.dataStream.Flush();<br />
messageSizeBuffer = null;<br />
encMessageBytes = null;<br />
}<br />
<br />
}<br />
catch<br />
{<br />
}
Now try to follow me:
Code bock 'A' Makes a call SendMessage(textMessage1);
and SendMessage is in the middle of its work and a Code Block 'B' also call this Method SendMessage(textMessage2);
the second call overwrite the textMessage1 with textMessage2 I think this is the problem May be I'm wrong I dont know what to do
Thanks for your help. If you have something more in mind please help me. I really need this so urgent.
Best Regards,
Rizwan
|
|
|
|
|
Well, you have a lock, which is fine. The lock will only let one thread through this code at a time. This is guaranteed. What's not guaranteed is what I'm asking about. This method is not static, so it must be an instance method. I assume that dataStream is also not static. My question is about where dataStream goes. Does it wrap around an existing stream that other objects can also wrap around? If you have a stream that is shared between two or more objects with this SendMessage method, then the lock is useless. That's why I'm asking if this is a singleton or not.
Logifusion[^]
|
|
|
|
|
Sorry guy to put you in problem. And thanks a lot for your help(Really appreciate it
I got the problem on receiving end. Please try to follow me:
lock (this.dataStream) // Lock the NetworkStream
{
byte[] serializedBytes = messageToSend.GetBytes(); // Get bytes from the Message object
byte[] encMessageBytes = encryptorDecryptor.Encrypt(serializedBytes); // Encrypt bytes
serializedBytes = null;
int messageSize = encMessageBytes.Length; // Know the Length of encrypted bytes
byte[] messageSizeBuffer = new byte[this.messageDataLengthBufferSize]; // Make a buffer of 4 bytes to store an integer value
messageSizeBuffer = BitConverter.GetBytes(messageSize); // Convert the messageSize into bytes
this.dataStream.Write(messageSizeBuffer, 0, this.messageDataLengthBufferSize); // Send the size of the message
this.dataStream.Write(encMessageBytes, 0, messageSize); // Send the actual message
this.dataStream.Flush();
messageSizeBuffer = null;
encMessageBytes = null;
what I'm doing in the above code is
1) I lock the NetworkStream
2) Convert the TextMessage's object into bytes (So that I can write them on NetworkStream)
3) Encrypt the Bytes
4) Take the Length of the encrypted bytes and store it into a 4 byte's array
5) Before I send the original message, I write the size of message as a 4 byte array.So that the receiver may know that incoming message includes XXX bytes and i have to read only this much data (not more than that).
But when I send messages too fast then the reading end reads data of more than one message (let me show you how)
Receiving End:
int messageSize = BitConverter.ToInt32(messageDataLengthBuffer, 0);
MemoryStream memstrm = new MemoryStream();
readByteCount = 0;
while (messageSize > 0)
{
readByteCount = dataStream.Read(this.messageDataBuffer, 0, messageDataBuffer.Length);
if (readByteCount < 1)
{
Disconnect();
return;
}
memstrm.Write(this.messageDataBuffer, 0, readByteCount);
messageSize -= readByteCount;
}
memstrm.Position = 0;
byte[] decryptedData = encryptorDecryptor.Decrypt(memstrm.ToArray());
receivedMessage = new TextMessage(decryptedData);
memstrm.Close();
memstrm = null;
1) I read 4 bytes from the Stream and Convert these 4 bytes to integer so I may know that incoming message has XXX number for bytes and I have to reaod only XXX bytes.
2) The Buffer I'm using is of 1024 which is filled up with this statement
readByteCount = dataStream.Read(this.messageDataBuffer, 0, messageDataBuffer.Length);
See problem exist at this statement i.e the dataStream inludes bytes of 3 messages on it which have to be read in 3 rounds but
I read all 3 messages with this statement because I'm reading 1024 bytes which is able to store all 3 messages.
Now suggest me a technique That i donot read more than the size of incoming message Assuming that a message can be of 5000 bytes for which I'm looping on read.
I hope, the problem is very clear now and you can definitely suggest a god solution. I'm also working on this, If i get the solution I'll let you know.
Best Regards,
Rizwan;)
|
|
|
|
|
Hmm, I see. I was assuming that since you wrote the size of the message into the stream:
this.dataStream.Write(messageSizeBuffer, 0, this.messageDataLengthBufferSize);
this.dataStream.Write(encMessageBytes, 0, messageSize);
That you were also reading the size of the message first and then reading the message. But you're saying you just blindly take in 1024 bytes at a time assuming that the message will be a multiple of that length. In your loop, you should take into account that your message will not be a multiple of 1024. Either look at your messageSize to figure out how much you can read (1024 bytes or less), or pad the messages you send to multiples of 1024 bytes.
Logifusion[^]
|
|
|
|
|
Yes Exactly, I already solved this problem a few hours back but thanks alot for being so concerned from heart to help me.
What I did is:
if (messageSize > messageDataBuffer.Length)<br />
readByteCount = dataStream.Read(messageDataBuffer, 0, messageDataBuffer.Length);<br />
else<br />
readByteCount = dataStream.Read(messageDataBuffer, 0, messageSize);
All is working perfect now
Thanks alot again!!!
Please add me in you Msn buddies if you feel comfortable
rizwansharp@hotmail.com
Best Regards,
Rizwan Ahmed
|
|
|
|
|
Hello,
I'm interested in how to get some special Windows colors.
I'm developing a small control (derived from System.Windows.Forms.CheckBox), where I need to draw my own look. Basically, it is in a look like a flat checkbox button with some additional functionality.
I tried using SystemColors and so on, but I'm getting not the same colors like the original control. I need colors like the one for shadowed or highlighted parts.
I have tried SystemColors.ControlDark and SystemColors.ControlDarkDark but I'm getting the same color. The original checkbox uses some different colors. Have checked this with some screen shots. I'm using w2k with .net 2.0 and all color theme setting have been set back to the original one by MS!
Does anyone know a solution, or does anyone have some tips?
Best regards,
NPow
|
|
|
|
|
One class you might want to look at is the System.Windows.Forms.ControlPaint class.
But your best bet is to look at the theme API. This is what you'll need for true look-alike controls. The Windows theme API is exposed within the System.Windows.Forms.VisualStyles namespace in .NET v2.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Messianic Instrumentals (with audio)
The apostle Paul, modernly speaking: Epistles of Paul
Judah Himango
|
|
|
|
|
nt
A man said to the universe:
"Sir I exist!"
"However," replied the Universe, "The fact has not created in me A sense of obligation."
-- Stephen Crane
|
|
|
|
|
Thanks for the fast replay!
Well... System.Windows.Forms.VisualStyles is not a choice for me at the moment; The OS does not support them... I'm working on W2k and my control should work here too.
Judah Himango wrote: One class you might want to look at is the System.Windows.Forms.ControlPaint class.
I'm very skeptical about it. At the framework reference, they say for example on the Dark() method:
...
If the specified Color is one of the SystemColors, the color is converted to a SystemColors.ControlDark color; otherwise, the color's luminosity value is decreased.
...
So that is the problem, SystemColors.ControlDark or SystemColors.ControlDarkDark do not return a color as the ones that the original control use.
Well, I will have to give it a try... tomorrow.
Regards,
NPow
|
|
|
|
|
NPow wrote: Well... System.Windows.Forms.VisualStyles is not a choice for me at the moment; The OS does not support them... I'm working on W2k and my control should work here too.
Windows 2000 doesn't support VisualStyles? The MSDN docs on VisualStyleRenderer says it's supported on 2k.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Goof around music jam with my brothers (with video)
The apostle Paul, modernly speaking: Epistles of Paul
Judah Himango
|
|
|
|
|
Judah Himango wrote: Windows 2000 doesn't support VisualStyles? The MSDN docs on VisualStyleRenderer says it's supported on 2k.
Where do you found that statement?
Direct from the MSDN 2, there stay:
[...]The VisualStyleRenderer class wraps the functionality of the visual styles (UxTheme) API from the Windows Shell portion of the Windows Platform SDK. For more information about visual styles, see "Using Windows XP Visual Styles" in the Platform SDK portion of the MSDN Library at http://msdn.microsoft.com/library.
Windows XP Home Edition, Windows XP Professional x64 Edition, Windows Server 2003 Platform Note: Visual styles are supported only on these platforms.
[...]
All tests I have maid about it's support, deliver a false value or raised an InvalidOperation Exception!
Regards,
NPow
-- modified at 10:10 Thursday 20th July, 2006
|
|
|
|
|
Interesting -- the page you cite (http://msdn2.microsoft.com/en-us/library/system.windows.forms.visualstyles.visualstylerenderer.aspx) shows supported platforms as all of them, yet the documentation text says it's not supported on Win2k.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Messianic Instrumentals (with audio)
The apostle Paul, modernly speaking: Epistles of Paul
Judah Himango
|
|
|
|
|