刷新Treeview c#任务计时器的最佳方法

时间:2013-04-26 10:56:49

标签: c# .net winforms treeview task

我有一个与树(C#)一起使用的进程(Task)。该树是从postgre数据库加载的。这个过程正在倾听事件。当事件发生时,树会更新。

使用另一个进程(Task),我使用相同的Tree,以使用Timer反映Treeview中的更改。

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

我需要帮助才能知道执行此操作的最佳方法是什么,有关书籍的信息,Thread,BackgroundWorker,Timer,Task,Real Time Systems等示例。

谢谢!

问候。

1 个答案:

答案 0 :(得分:0)

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

当操作完成时C类就绪(在示例中,将变量“cIsReady”设置为true。当列表中的所有cObject(对象B)都准备就绪时,“bIsReady”设置为true。当对象A的列表中的所有bObject都准备就绪时,则“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永远不会准备好......

谢谢!

相关问题