Thread Stops working on its own HELP!

08/31/2010 00:28 µ~Xero~µ#1
Its set so this thread runs every 500ms. Im aware here arent any Try/Catch blocks. They didnt catch anything.

After some jumping, or just waiting the thread stops. I cant figure out where the problem is at... Its not a dead loop, not a dead lock,...

PHP Code:
public static void CharThread()
       {
           
lock (Kernel.AllChars)
           {
               foreach (
KeyValuePair<uintCharacterCharr in Kernel.AllChars)
               {
                   
Character MyChar Charr.Value;

                   if (
MyChar.DoubleEXP)
                   {
                       
DoubleExp.Handle(MyChar);
                   }
                   if (
MyChar.Attacking)
                   {
                       
HandleAttack.Attack(MyChar);
                   }
                   if (
MyChar.CastingPray || MyChar.Praying)
                   {
                       
LuckyTime.Handle(MyChar);
                   }
                   
GeneralHandling.Handle(MyChar);
               }
               
Thread.Sleep(1);
           }
       } 
Edit: This way seems to work... atleast works since now but idk why.. Im just scared itll crash at one point tho

PHP Code:
public static void CharThread()
       {
           
lock (Kernel.AllChars)
           {
               
IDictionaryEnumerator DE Kernel.AllChars.GetEnumerator();
               try
               {
                   while (
DE.MoveNext())
                   {
                       
Character MyChar = (Character)DE.Value;

                       if (
MyChar.DoubleEXP)
                       {
                           
DoubleExp.Handle(MyChar);
                       }
                       if (
MyChar.Attacking)
                       {
                           
HandleAttack.Attack(MyChar);
                       }
                       if (
MyChar.CastingPray || MyChar.Praying)
                       {
                           
LuckyTime.Handle(MyChar);
                       }
                       
GeneralHandling.Handle(MyChar);
                   }
               }
               
finally
               
{
                   
IDisposable disposable DE as IDisposable;

                   if (
disposable != null)
                       
disposable.Dispose();
               }
               
Thread.Sleep(1);
           }
       } 
08/31/2010 00:48 Korvacs#2
You dont loop the void so it does it once and then closes.

Eg:

Code:
        private static bool DoWork = true;

        static void ThreadWorker()
        {
            while (DoWork)
            {
                //Perform Work
            }
        }
08/31/2010 00:52 µ~Xero~µ#3
I´m afraid i dont get it, sorry..
could you try explaining in-depth?

EDIT: Im using that LIKE a timer. A thread with intervall.

PHP Code:
public class ThreadTimer
    
{
        public 
event Execute Execute;
        
Thread T;
        
        
DateTime DT;
        
uint interval;
        
/// <summary>
        /// Starts the Thread on specific Intervall(Miliseconds)
        /// </summary>
        /// <param name="Interval"></param>
        
public void Start(uint Interval)
        {
                
interval Interval;
                
= new Thread(new ThreadStart(Run));
                
T.Start();
        }
        
bool closed false;
        
/// <summary>
        /// Stops The Thread
        /// </summary>
        
public void Close()
        {
            
closed true;
        }
        
/// <summary>
        /// Starts the Thread
        /// </summary>
        
void Run()
        {
            try
            {
                while (
true)
                {

                    if (
DateTime.Now DT.AddMilliseconds(interval))
                    {
                        
DT DateTime.Now;
                        try
                        {
                            
Execute.Invoke();
                        }
                        catch (
Exception e) { GUI.g.WriteToRichBox("Threading SYstem Error! \n\r" e1); Thread.Sleep(1); }
                    }
                    
Thread.Sleep(1);
                }
            }
            catch (
Exception e)
            {
                
Debug.WriteLine("Threading Error: A Thread crashed while executing! LOG: " e.ToString());
                
Thread.Sleep(1);
            }
        }
    } 
08/31/2010 00:53 Korvacs#4
Quote:
Originally Posted by Korvacs View Post
You dont loop the void so it does it once and then closes.

Eg:

Code:
        private static bool DoWork = true;

        static void ThreadWorker()
        {
            while (DoWork)
            {
                //Perform Work
            }
        }
Sorry just edited my post.

If you dont tell create some form of loop to perform the work required, then the thread will call the void once (and only once) and then the void will return, and the thread will close itself because it beleives its finshed.

Quote:
Originally Posted by µ~Xero~µ View Post
I´m afraid i dont get it, sorry..
could you try explaining in-depth?

EDIT: Im using that LIKE a timer. A thread with intervall.

PHP Code:
public class ThreadTimer
    
{
        public 
event Execute Execute;
        
Thread T;
        
        
DateTime DT;
        
uint interval;
        
/// <summary>
        /// Starts the Thread on specific Intervall(Miliseconds)
        /// </summary>
        /// <param name="Interval"></param>
        
public void Start(uint Interval)
        {
                
interval Interval;
                
= new Thread(new ThreadStart(Run));
                
T.Start();
        }
        
bool closed false;
        
/// <summary>
        /// Stops The Thread
        /// </summary>
        
public void Close()
        {
            
closed true;
        }
        
/// <summary>
        /// Starts the Thread
        /// </summary>
        
void Run()
        {
            try
            {
                while (
true)
                {

                    if (
DateTime.Now DT.AddMilliseconds(interval))
                    {
                        
DT DateTime.Now;
                        try
                        {
                            
Execute.Invoke();
                        }
                        catch (
Exception e) { GUI.g.WriteToRichBox("Threading SYstem Error! \n\r" e1); Thread.Sleep(1); }
                    }
                    
Thread.Sleep(1);
                }
            }
            catch (
Exception e)
            {
                
Debug.WriteLine("Threading Error: A Thread crashed while executing! LOG: " e.ToString());
                
Thread.Sleep(1);
            }
        }
    } 
If your going to the trouble of trying to write your own sort of timer, why not just use the Timer class which is in the framework, it will do exactly what you want, and it runs on its own thread.
08/31/2010 01:06 µ~Xero~µ#5
My ThreadTimer Class works more efficient than a timer does. Ive discovered a small performance plus.

Also you have stated if i use a "foreach loop" it will only call the void once, which i cant copy.

Eg. Mob Thread: Same as my CharThread(1st code) runs over and over.

My CharThread (1st Code) runs like 20 times too then it stops.
08/31/2010 01:23 Korvacs#6
Quote:
Originally Posted by µ~Xero~µ View Post
My mob thread is exactly same as my char thread (1st code). It doesnt stop.

My ThreadTimer Class works more efficient than a timer does. Ive discovered a small performance plus.

Also you have stated if i use a "foreach loop" it will only call the void once, which i cant copy.

Eg. Mob Thread: Same as my CharThread(1st code) runs over and over.

My CharThread (1st Code) runs like 20 times too then it stops.
No, what i said (based on your first post) was that if you start a thread and call a void without a loop in it, it will call once and return, this is true. What you failed to mention was that you had anouther thread starting the second thread over and over.

I dont remember mentioning a foreach loop, i showed you an example, the foreach loop would be called once, and then since the foreach loop isnt infinite it would close at some stage, the void would return and the thread would close.

As for your claim that it improves performance, i find that highly unlikely, your using 1 thread, to cycle and start numerous threads repeatedly, where as the timer is 1 thread, which performs an action every given interval, sort of like this:

This is just an example, i have not bothered to test it, however i beleive this would work perfectly fine.

Code:
        private static bool DoWork = true;

        static void Worker(long Interval)
        {
            TimeSpan begin = Process.GetCurrentProcess().TotalProcessorTime;

            while (DoWork)
            {
                if ((begin - Process.GetCurrentProcess().TotalProcessorTime).Ticks >= Interval)
                {
                    //Perform Work
                    begin = Process.GetCurrentProcess().TotalProcessorTime;
                }
            }
        }
All in one thread, work is performed every interval as required, nothing to worry about, not spawning hundreds of threads needlessly.
08/31/2010 01:28 µ~Xero~µ#7
Now everything is clear! Thanks alot for having the needed patience with me :)

Also idk why but If i use timers, my CPU Usage goes up to 4% where it uses as it is now 1%

Ima try your way tho.

Last thing i dont get:

Why did my 1st code not work and why does the second one?
08/31/2010 01:36 Korvacs#8
Not sure, i would need to debug it myself to understand your system fully.

If your worried about using timers that much, write a thread similar to what i just showed you where you check the interval inside a loop and perform your work as and when you need to, also make sure you have a Thread.Sleep(1) in there somewhere otherwise your CPU usage will skyrocket.

And no problem, thanks for listening, makes a change :p
08/31/2010 01:41 µ~Xero~µ#9
Ill work on some system like you suggested now. thanks a lot for clearing things up!

We can be proud to have you supporting us. even tho u made me get banned =(..<3

can now be closed if you want to.