led mike wrote:
The threads are NOT "finished" after they are "queued", they are ummmm... queued.
Yup. Figured it out.
Kelsen
led mike wrote:
The threads are NOT "finished" after they are "queued", they are ummmm... queued.
Yup. Figured it out.
Kelsen
led mike wrote:
The threads are NOT "finished" after they are "queued", they are ummmm... queued.
After the foreach completes and finishes waiting the threads should have finished executing. An example I wrote prior to posting this question was:
public class ThreadTest
{
private static readonly int MAX_THREADS = 5;
public static void Main()
{
ThreadPool.SetMaxThreads(MAX_THREADS, MAX_THREADS);
for (int i = 0; i < 20; i++)
ThreadPool.QueueUserWorkItem(new WaitCallback(processThread), (i + 1));
Thread.Sleep(25000);
Console.WriteLine("Finished!");
}
static void processThread(object obj)
{
Console.WriteLine("Thread " + (int)obj + " executing.");
Thread.Sleep(5000);
}
}
This is of course assuming the Thread.Sleep(25000) will be long enough for the last 5 threads to execute. In my original post I didn't show my Thread.Sleep() but it was there within my code. I'll better document that to help clear up any confusion.
Kelsen
Leslie Sanford wrote:
Could it be that the console prints out before the thread pool items have a chance to run?
I migrated the WriteLine outside of the method and it still outputs 0. The code looks like:
public class SomeClass {
private static ManagerClass manager = new ManagerClass();
public static void Main() {
List beans = manager.getBeans();
Console.WriteLine(beans.Capacity);
}
}
I also placed a wait at the end of the getBeans method. Any ideas?
Kelsen
I'm new to C# threads and I was trying to populate a list with a series of threads but my list is not being populated after the threads finish. Some code might help describe my situation: NOTE: I have left some methods out but I don't believe that will be an issue.
public class ManagerClass {
private List<BeanType> beans = new List<BeanType>();
private static Object lockObj = new Object();
private static readonly int MAX_THREADS = 5;
public List<BeanType> getBeans() {
string[] keys = null;
keys = getKeys();
ThreadPool.SetMaxThreads(MAX_THREADS, MAX_THREADS);
foreach (string key in keys) {
ThreadPool.QueueUserWorkItem(new WaitCallback(getValueThreaded), key);
}
// Assuming 10000 is enough time for the threads to finish.
Thread.Sleep(10000);
Console.WriteLine(beans.Capacity);
return beans;
}
public void getValueThreaded(object obj) {
string key = (string)obj;
string value = getValue(key);
lock(lockObj) {
beans.Add(new BeanType(key, value));
}
}
}
That is the jist of it. BeanType is an arbitrary type for the sake of this example. In the console the list prints out a capacity of 0 every time. Can anyone explain this? -- modified at 11:20 Tuesday 6th March, 2007 -- modified at 12:24 Tuesday 6th March, 2007
Kelsen
For starters, I am coming from a C/C++ background and I am trying to format a string with String.Format() but the options are somewhat different than that of the conventional printf and its derivatives. Essentially what I want to have happen is I want to print out a numeric value in currency format but I want the number of spaces "before" the decimal place to be fixed. For instance, in the following example I show what I want the output to look like: WHAT I WANT!
Name: Amount Due:
Jack Rolley $1,234.56
Jane Polley $ 843.76
WHAT I HAVE BEEN GETTING!
Name: Amount Due:
Jack Rolley $1,234.56
Jane Polley $843.76
I have tried using a variety of option with the String.Format() method and I can't seem to find a way to get what I want. Is there a common method (option) that I can use to get the hanging dollar sign? I am using Visual Studio .NET at the student labs here at my university. Any insight into the string formatting issue would be greatly appreciated. Thanks in advance, Luke Martell Kelsen_@hotmail.com