|
Yes, I know but how to transfer to webclient the contain of listbox (items) and how create many threads without dommage application ?
Thanks in advance.
|
|
|
|
|
...and Nick gave you the answer if you dig a little deeper into the application. The HttpWebRequest and HttpWebResponse define Begin* methods that asynchronously invoke downloads. This uses a thread pool to minimize CPU load by your process.
There is a catch: the default ServicePointManager that the requests use limit request to a host to 2. You'll need to either change ServicePointManager.DefaultConnectionLimit to another number before every instantiating a WebRequest object to another host, or create your own ServicePoint and change it's ConnectionLimit property accordingly to the hosts in your list.
In the future, if you have a question be more specific. If you ask a generic question it's hard to give you a non-generic, and if you don't provide details we can't know what you do and don't know, or what you've tried and haven't tried.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hi
Is there anything like "Time to Live" for a Windows Form.
I want to close one after a few seconds. Is that possible?
I got VS 2003..
Greetings from Germany
|
|
|
|
|
Maybe I'm not understanding your question, but you can run a timer when your form starts up, then close the form after a certain amount of time lapses.
Any remotely useful information on my blog will be removed immediately. There are 10 kinds of people in the world. Those who have heard of the ubiquitous, overused, worn-out-like-an-old-shoe binary "joke" and those who haven't.
Judah Himango
|
|
|
|
|
The .NET Framework base class libraries (BCL) are a toolkit. Specific functionality like this is something you have to add.
In this case, all you need to do (if you're of the drag-n-drop variety) is to drag the Timer component from your toolbox in VS.NET onto your form. Set the internal you want (if by "a few seconds" you mean 3 seconds, you set it to 3000 (milliseconds). Switch to the event tab in the PropertyGrid and double-click the Tick event to add a handler. In the handler, add the following:
private void timer1_Tick(object sender, EventArgs e)
{
if (InvokeRequired)
{
MethodInvoker d = new MethodInvoker(Close);
Invoke(d, null);
}
else Close();
} This extra code is necessary because the event handler may fire on a different thread. You should not modify controls on threads other than the thread on which they were created, which is why the Control.InvokeRequired property and Control.Invoke method exist. The MethodInvoker is a standard delegate that takes no parameters and returns void. Often times you would have to declare your own delegates (which are like managed function pointers, or callbacks).
Don't forget to set the Enabled property to true . This would get set in InitializeComponent which is called from the constructor. If your form takes a while to open, you may consider overriding OnLoad (better than handling the Load event when you derive from a control that defines the event you want) and setting Enabled to true in your handler to make sure the timer doesn't start until your form is loaded (just before it displays).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
I want to change text color on a single line of text with DrawString().
Ex. <red>The <blue>qui<red>ck, b<blue>rown f<red>ox easily jumps over the lazy dog.
I'm trying to use Graphics.MeasureCharacterRanges(), this method gets the ranges
of a number of characters. All fine. Then I can use a TextureBrush with a colored
image to change the text color. The problem is that I can't use this method when rendering
Italic style text. The color rectangles don't compensate for italics text.
If anyone have any tips or a better method to do this, I will be very grathful.
Many thanks, Gywox
<br />
public void DrawTextColor(PaintEventArgs e)<br />
{<br />
Graphics graphics = e.Graphics;<br />
Rectangle layoutRect = new Rectangle(20, 20, 400, 100);<br />
Font tnrFont = new Font("Times New Roman", 24, FontStyle.Italic);<br />
StringFormat strFormat = new StringFormat();<br />
CharacterRange[] charRanges = {<br />
new CharacterRange(3, 4), <br />
new CharacterRange(12, 6) };<br />
Region[] charRegions = new Region[charRanges.Length];<br />
string str = "The quick, brown fox easily jumps over the lazy dog.";<br />
strFormat.SetMeasurableCharacterRanges(charRanges);<br />
<br />
Image image = new Bitmap(layoutRect.Width, layoutRect.Height);<br />
Graphics graphicsImage = Graphics.FromImage(image);<br />
graphicsImage.FillRectangle(Brushes.Red, 0, 0, image.Width, image.Height);<br />
<br />
charRegions = graphics.MeasureCharacterRanges(str, tnrFont, layoutRect, strFormat);<br />
for (int index = 0; index < charRegions.Length; index++)<br />
{<br />
RectangleF boundsRect = charRegions[index].GetBounds(graphics);<br />
graphicsImage.FillRectangle(Brushes.Blue, boundsRect);<br />
}<br />
TextureBrush textureBrush = new TextureBrush(image);<br />
graphics.DrawString(str, tnrFont, textureBrush, layoutRect, strFormat);<br />
}<br />
Gywox
|
|
|
|
|
Is there a way to test if a file is locked, without opening a FileStream? I find it particularly annoying that when you try to open a file that's locked, an exception is thrown. That's a really dumb use for an exception, but equally dumb is that there is no "test" function, as far as I know.
Interestingly, .NET 2.0 fixes a lot of this by providing "can succeed" type of methods, which is much faster than throwing an exception.
And yes, I know that a "can succeed" on a file lock might return true but then the subsequent Open call will fail because the file got suddenly locked.
Marc
MyXaml
Advanced Unit Testing
|
|
|
|
|
Would you rather have return codes like all the Win32 APIs? An exception is what should be thrown from the constructor. Would you rather it return null and not know why it returned null?
This is consistent with how the native APIs work, as I'm sure you know. When you call CreateFile you will get a return code that states the file is locked.
BTW, I don't see any such "can succeed"-type methods in the latest bits for 2.0 for the file-related APIs. Where did you see such information?
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Heath Stewart wrote:
When you call CreateFile you will get a return code that states the file is locked.
There is no CreateFile method in the .NET FileStream implementation. The API call is embedded in the constructor, it would seem. I would much rather have a method that I can call to test whether the file is locked.
Personally, I've never liked exceptions, except (haha) that they are a useful way of cleaning up the stack. They're innapropriately and inconsistently applied, and often abused.
Heath Stewart wrote:
I don't see any such "can succeed"-type methods in the latest bits for 2.0 for the file-related APIs
Sorry, I meant in general that there's an effort to do this, not that it is specifically done with file-related API's.
Marc
MyXaml
Advanced Unit Testing
|
|
|
|
|
Marc Clifton wrote:
There is no CreateFile method in the .NET FileStream implementation.
Yes, this I know. But if you look at the context, I was talking about native APIs, so I was talking about the native CreateFile API.
Exceptions can be abused, but I think the BCL has a pretty tight reign on them. I see abuses mostly in third-party code (the "less commercial" kind), or just a complete absence of throwing - and especially catching - them.
I personally like them (and other exception mechanisms, like SEH) better than return codes. You don't have to add different conditions with different localized texts (unless you're defining your own exceptions or using your own text, for that matter). The code impact is minimal, but I realize they do cause a slight performance drain to throw, as opposed to just returning an error code. I guess it's really just a matter of opinion, though.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
I hate to (cough) agree with Marc, but he's right. There no way an exception should be thrown when you can anticipate the failure of a method call beforehand. Exceptions are better thrown when a caller passes a NULL pointer or some other incorrect parameter value to a method when he shouldn't have; that's something you don't expect; that's something you don't know how to deal with; that's something you don't have an error value for; that's when you throw an exception.
Exceptions are for non-recoverable program failures. Proper error-handling is anticipating that a file you're trying to open may not exist; that should not be the cause of an exception.
Seems to me if the framework is going to provide file locking, it needs to provide a test method that does more than just test to see if the file is locked; it should be able to test, lock the file and open it all in one call, because between the return of the test and any attempt to open the file, someone else may beat you to the it, causing you to get the very exception you're trying to avoid by calling the test.
Marc's (cough) correct, as Microsoft has a bad design and implementation, and should fix the problem.
Also, there is no difference in all of the possible errors you can from win32 and the abundance of exceptions you can get from .NET. Exceptions as used today are little more than typed "goto" statements masquerading as objects, deluding people into believing they have OO error management.
When you force a developer to accept exceptions, you're telling him his program is going to run slower so all of the junior programmers using the language can write code that doesn't do proper failure testing, or instead put error-handling code in an else-statement that appears nowhere near the if-statement that tested the condition that failed. A good portion of exception code and logic entanglements (got that word from Marc) can be corrected by correcting the use of if-statements. Namely, don't handle errors in an else clause.
|
|
|
|
|
I'm writing a winforms app in C# that uses XMLHttp to download jpg's from the web. I previously used WebRequest, but decided to switch to XMLHttp because it utilizees IE's browser cache.
However, I can't figure out how to convert the response from XMLHttp into a bitmap. I've tried loading the response into a byte array, converting that to a memory stream, and then using the stream to create the bitmap, but no luck.
Can anoyone give me a pointer on doing this?
Better yet, does anyone know of a .NET framework method that will use the IE cache for http transactions? I know I can build my own using the Cache object, but really don't want to have to do that.
|
|
|
|
|
Does the response actually contain bitmap data, like for a GIF, JPG, PNG, etc. (or perhaps another image that would just be downloaded as binary data instead of being displayed in a page)? Your subject is misleading, which is why I ask.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hi gurus,
I would like to know how to access to the "enable" property of all controls within a form using the foreach loop?
I wish to disable all controls during an operation and enable them once the operation is complete.
Thanks.
Best regards
There is no spoon.
|
|
|
|
|
foreach (Control c in form.Controls)
c.Enabled = false;
Any remotely useful information on my blog will be removed immediately. There are 10 kinds of people in the world. Those who have heard of the ubiquitous, overused, worn-out-like-an-old-shoe binary "joke" and those who haven't.
Judah Himango
|
|
|
|
|
thanks
There is no spoon.
|
|
|
|
|
That'd only work if your controls are nested only 1 level deep. To enable all controls in all containers contained with your container, you'll need to use recursion - not enumeration:
public void EnableAll(bool enabled)
{
this.Enabled = enabled;
EnableAllRecursively(this.Controls, enabled);
}
void EnableAllRecursively(Control.ControlCollection controls, bool enabled)
{
if (controls != null)
{
foreach (Control c in controls)
{
c.Enabled = enabled;
EnableAllRecursively(c.Controls, enabled);
}
}
}
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hi,
Thanks to all.
Both techniques work.
In my case Judah's simple loop was the solution I've finally found.
But It is true that the simple loop work only at 1 level.
for nested windows, the recursive version is better...
Thanks to all again
There is no spoon.
|
|
|
|
|
Making such generalized assumptions is not a good practice. The recursive method would be best to implement now in case someday you add a composite control - like a UserControl - to your form. I merely adds an extra 1 or 2 calls to the call stack depending on the state, but still enumerates your first level controls as did the first example.
an even better approach and recursive in nature is to extend a control class and override Enable to also have it disable its children (some controls do this inherently, like the ComboBox ). Then when its children are disabled they disable their children. Now your code isn't responsible for its children and you have a better OO design.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Setting the .Enabled property on a control automatically sets the enabled property on all child controls, no?
Say you have 2 panels, and each panel has a button. Disabling the 2 panels will also disable the nested buttons.
Actually, perhaps a better solution would be to simply set the .Enabled property of the form itself, which should also disable any nested controls. Even if you explicitly turn on the Enabled property of a nested control, it will still be disabled if its parent is disabled.
Any remotely useful information on my blog will be removed immediately. There are 10 kinds of people in the world. Those who have heard of the ubiquitous, overused, worn-out-like-an-old-shoe binary "joke" and those who haven't.
Judah Himango
|
|
|
|
|
Typically, yes. It depends on the control.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
I don't know if you're talking about a windows app or a web app, but here goes:
Assuming all the controls in question can be enabled or disabled, then you'll have to recurse through the collection of controls from the top (form) level (since controls can contain other controls), so for each control found, you'll have to check its Controls collection.
Whenever you find a control, you'll have to make sure it can be enabled or disabled (meaning you'll have to cast it to its original type or a base type) before setting the enabled/disabled flag.
Please let me know if this isn't detailed enough - I can try to make some sample code, but I'm short on time right now.
The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’
|
|
|
|
|
I'm having an issue with C# string literals. I'm trying to include double quotes in the string for a sql text. I've followed all the advice as to use either the backslash or "double double" quotes, but when I check the resulting output in the command window during debug, I get the backslahes showing up in the string.
code:
(backslash)
string s ="select ld_part, ld_lot, ld__dte01, ld_ref, ld_qty_oh, pt_um, (ld_qty_oh / (ld__dec01 * .012)) when pt_um = \"MS\" from ld_det, pt_mstr where ld_part like \"" + spec + "\" and pt_part = ld_part and ld__Dec01 > 0 and ld_ref= \"\" order by ld__dec01, ld__dte01";
(@ sign and double doubles)
string s = @"select ld_part, ld_lot, ld__dte01, ld_ref, ld_qty_oh, pt_um, (ld_qty_oh / (ld__dec01 * .012)) when pt_um = ""MS"" from ld_det, pt_mstr where ld_part like """ +spec+ @""" and pt_part = ld_part and ld__Dec01 > 0 and ld_ref= """" order by ld__dec01, ld__dte01";
output in command window(either way):
?sql
"select ld_part, ld_lot, ld__dte01, ld_ref, ld_qty_oh, pt_um, (ld_qty_oh / (ld__dec01 * .012)) when pt_um = \"MS\" from ld_det, pt_mstr where ld_part like \"12406%\" and pt_part = ld_part and ld__Dec01 > 0 and ld_ref= \"\" order by ld__dec01, ld__dte01"
desired result:
"select ld_part, ld_lot, ld__dte01, ld_ref, ld_qty_oh, pt_um, (ld_qty_oh / (ld__dec01 * .012)) when pt_um = "MS" from ld_det, pt_mstr where ld_part like "12406%" and pt_part = ld_part and ld__Dec01 > 0 and ld_ref= "" order by ld__dec01, ld__dte01"
Any ideas?
thanks!
Bill Polewchak
Technical Design Lead
Software Development/Architecture
Avery Dennison Corp.
|
|
|
|
|
Both of your examples will produce the same string. The debug window shows the backslashes because it is intended to display "special" characters in that way. When the string is sent to the database, the characters will be expressed as intended, not parsed for display. I don't know what kind of database you're using, but what you have should work in Access.
You might consider sidestepping the whole issue and using single quotes - I think they'll work in Access, and they're required by most other DB platforms instead of double quotes.
One other note: You might want to also consider using parameterized queries instead of putting everything into a single string. I don't know where the value for "spec" comes from, but if it is in any way visible to users, you're opening yourself to SQL injection attack. Also, parameterized queries can often be cached by the DB platform, improving performance.
Hope this helps.
The most exciting phrase to hear in science, the one that heralds the most discoveries, is not 'Eureka!' ('I found it!') but 'That's funny...’
|
|
|
|
|
Thanks for promptly confirming my hopes! I was wondering whether or not this is a display issue in the command window.
Since this is being used in an oledb (ODBC.NET) object query to a Progress database in our ERP system, I needed to use the double quotes. The resident application db is SQL Server 2000, so in that case, I could have used single quotes, or as I'm more used to doing, using a SQLOLEDB command object to call a parameterized sp!!!
I actually had to do a bit of research to use this old school text method
Thanks again.
cheers,
Bill Polewchak
Team Leader
Software Development/Architecture
Avery Dennison Corp.
|
|
|
|
|