|
Judah Himango wrote: What happens if you remove all Invoke and BeginInvoke calls?
Same thing, if it's called from within a worker thread it's quite a bit slower. I've even done some more timings where i take out the long process out of the method that takes too much time and there is still a noticable difference between threaded and non-threaded.
|
|
|
|
|
How much slower are we talking here?
Keep in mind, multithreading does not make your application any faster on single-CPU machines. In fact, due to context switching, using multiple threads is actually slower than using a single thread on single-CPU machines.
So with that in mind, of course running on a background thread is going to take longer. What's more is, if your main UI thread is busy and you've got only 1 CPU, then your CPU is going to be constantly doing context switches to execute pieces of each thread, which is going to slow down the overall background process. If you've got a dual core machine or better, this wouldn't apply.
But you shouldn't be seeing huge differences even on single-CPU machines; only minor differences. If you're seeing huge differences, then something in your code is wrong (lock problems? sharing data between threads?), but it's impossible to tell without seeing what all the code is doing.
If all else fails, run a performance profiler on your code and see what's taking time. I use Ants Profiler which gives per-line timings and shows you your slowest methods. This should help diagnose what's taking long, which will help you figure out why it's taking longer on a background thread.
|
|
|
|
|
I ran the Ants profiler and it takes 90 seconds within the background thread and less then 1 second if it's in the main thread. This seems excessive to me to be just single CPU related but it may be, considering every line of code is being executed an equal number of times regardless which thread executes it. I'll have to run it on my dual core processor tonight and see if the results are better.
|
|
|
|
|
an 89 second difference tells me that certainly something is wrong in your code. What lines of code are taking longer? What are those lines doing?
|
|
|
|
|
I have a foreach loop that does a number of string comparisons, contains, and removes whitespace.
I tried running it on a dual core processor and there was no change.
|
|
|
|
|
Is it possible to post your project? I'm pretty sure I could fix it if I've got it here sitting in front of me.
When you run it under the performance profiler, which lines are taking longer? Surely the string comparison and manipulation isn't taking longer on a background thread?
|
|
|
|
|
Judah Himango wrote: Is it possible to post your project? I'm pretty sure I could fix it if I've got it here sitting in front of me.
When you run it under the performance profiler, which lines are taking longer? Surely the string comparison and manipulation isn't taking longer on a background thread?
Unfortunately I'm not able to send my project. But I did look at the profiler a little more and can give you some more information on the lines of code that seem to be causing the slowdown.
The dataobject that I pass into the method that takes a long time has a few properties that are List<htmlelement>. I loop through all these and analyze the individual HtmlElements contained within the list.
When I do the following:
<br />
string href = link.GetAttribute("href");<br />
*where link is an HtmlElement that is contained in the list that's a property of the passed in DataObject
or
<br />
if((currentelement.Name == destination &&<br />
currentelement.TagName.ToUpper() == "A") ||<br />
currentelement.Id == destination)<br />
*where currentelement is an HtmlElement that is contained in the list that's a property of the passed in DataObject
When I access the properties of the HtmlElement within the background thread is when the excessive execution takes place. This may have to do with HtmlElement actually being a COM object with the .NET wrapper around it?
Hopefully this gives you some more info I appreciate your time looking into this.
|
|
|
|
|
Yes; since HtmlElement is actually part of a windows forms control, you may not want to access that on a background thread. Also, since the background thread is most likely MTA thread apartment, COM is probably wailing loudly at all this.
Instead of passing a list of System.Windows.Forms.HtmlElement to the background thread, can you pass a copy of only the data you need? For instance, from your snippet it appears you need the href, Name, TagName, and Id of every HtmlElement. So instead of pulling those from the MTA background thread, could you pull them from the UI thread, then pass those strings off to the background worker (perhaps contained in a custom HtmlElementInfo class or something)?
p.s. ToUpper() always allocates a new string. Perhaps you could use this instead:
string.Equals(currentElement.TagName, "A", StringComparison.InvariantCultureIgnoreCase)
|
|
|
|
|
I actually just started to make my own HtmlElement class which only contains the info I need. I think this will solve the problem. Thanks again
|
|
|
|
|
Hello,
I have the following code:
strCmd = "UPDATE [TBl] SET [text] = " + htmldoc + " where id = 11";<br />
OleDbCommand cmd2 = new OleDbCommand(strCmd, conn);<br />
cmd2.ExecuteNonQuery();
Where "htmldoc" is a variable that contains the content of a big html file with lot of quotes extra... so when I execute the code I'm getting an error.
How can I fix that?
Thanks in advance.
|
|
|
|
|
That is not the problem. Quotation marks has no special meaning at all in a string in SQL. The problem is that you forgot the apostrophes around the string.
There are other characters that you should encode, though, as they may appear in the html code. You should encode apostrophes, and if you are using an MySQL database you should also encode backslashes.
---
b { font-weight: normal; }
|
|
|
|
|
Hi
Your problem is, quite possibly, the fact you're using concatenated command text. If the HTML document contains any apostrophe characters, the command will fail. Even worse, if the HTML document contained specially crafted SQL commands, your whole database could be wiped out or an attacker could read your entire database freely. This security risk and bug in your code is known as SQL injection.
Here's how to fix the problem and remove the security risk:
strCmd = "UPDATE [TBl] SET [text] = @HtmlInput where id = 11";
OleDbCommand cmd2 = new OleDbCommand(strCmd, conn);
OleDbParameter htmlParameter = new OleDbParameter("@HtmlInput", htmlDoc);
cmd2.Parameters.Add(htmlParameter);
cmd2.ExecuteNonQuery();
Note that this assumes htmlDoc variable to be of type string.
p.s. next time use <pre> tags to surround your code snippets.
|
|
|
|
|
Hi!
It's embarrasing to ask such a simple question, but is there a simple way to find out how much a NumericUpDown control changed on a ValueChanged event? I know it can be worked around, but I first want to check so that I don't do a lot of extra work.
Regards,
Martin
|
|
|
|
|
Nope, the ValueChanged event does not send the old value, sorry.
Obviously this could be done via a workaround by storing the current value every time the ValueChanged event occurs. You can then compare that against the current value the next time the ValueChanged event occurs.
|
|
|
|
|
The problem is that I have a lot of spinners, and I don't think it's very efficient to create a variable for every single one of them...
|
|
|
|
|
You'd be storing an integer variable for each of them. Unless you have thousands of numeric up down controls, the performance hit would be negligible.
|
|
|
|
|
hi ..
this is my code to set ImageIndex of my button :
ImageList list = new ImageList();
System.Drawing.Icon _icon = new Icon("PRINTERS11.ico");
list.Images.Add(_icon);
button1.ImageList = list;
button1.ImageIndex = 0;
but my Icon is very small in button and i can not resize :
button1.ImageList.Images[0].Size
because it's readonly . How to set size of My icon in button ??
thanks
|
|
|
|
|
You need to copy it onto a bitmap that is bigger, of course this will make it jagged in appearance.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Dear all,
I made a custom control (a type of thumbnail with labels, buttons, ..) and I would like them to be in a list(box). Is this possible? And if yes (please do), how can I do this? Can I add it as some "item object", and add that to the list?
Thank you,
Michael
www.code.ae
|
|
|
|
|
With Windows Forms, you can, but the list box won't show them; it'll just show the .ToString version of the custom control. Note that with the WPF toolkit this is possible.
AFAIK, in order to do that, you'll probably need to inherit a control from ListView and draw your items manually in the list. Not fun.
|
|
|
|
|
I am having great difficulty in marshalling from VB6 to C#. What I need to be able to do is marshall a VB6 Type aka UDT holding a number of fields into C#. Is this possible? If not then I will need to change the legacy code to use a class rather than a UDT.
|
|
|
|
|
Further to my first post above. I went the route of instantiating an object in VB6 that has a number of public fields of type Currency. Successfully created a .NET TypeLib with TLBIMP and can marshall my VB6 object accross to C# but when I tried to access the field value I got an invalid cast exception. Does this mean that I will have to write the ODL information within my C# class rather than use the TLBIMP tool so that I can then marshall the field data from unmanaged type 'Currency' to Decimal?
Or is there an easier way to marshall accross from VB6 to C# and back? Any help will be much appreciated.
|
|
|
|
|
Greetings!
I have been using the System.Management object in a Visual Studio 2003 C# application to access Microsoft DNSS entries. The problem I have encountered is with TXT records which have multiple string entries ("A DNS TXT record can be up to 65535 (0xFFFF) bytes long"). Microsoft supports this just fine, for example, I have a TXT record with the following string values:
KeyA=ValueA
KeyB=ValueB
KeyC=ValueC
KeyD=ValueD
KeyE=ValueE
KeyF=ValueF
KeyG=ValueG
KeyH=ValueH
Microsoft DDNS has no problem with this. But when I retrieve the TXT record with:
(ObjectQuery oQ = new ObjectQuery("select * from MicrosoftDNS_TXTType where OwnerName=...");
the WMI query returns in the following:
OwnerName: aaatesttxtrec.domain.com
RecordData: "KeyA=ValueA" "KeyB=ValueB"
DescriptiveText: KeyA=ValueA
TextRepresentation: aaatesttxtrec.domain.com IN TXT "KeyA=ValueA" "KeyB=ValueB"
(changing the domain name to protect the guilty)
Now, I would be a whole lot less confused if the WMI query had only returned the first value string (or, if the total length of the returned values was less than 64, the maximum length for a single TXT value string).
So why am I only getting the first two (of eight) data strings from the TXT record? Is this a bug in the .NET WMI interface, or am I doing something horribly wrong?
Thank you'all for any help you may be able to offer!
Grant V. Morse
|
|
|
|
|
Hi
i have a made an app where you can draw things like lines graphics etc.,the problem is that when i save it there are no lines in bitmap, though all the physiacal controls like labels are there , but no lines or graphics why?
Sameer Sood
Microsoft Student partner
NIT Durgapur,india
|
|
|
|
|
How do you keep the information about the graphics, and what do you do to save it?
---
b { font-weight: normal; }
|
|
|
|
|