|
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
|
|
|
|
|
i make windown application with c#
http://www.shoppinp.com/11.zip
i my pc run without probelm
but when i instul .Net 2.0 in other machin
i see this error
http://www.shoppinp.com/untitled.BMP
Palestine
|
|
|
|
|
TAREQ F ABUZUHRI wrote: http://www.shoppinp.com/11.zip
It runs fine on my machine.
You should try logging errors in the application to a log file. Use try/catch to catch errors. Also, in your Program.Main method, add a Application.ThreadException handler; this way you'll get forwarded the errors that occur in your program. You can then log them and see what's going on.
Another possibility is debugging. If you can attach a debugger to the the process that's erroring out on the other machines, you should be able to get a better idea what's going wrong.
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
|
|
|
|
|
Hi,
We are parsing csv file to XML document. It follows a structure. One part has changed.
<code>
<PRODUCTS>
<PRODUCT INVOICE="2" PRODID="1 No Lead" QTY="3000" />
<PRODUCT INVOICE="2" PRODID="3 Super" QTY="1000" />
<PRODUCT INVOICE="2" PRODID="4 Diesel" QTY="1000" />
</PRODUCTS>
</code>
In this case we need to add a
<PRODUCT INVOICE="2" PRODID="2 Plus" QTY="0" />
If a product doesn't exist need to add that name with qty 0. How would we go about doing this? We are adding this to PRODUCT node.
Thanks.
-- modified at 12:59 Wednesday 19th July, 2006
|
|
|
|
|
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
XmlNode productsNode = xmlDoc.ChildNodes[0].ChildNodes[0];
XmlElement productElem = xmlDoc.CreateElement("PRODUCT");
XmlAttribute invAttr = xmlDoc.CreateAttribute("INVOICE");
invAttr.Value = "2";
XmlAttribute prodidAttr = xmlDoc.CreateAttribute("PRODID");
prodidAttr.Value = "2";
XmlAttribute qtyAttr = xmlDoc.CreateAttribute("QTY");
qtyAttr.Value = "0";
productElem.Attributes.Append(invAttr);
productElem.Attributes.Append(prodidAttr);
productElem.Attributes.Append(qtyAttr);
productsNode.AppendChild(productElem);
|
|
|
|
|
Thanks for the reply.
Reading the values from a csv file, which has rows for the different product/fuel types. Sometimes there are 3 rows for Lead, Plus, super. so, want to add another line in XML document for diesel, with quantity 0. that way, there are alwasy 4 rows in the XML document.
|
|
|
|
|
Consider adding all four with value zero to each products node, then updating with values from the CSV file...
|
|
|
|
|
Hi all!
I need some solution, to do a "low level" logging for my application. In teh log, I want to see everything like function calls, strak trace, button events, textbox events.... like the profilers do.
Until now, I write my own loging system, and send messages to the logfile by hand like this:
Log.LoggThis("Something went ok",LogType.Message); or Log.LogThis("some error occured",exception,LogType.Error);
But I think is there a better vay to do this vithout hand coding in every method, and event handler....
If anybody have some idea, please wrote it to me.
Thanks, and sorry for my bad english.
|
|
|
|
|
|
Thank's I alredy know about NLog, and it's a good thing, but it's do the same, like my own loging engine.
Nlog write to file, what you want, but you must tell it, what, where, and when. So it isn't automatic, and still ned to write the logging messages into the code.
My solution has 3 projects, and a lot of forms, and classes witch have a tonn of functions, and events, so I don't want to write logging procedure for each method...
If you ever yuse some profiler, like Ants, or ReSharper yo see te way I like. These pograms need only the target application executable fle, and they listen into the app code, and tell you what happend in where, ad the target application don't need to modyfi.
-- modified at 16:59 Wednesday 19th July, 2006
|
|
|
|
|
I think you can solve this issue by using .NET Reflection. Take a look at this article (TraceListeners and Reflection)[^] - it contains enough to get you started.
In the example, the author of the article shows how to log information such as the calling function on the stack and the argument list of that function. He also explains how you retrieve information about the call stack.
Hope that helps!
Sincerely,
Alexander Wiseman
|
|
|
|
|
Tank's for the answer!
I try to use the refered article, to work out the correct solution.
-- modified at 3:37 Thursday 20th July, 2006
I checked the article, its good, but isn't do what I want. This type of logging stil need to be modyfy the target program.
I need solution to log a program/process without modifying it's code...
|
|
|
|
|
My progress bar in the status bar doesn't want to run.
The application I created is a SQL tool for an oracle db I work on. Basically, I just type my SQL code in a RichTextbox and then the data comes back in my datagrid. That works fine.
I thought it would be cool if I could add a progress bar and set it to marquee while I was waiting for my data to come back.
I'm pretty sure I need to multi-thread this beasty but when I do I get the following message: "Cross-thread operation not valid: Control 'rtbSQL' accessed from a thread other than the thread it was created on."
Here's what I passed to the new thread (which is created when I click the run button):
btnRunSQLClick(bla bla...)
{
Thread t1 = new Thread(new ThreadStart(GetData));
t1.Start();
}
the GetData() code:
public void GetData()
{
try
{
da=new OdbcDataAdapter(this.rtbSQL.Text, this.cnn);
cb=new OdbcCommandBuilder(da);
dt=new DataTable();
da.Fill(dt);
}
catch(OdbcException ex)
{
MessageBox.Show(ex.Message);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
this.dataGrid1.DataSource = dt;
}
Any ideas? I'm definitely a novice at threading!!
|
|
|
|
|
If you are using 2.0 you could use BackgroundWorker and subscribe to the OnProgressChanged event to update your progressbar.
Otherwise you need to use an event to update your progressbar in the UI thread or use invoke on the progressbar to update it.
Take a look on this site for some good examples of multi-threading
|
|
|
|
|
The problem is the following line: da=new OdbcDataAdapter(this.rtbSQL.Text, this.cnn); . As the error message says accessing a control a thread other than the thread it was created on isn't allowed.
You should retrieve the value of the this.rtbSQL.Text property before starting the worker thread and then pass it to the method. Therefor you could either use this overload of the Thread.Start method[^] or follow techniques presented in this MSDN article[^].
"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." - Rick Cook
www.troschuetz.de
|
|
|
|
|