0

我有一个与树(C#)一起工作的进程(任务)。该树是从 postgre 数据库加载的。该进程正在监听事件。当事件发生时,树会更新。

对于另一个进程(任务),我使用相同的树,以使用计时器反映 Treeview 中的更改。

它很慢。所以有些事情我做错了......

我需要帮助来了解执行此操作的最佳方法、有关书籍的信息、线程、BackgroundWorker、计时器、任务、实时系统等的示例。

谢谢!

问候。

4

1 回答 1

0

这是一个类似于我正在开发的代码的示例......有 3 个类:A、B 和 C。A 是“主”类。它有一个 B 个对象的列表和一个 B 个线程的列表。每个 B 对象都有一个 C 类和 C 线程列表。

C 类在操作完成时准备好(在示例中,将变量“cIsReady”设置为 true。当对象 B 的列表中的所有 cObjects 都准备好时,将“bIsReady”设置为 true。当所有对象 A 的 List 中的 bObjects 已准备就绪,然后将“aIsReady”设置为 true。

public class A
{
    private List<B> bList;

    private List<Thread> threadBList;

    private bool aIsReady;




    /// <summary>
    /// List of classes managed by A Class.
    /// </summary>
    public List<B> BList
    {
        get
        {
            return bList;
        }
        set
        {
            bList = value;
        }
    }

    /// <summary>
    /// List of Threads. Each Thread manages a B Class.
    /// </summary>
    public List<Thread> ThreadBList
    {
        get
        {
            return threadBList;
        }
        set
        {
            threadBList = value;
        }
    }

    /// <summary>
    /// Indicates when A is ready.
    /// </summary>
    public bool AIsReady
    {
        get
        {
            return aIsReady;
        }
        set
        {
            aIsReady = value;
        }
    }




    /// <summary>
    /// Constructor.
    /// </summary>
    public A()
    { 

    }



    /// <summary>
    /// Starts the A Class.
    /// </summary>
    public void startA()
    {
        this.bList = new List<B>();

        this.threadBList = new List<Thread>();

        // for example
        int numberOfBClasses = 3;


        for (int i = 0; i < numberOfBClasses; ++i)
        {
            B bObject = new B();
            this.bList.Add(bObject);

            Thread bThread = new Thread(bObject.startB);
            bThread.IsBackground = true;

            this.threadBList.Add(bThread);


        }   // for (int i = 0; i < numberOfBClasses; ++i)

        // Start all the B Threads.
        for (int i = 0; i < numberOfBClasses; ++i)
        {
            this.threadBList[i].Start();
        }   // for (int i = 0; i < numberOfBClasses; ++i)


        while (!aIsReady)
        {
            foreach (B bObject in this.bList)
            {
                if (bObject.BIsReady)
                {
                    this.aIsReady = true;
                }   // if (bObject.BIsReady)
                else
                {
                    this.aIsReady = false;
                }   // else [ if (bObject.BIsReady) ]
            }   // foreach (B bObject in this.bList)
        }   // while (!aIsReady)

        this.aIsReady = true;



    }



}   // public class A







public class B
{
    private List<C> cList;

    private List<Thread> threadCList;

    private bool bIsReady;










    /// <summary>
    /// List of classes managed by B Class.
    /// </summary>
    public List<C> CList
    {
        get
        {
            return cList;
        }
        set
        {
            cList = value;
        }
    }

    /// <summary>
    /// List of Threads. Each Thread manages a C Class.
    /// </summary>
    public List<Thread> ThreadCList
    {
        get
        {
            return threadCList;
        }
        set
        {
            threadCList = value;
        }
    }

    /// <summary>
    /// Indicates when B is ready.
    /// </summary>
    public bool BIsReady
    {
        get
        {
            return bIsReady;
        }
        set
        {
            bIsReady = value;
        }
    }




    /// <summary>
    /// Constructor
    /// </summary>
    public B()
    { 

    }


    /// <summary>
    /// Start B
    /// </summary>
    public void startB()
    {
        this.cList = new List<C>();

        this.threadCList = new List<Thread>();

        // for example
        int numberOfCClasses = 5;


        for (int i = 0; i < numberOfCClasses; ++i)
        {
            C cObject = new C();
            this.cList.Add(cObject);

            Thread cThread = new Thread(cObject.startC);
            cThread.IsBackground = true;

            this.threadCList.Add(cThread);


        }   // for (int i = 0; i < numberOfCClasses; ++i)

        // Start all the C Threads.
        for (int i = 0; i < numberOfCClasses; ++i)
        {
            this.threadCList[i].Start();
        }   // for (int i = 0; i < numberOfCClasses; ++i)

        while (!bIsReady)
        {
            foreach (C cObject in this.cList)
            {
                if (cObject.CIsReady)
                {
                    this.bIsReady = true;
                }   // if (cObject.CIsReady)
                else
                {
                    this.bIsReady = false;
                }   // else [ if (cObject.CIsReady) ]
            }   // foreach (C in this.cList)
        }   // while (!bIsReady)

        this.bIsReady = true;
    }


}   // public class B






public class C
{
    private bool cIsReady;




    /// <summary>
    /// Indicates that the object is ready.
    /// </summary>
    public bool CIsReady
    {
        get
        {
            return cIsReady;
        }
        set
        {
            cIsReady = value;
        }
    }






    /// <summary>
    /// Constructor.
    /// </summary>
    public C()
    { 

    }

    /// <summary>
    /// Starts C.
    /// </summary>
    public void startC()
    {
        this.cIsReady = true;          
    }

}   // public class C

因此,当我将以下代码放在下面时,例如表单加载事件:

A aObject = new A();

        Thread aThread = new Thread(aObject.startA);

        while (!aObject.AIsReady)
        {
            Thread.Sleep(100);
        }

        MessageBox.Show("A is Ready");

aObject 永远不会准备好......

谢谢!

于 2013-04-30T17:07:33.047 回答