|
-
Dec 18th, 2006, 12:13 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] [VS2005] Threading
I have a program that is running multiple threads at the same time. The maximum amount of threads is a user-changeable option. I was doing some testing on this and after a certain amount of threads have been called, I get a System.OutOfMemoryException.
I have a program I created in Visual Studio 2003 that never had this problem, it did pretty much what this application is doing, and it doesn't give me that error with even more threads being called than my current application.
I know they have changed a few of the threading things with the new 2.0 Framework, I was wondering if they made a max limit of sorts also?
I guess, my questions are...
1) What is the correct way to start a thread?
2) Do I need to close/dispose the thread in some way? (An example of this would be helpful if this is the case).
Here is the code I am using, and where the error occurs.
lbox is an arraylist of values that need calculated.
threadParamClass is a class I made to pass parameters via thread.
The thread can access the parameters passed this way.
PHP Code:
for (int x = 0; x <= thecount; x++)
{
Application.DoEvents();
gu = null;
mythread = null;
gu = new threadParamClass(f1, this, x,lbox[0].ToString());
mythread = new Thread(new ThreadStart(gu.Calculate));
mythread.IsBackground = true;
mythread.Start(); //out of memory here
cint++;
f1.textStat.Text = x + "\\" + thecount;
lbox.RemoveAt(0);
if (cint == 100)
{
manResetEvt.Reset();
manResetEvt.WaitOne(500000, false);
}
}
Last edited by Tool; Dec 21st, 2006 at 04:04 AM.
Reason: Resolved (Thanks wild_bill)
-
Dec 19th, 2006, 02:01 AM
#2
Re: [VS2005] Threading
Have you tried using a threadpool to manage your threads?
Code:
for (int x = 0; x <= thecount; x++)
{
Application.DoEvents();
gu = new threadParamClass(myParms);
ThreadPool.QueueUserWorkItem(new WaitCallback(gu.Calculate));
//do other work
}
public class threadParamClass
{
public threadParamClass(object parms)
{
//setup your class
}
public void Calculate(object stateInfo)
{
Console.WriteLine("I'm doing work");
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|