Coder Social home page Coder Social logo

Comments (2)

Dobby233Liu avatar Dobby233Liu commented on August 19, 2024

creation date = 2009-03-04T10:48:35.51-08:00

I've experienced these quite alot some time ago... my solution for fixing it was implementing a pool of threads. In my app there are about 7-8 concurrent threads and some of course will require access to the UI.

When working with the tree from another thread you should go for something like if (this.InvokeRequired) ... and use BeginUpdate() and EndUpdate() when using data from the tree.

This is my ThreadMgr class, feel free to adapt it to your needs.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Collections;

namespace LUR.Interop
{
    public class ThreadMgr : IDisposable, IEnumerable
    {
        private List<Thread> Pool = null;

        public ThreadMgr()
        {
            Pool = new List<Thread>();
        }

        public int Clean()
        {
            int Running = 0;
            if (Pool == null)
                return Running;

            for (int i = 0; i < Pool.Count; i++)
            {
                if (Pool[i].IsAlive)
                    Running++;
                else
                    Pool.Remove(Pool[i--]);
            }
            return Running;
        }

        public bool this[string Item, ThreadStart x]
        {
            set
            {

                Thread t = this[Item];
                if (t == null)
                {
                    try
                    {
                        if (x == null)
                            throw new System.ArgumentNullException();

                        t = this[Item] = new Thread(x);
                        t.TrySetApartmentState(ApartmentState.MTA);
                        if (value)
                            t.Start();
                    }
                    catch (System.ArgumentNullException)
                    {
                        Console.WriteLine("ThreadMgr: null or malformed parameterized thread start invoked.");
                    }
                    catch (System.Threading.ThreadStateException msg)
                    {
                        Console.WriteLine("ThreadMgr: failed " + msg.Message + " at " + msg.Source + " in " + Item + " context");
                    }
                    catch (Exception msg)
                    {
                        Console.WriteLine("ThreadMgr: unrecoverable fault caused by " + msg.Message + " in " + msg.StackTrace);
                    }
                }
                else
                {
                    // Ionutz: testeaza-ma!
                    MessageBox.Show(null, Item + " context is already locked or request is null; cannot instanciate.\n" +
                        "You may be trying to re-execute the same operation before waiting the previous attempt to complete", 
                        "Threading Manager", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }

        public Thread this[string Item]
        {
            get
            {
                if ((Pool != null) && (Pool.Count > 0))
                {
                    foreach (Thread t in Pool)
                        if (t.Name == Item)
                            return t;
                }
                return null;
            }
            set
            {
                if (Pool == null)
                    Pool = new List<Thread>();
                
                try
                {
                    Pool.Add(value);
                    value.Name = Item;
                }
                catch (Exception msg)
                {
                    Console.WriteLine("ThreadMgr: " + msg.Message + "; " + value.Name + " override attempt with " + Item + " denied.");
                }
            }
        }

        public void Sync(string Item)
        {
            Thread t = this[Item];
            try
            {
                if (t.IsAlive)
                    t.Join();
            }
            catch { }
        }

        #region IDisposable Members

        public void Dispose()
        {
            if ((Pool != null) && (Pool.Count > 0))
            {
                for (int i = 0; i < Pool.Count; i++)
                {
                    if (Pool[i].IsAlive)
                        Pool[i].Abort();

                    Pool.Remove(Pool[i--]);
                }
            }
        }

        #endregion

        public void Recycle(string id)
        {
            try
            {
                foreach (Thread Pooled in Pool)
                {
                    if (Pooled.Name.Contains(id))
                    {
                        Pooled.Abort();
                        break;
                    }
                }
            }
            catch
            { }
        }


        #region IEnumerable Members

        public IEnumerator GetEnumerator()
        {
            foreach (Thread t in Pool)
            {
                yield return t;
            }
        }

        #endregion
    }
}

from anolis.

Dobby233Liu avatar Dobby233Liu commented on August 19, 2024

closed date = 2009-04-10T14:29:58.64-07:00

I solved this by making the PopulateResourceType method check if the .CancelAsync method had been called. Additionally there are two layers of threads from ShowResourceType and PopulateResourceType.

from anolis.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.