|
Please be careful with this and don't rely on using this method too much as uses ThreadPool threads of which there are only 25 assigned. It may be the case that .Net will adjust this as necessary but, you may want to revise your design to using a dedicated thread if the number of concurrent events becomes large. You'll notice this if your program slows, but you have no explanation for the slowness.
Oh and another note, *invoke methods, along with the UI, generally use thread pool threads to accomplish their tasks.
Finally, when you read the documentation on thread pool threads, please note that a busy system will sometimes drop the tasks assigned to thread pool threads just because the system is too busy.
Phil
|
|
|
|
|
Thanks for this pointer.
I was going to implement a limit of 20 calls to IAsyncResult at any one time as I had read somewhere else about the issue you raise. I have since read the msdn note on the thread pool.
I'm beginning to wonder whether I should create separate backgroundworkers for this task but someone else has said that these backgroundworkers also use the thread pool.
I'm now not sure of which way to go. I'm tempted to creat separate threads and be done with it.
I would expect that on average there will be something like 200 to 500 dns lookups to be performed. Often less but sometimes double this.
What would you suggest.
BTW: The UI will be blocked during these lookups with a simple progress bar shown for the user to watch if they are totally bored .
Glen Harvy
|
|
|
|
|
How long will the operation take that the thread pool thread will be doing? If it is a long process, then by all means create your own pool of threads to handle this. Otherwise using the thread pool threads will work just fine. DNS lookups should be short enough for you to be able to use the thread pool threads.
Another note about thread pool threads: If an exception occurs within the thread, it is not reported to the application and therefore you should always do exception handling in the thread pool thread if for no other reason than to indicate when something has gone wrong.
Phil
|
|
|
|
|
Thanks again Phil.
I suppose my question was really "how longs a piece of string" but your experience was what I was looking for and thanks for sharing.
I was aware of the exception problem and already have ways of dealing with this
Glen Harvy
|
|
|
|
|
|
Leslie Sanford wrote:
private void MyResultMethod(string s)
{
MessageBox.Show(s);
}
Ok, I've been able to digest most of this but still have my original problem.
How can I "do other updating .." unless I know which hostname that this ip belongs to. I can't figure out how to match this thread with any of the other threads I've created at:-
for (int x = 0; x < hostList.Count; x++)
{
getIP.BeginInvoke(hostList[x], new AsyncCallback(MyCallback), getIP);
}
-- modified at 6:07 Wednesday 15th August, 2007
I've just noticed that doing what you suggest doesn't actually run more than one call in the calling foreach loop at a time which is not what I wanted. The main thread needs to create at least 20 lookups running in async mode at any one time. Does that make more sense?
Glen Harvy
|
|
|
|
|
Glen Harvy wrote: Ok, I've been able to digest most of this but still have my original problem.
How can I "do other updating .." unless I know which hostname that this ip belongs to. I can't figure out how to match this thread with any of the other threads I've created at:-
Does the getIP object have this information? If so, you can pass this information along to the UI from your callback:
delegate void Result(string hostName, string result);
delegate void ExceptionHandler(Exception ex);
private void MyCallback(IAsyncResult ar)
{
getDNS getIP = (getDNS)ar.AsyncState;
try
{
string result = getIP.EndInvoke(ar);
BeginInvoke(new Result(MyResultMethod), getDNS.HostName, result);
}
catch(Exception ex)
{
BeginInvoke(new ExceptionHandler(MyExceptionHandler), ex);
}
}
private void MyResultMethod(string hostName, string result)
{
MessageBox.Show(hostName);
}
private void MyExceptionHandlerMethod(Exception ex)
{
MessageBox.Show(ex.Message);
}
Glen Harvy wrote: I've just noticed that doing what you suggest doesn't actually run more than one call in the calling foreach loop at a time which is not what I wanted. The main thread needs to create at least 20 lookups running in async mode at any one time. Does that make more sense?
I'm not sure I follow. All I've shown is the callback method. This is independent of how many times it runs.
|
|
|
|
|
Thanks for your input - it has helped me glean a lot from using the async features.
I decided to go with using the following as it suits my purposes well (I think):
IAsyncResult[] results = new IAsyncResult[hostList.Count];
if (results.Length > 19)
{
}
for (int x = 0; x < hostList.Count; x++)
{
results[x] = getIP.BeginInvoke(hostList[x], null, null);
}
for (int i = 0; i < results.Length; i++)
{
if (results[i].AsyncWaitHandle.WaitOne(20000, true))
{
string r = getIP.EndInvoke(results[i]).ToString();
string[] n = r.Split(new char[1] { '|' });
DataRow nr = this.myWebLoggerDataSet.Tables["dnscache"].NewRow();
nr["IP"] = n[1].ToString();
nr["Name"] = n[0].ToString();
nr["Date"] = DateTime.Now;
this.myWebLoggerDataSet.Tables["dnscache"].Rows.Add(nr);
}
}
this.dnscacheTableAdapter.Update(this.myWebLoggerDataSet.dnscache);
myWebLoggerDataSet.AcceptChanges();
I think I'll be OK now but of course any comments will most definitely be appreciated.
Glen Harvy
|
|
|
|
|
Does anybody know if there's a way to interact with a printer that prints onto CD's? I've googled it and haven't found anything promising. The app that I'm writing is going to need it. It seem that only the manufacture of the printer can interface with it. Any ideas?
|
|
|
|
|
If a printer that prints to CDs does anything special, it's propetary, and you'll need to talk to the manufacturers of that printer, to work out how to interact with it
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
I'm having a hard time installing the sourceGrid into my workstation. I download the source code and I don't know where to put it to get it to work with my Project. Can someone show me steps by steps on how to installed it and how I can use it on my project.
For starter I just want to use the Basic grid and show a 3x5 grid.
|
|
|
|
|
Just compile the project to get the binaries and then add a reference to the assembly produced in your project. You won't have to include the sources then.
|
|
|
|
|
What grid ? Is it an article ? The article has a forum for questions.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
When I add the dll files as a reference I get the following error message
A Reference to "D:\path\to\project\sourcegrid\bin\sourceGrid.dll" could not be added. This is not a valid assembly or COM component. Only assemblies with extension 'dll' and COM components can be referenced. Please make sure that the file is accessible, and that is a valid assembly or COM component.
Any idea why I'm getting this error?
|
|
|
|
|
Could anyone tell me why this doesn't work:
string st = "ä";
st.Replace("ä", "ae");
Console.WriteLine(st); //output: ä
What is going on?
|
|
|
|
|
string.Replace() is a method with a string return value. Try this
st = st.Replace("a", "ae");
- Arcond
|
|
|
|
|
Thanks! I would never have figured it out alone.
|
|
|
|
|
Hi,
strings are immutable, this means nothing can change their value; the only
thing you can do is create a new string, and that's what this method does for you.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Is it possible to implement Up,Down,Left, Right, and Enter function for DVD playback in Windows Media Player SDK?
I want to create my own video player that has these buttons that will control DVD movies.
|
|
|
|
|
Hi,
I have a class Dog which derives from Animal. Suppose my instance of Dog is bulldog. bulldog has all the members of Animal plus Dog. How can copy just the members from Dog to a new Dog called boxer? Just get the Dog members from bulldog and leave out any inherited members from Animal.
Dog bulldog=new Dog(); <-has inhereited members from Animal
Dob boxer = bulldog - inherited Animal members
How can I do this?
/\ |_ E X E GG
|
|
|
|
|
You could add a method to your Dog class that returns a copy of the current instance of the class. In your code, you could omit copying the member of the Dog class that have been inherited from the Animal class.
Paul
|
|
|
|
|
You want to set properties that are common to the animal class, and then keep them that way ? So, for instance, if sex is a common property, you want to do this:
Dog bulldog=new Dog(); // defaults to male
bulldog.Sex = Sex.Female;
Dob boxer = bulldog; // boxer is male
That's a weird and counterintuitive requirement. You could do this with your own operator =, but I would recommend against it. Adding a method that creates a new Dog, then sets the local properties, is the way to go
public Dog GetDefaultAnimalWithDogProperties()
{
Dog dog = new Dog();
dog.DogBreed = this.DogBreed; // Or whatever properties are only on the dog class
return dog;
}
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Maybe, I'm not explaining it very well, here's what I want to do without dog metaphors.
I have a class that is derived from another class that does lots of reflection.
So when I pass my class to JavascriptConvert.SerializeObject(myclass) all of the base class reflection causes a self referencing loop exception to be thrown from SerializeObject. That's why I don't want to include anything from the base class.
(SerializeObject creates a JSON string of the object)
-- modified at 18:24 Tuesday 14th August, 2007
/\ |_ E X E GG
|
|
|
|
|
Hi,
when you use reflection, you can specify the scope of the things you are looking for.
As an example, Type.GetMethods accepts a BindingsFlag, you may want to specify
BindingFlags.DeclaredOnly to avoid inherited stuff.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Hi.
I have 2 questions: How to... (in C#)
1) set caret position in the textBox/richTextBox
2) scroll any scrollable control (multiline textBox/listView/listBox e.t.c) programmatically?
Please answer. )
Artem
|
|
|
|