|
Benjamin.Buhr wrote: I think the other problem ist that I'm using UserControl in WPF. A UserControl is not a Window, and cannot be topmost; it needs to be hosted in a Window.
Then again, it would not be very friendly to have a usercontrol modify its parents' behaviour.
In WinForms (I don't do WPF) there's an event that is fired on the form as soon as the handle is created; it is fired quickly after the constructor. That's where you could initialize your UC.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
hi,
i have Windows-Mobile program that work with weight Connecting through rs232.
its work excellent - If the program works in a period of time, it starts to slow
down and even get stuck at some point.
my code:
Hide Expand Copy Code
port = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
port.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(Recepcion);
private SerialPort port;
StringBuilder SB;
private void Recepcion(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
try
{
SB = new StringBuilder(1000);
Application.DoEvents();
System.Threading.Thread.Sleep(122);
SB.Append(port.ReadExisting());
port.DiscardInBuffer();
this.Invoke(new EventHandler(Actualizar));
}
catch { }
}
string MOMO1, MOMO2;
string[] WI;
string ALL;
private void Actualizar(object s, EventArgs e)
{
ALL = SB.ToString().Trim();
WI = ALL.Split(',');
ALL = WI[2].ToString().Trim();
MOMO1 = ALL.Replace("+", "").Replace("g", "").Replace("ST", "").Replace("GS", "").Replace("US", "");
if (MOMO1 != "")
{
MOMO2 = MOMO1;
}
lblMSG.Font = new Font("Ariel", 48, FontStyle.Bold);
lblMSG.Text = MOMO2;
Check_Weight();
GC.Collect();
}
Can anyone advise me why and how to solve it ?
|
|
|
|
|
For starters, you should never have to call in the garbage collector yourself: it only needs to be used when you start to run out of memory.
Second, that's really rather nasty code. The DataReceived event is started on a non-UI thread, as you know (or you wouldn't be Invoking the UI), but you are changing / using the value of SB on two threads, with no checking or locking to make sure that there isn't a problem. And you Invoke the UI thread even if there is no data to play with - and DataReceived can be fired for each character received, even if you have already removed them from the buffer.
You are also creating a new Font object (which is identical to the last) potentially for each character you receive, without any attempt to dispose of them. The GC will not do that, so Graphics Handles are being used up at a frightening rate - and these do not trigger the GC to do anything.
And there is a spurious ToString in there: Split returns an array of strings, so converting its constituent parts to a string is unnecessary.
Personally?
I'd have a background task processing the data with locking to prevent problems, and only invoke the bits which update the actual UI elements. I'd also use the same Font object for the label, and not change it at all.
And Application.DoEvents is a definite no-no!
I'd probably not use Split at all - it's not particularly efficient since it creates a lot of memory you aren't using. I'd look at IndexOf and SubString instead to "pull out" just the bit you are interested in. Even if I did use Split, you really, really need to check the number of array elements it returns: The DataReceived event doesn't wait for the end of your message before it "kicks in"...
[edit]Typos[/edit]
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
modified 22-Sep-15 5:06am.
|
|
|
|
|
Many thanks for the detailed answer !
If this is not hard, I'd be happy if you can change things problematic in this code.
|
|
|
|
|
OG has given you a detailed answer of the issues in your code, it shouldn't be too much trouble for you to convert it based off his reply. You might want to do this for yourself.
|
|
|
|
|
Just to add to what Pete said, I couldn't if I wanted to: I don't have access to the equipment you are connecting to, or any idea what the data looks like, or what's important about it - so I couldn't test any changes I did make!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
thanks for the help,
i change my code to:
private SerialPort port;
StringBuilder SB;
private void Recepcion(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
try
{
SB = new StringBuilder(1000);
SB.Append(port.ReadLine());
port.DiscardInBuffer();
this.Invoke(new EventHandler(Actualizar));
}
catch { }
}
but still same problem..... my program fonts change alone for no reason.....
|
|
|
|
|
All you have done it take out the DoEvents and the Thread.Sleep.
You have done nothing about the problems I told you about.
And you are surprised that your code has the same fault?
Are you just guessing here, or have you designed this properly?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Maybe I'm not an expert like you,
I do my best.
Right now I have a problem and I'm really trying to solve it in all ways
|
|
|
|
|
I have around 100 Virtual Machines open in my Remote Desktop Connection Manager and I want to perform a same operation on all those machines. Can I do this using some code?
|
|
|
|
|
No idea. You haven't told us what operation you want to perform.
|
|
|
|
|
I have a Windows Desktop application opened on all the machines and I want to mark/unmark a checkbox.
|
|
|
|
|
I am compiling the c# code at run time using "Reflection" and trying to get the methods names. I am able to get the user defined methods and also predefined methods. But I want get the only the user defined methods.
My code :
Assembly assembly = results.CompiledAssembly;
var names = (from type in assembly.GetTypes()
from method in type.GetMethods(
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.Static)
select method.Name).Distinct().ToList();
Can any one help me.
Thanks in Advance.
|
|
|
|
|
If you don't want the "inherited" methods in there, then lookup its baseclass, find out which methods it has, and subtract those from the list you originally got.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Sounds good to me!
Might need to extract any interface methods as well, though.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Thanks for you reply.
I have solved the my problem by using below solution
solution
**************
.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
|
|
|
|
|
Try using 'GetMethod like this:
.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
"Inherited members are not considered"
Nice
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Thanks BillWoodruff.
I have solved the problem with your suggestion.
Solution
****************
.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
|
|
|
|
|
Write an algorithm to add and multiply two large integers, which cannot be represented by built-in types
need the solution for this..
|
|
|
|
|
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.
Try it yourself, you may find it is not as difficult as you think!
If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
Hint: there is a framework structure which handles arbitrarily large integers: BigInteger[^] - you might want to see what that provides...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
update: examining the value of the MemoryStream's 'Position property after the decompressed GZipStream was put into it with 'CopyTo showed it to be set to the length of the decompressed GZipStream: setting the MemoryStream's 'Position property to #0 resulted in being able to correctly call Serializer.ReadObject on the MemoryStream and create the desired outcome.
Interesting that in all the searching/reading I did here and on StackOverFlow, I came across no mention of this. Time for a Tip/Trick ?
~
Serializing a class-instance/object using WCF [DataContract] and [DataMember] Attributes is very easy; compressing it with the GZip library, and saving the compressed stream to a file is straightforward: no problems.
Currently my working code for de-serialization from GZip file does the following:
1. open the saved .gz file in the standard way
2. create a new file to hold the un-gzipped .xml whose name is the same as the GZip file, with the .gz extension replaced with .xml.
// compressedstuff.gz => compressedstuff.xml
3. create a new GZip compression stream with Decompress option
4. copy the Gzip compression stream into the new file
5. re-create the "object" in the standard way from file using WCF Serializer What I want to do is go from the uncompressed GZip file directly to using the WCF Serializer to "resurrect" the object without creating a new file. This code dies with the error "Unexpected end of file:"
using System.Runtime.Serialization;
using System.IO;
using System.IO.Compression;
public static SomeClass GZToObject(string gzFilePath)
{
DataContractSerializer serializer = new DataContractSerializer(typeof(SomeClass));
SomeClass someClassInstance = null;
FileInfo fileToDecompress = new FileInfo(gzFilePath);
using (MemoryStream xmlStream = new MemoryStream())
{
using (GZipStream deCompStream = new GZipStream(File.Open(gzFilePath, FileMode.Open), CompressionMode.Decompress, true))
{
deCompStream .CopyTo(xmlStream);
deCompStream .Close();
someClassInstance = (SomeClass) serializer.ReadObject(xmlStream);
}
}
return someClassInstance;
} Is it possible that using GZip facilities requires creating a 'temp file ?
thanks, Bill
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
modified 20-Sep-15 9:33am.
|
|
|
|
|
What if you try to end the using block a bit earlier. In other words:
using (MemoryStream xmlStream = new MemoryStream())
{
using (GZipStream deCompStream = new GZipStream(File.Open(gzFilePath, FileMode.Open), CompressionMode.Decompress, true))
{
deCompStream.CopyTo(xmlStream);
deCompStream.Close();
}
someClassInstance = (SomeClass) serializer.ReadObject(xmlStream);
}
modified 20-Sep-15 14:54pm.
|
|
|
|
|
Thanks, Mika,
I've tried many combinations of varying the code, including the one you recommend.
I also tried just creating a new MemoryStream and saving it; i.e., not using it within a 'Using block. Both these variations gave the same error as in the code above.
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
Not sure if this helps with your problem but when thinking about your question, why use the memorystream at all.
Consider the following:
public class SomeClass {
[DataMember()]
public string FirstName;
[DataMember]
public string LastName;
public static bool ObjectToGZ(SomeClass instance, string gzFilePath) {
using (GZipStream zipstream
= new GZipStream(File.Open(gzFilePath, FileMode.Create), CompressionMode.Compress, false)) {
DataContractSerializer ser = new DataContractSerializer(typeof(SomeClass));
ser.WriteObject(zipstream, instance);
zipstream.Close();
}
return true;
}
public static SomeClass GZToObject(string gzFilePath) {
DataContractSerializer serializer = new DataContractSerializer(typeof(SomeClass));
SomeClass someClassInstance = null;
using (GZipStream deCompStream
= new GZipStream(File.Open(gzFilePath, FileMode.Open), CompressionMode.Decompress, false)) {
someClassInstance = (SomeClass)serializer.ReadObject(deCompStream);
}
return someClassInstance;
}
}
And the call
SomeClass some1 = new SomeClass() { FirstName = "A", LastName = "B" };
SomeClass.ObjectToGZ(some1, "C:\\TEMP\\BillTest1.gz");
SomeClass some2 = SomeClass.GZToObject("C:\\TEMP\\BillTest1.gz");
|
|
|
|