刷新Treeview的最佳方式c#任务计时器
我有一个与树(C#)一起工作的进程(任务)。该树从Postgre数据库加载。这个过程正在倾听事件。当事件发生时,树被更新。刷新Treeview的最佳方式c#任务计时器
与另一个进程(任务),我使用相同的树,以反映使用计时器树视图中的更改。
这很慢。所以,我做错了什么...
我需要帮助,以了解什么是最好的方法来做到这一点,有关书籍的信息,线程,BackgroundWorker,计时器,任务,实时系统的例子,以及等等。
谢谢!
问候。
这是一个类似于我正在开发的代码的例子......有3个类:A,B和C.A是“主”类。它有一个B对象列表和一个B列表线程列表。每个B对象都有一个C类和C线程的列表。
当一个操作完成时,C类已准备就绪(在本例中,将变量“cIsReady”设置为true,当对象B的列表中的所有cObject已准备就绪时,则将“bIsReady”设置为true当列表中的所有的bObjects,对象A的,准备好了,那么“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从未准备好...
谢谢!
这是一个问题或答案? – saeed 2013-05-04 07:12:06
这是我想要做的一个例子。不是答案。 – user2323434 2013-05-15 16:31:46
我正在使用Winform。 – user2323434 2013-04-26 11:06:47
你有交叉线程有问题吗? http://www.codeproject.com/Articles/16024/Cross-Thread – saeed 2013-04-27 10:54:32
第一个进程调用正在监听事件的另一个任务(“次要任务”)。在第一个进程中,我试图知道值的变量在“次要任务”中发生了变化。我是否需要通过引用传递第一个进程和“辅助任务”之间共享的对象?谢谢! – user2323434 2013-04-29 12:19:33