C# 使用线程连续检查值的更改
我正在尝试创建一个线程,该线程将持续检查某个值的更改,然后在GUI中的PictureBox中直观地显示该更改 我实际上写的东西有点复杂,所以我在保留基本想法的同时简化了它,如果这还不够,我很乐意提供澄清:C# 使用线程连续检查值的更改,c#,multithreading,continuous,C#,Multithreading,Continuous,我正在尝试创建一个线程,该线程将持续检查某个值的更改,然后在GUI中的PictureBox中直观地显示该更改 我实际上写的东西有点复杂,所以我在保留基本想法的同时简化了它,如果这还不够,我很乐意提供澄清: public class CheckPictures { PictureBox update; List<String> check; public CheckPictures(PictureBox anUpdate, List<String>
public class CheckPictures
{
PictureBox update;
List<String> check;
public CheckPictures(PictureBox anUpdate, List<String> aCheck)
{
update = anUpdate;
check = aCheck;
}
public void start()
{
while(true)
{
if (aCheck[0] == "Me")
{
update.Image = Image.fromFile("");
}
}
}
}
static int Main(string[] args)
{
List<String> picturesList = new List<String>();
CheckPictures thread1 = new CheckPictures(PictureBox1, picturesList);
Thread oThread1 = new Thread(thread1.start));
}
公共类检查图片
{
图片盒更新;
清单检查;
公开检查图片(图片盒更新,列表检查)
{
更新=更新;
检查=检查;
}
公开作废开始()
{
while(true)
{
if(aCheck[0]=“Me”)
{
update.Image=Image.fromFile(“”);
}
}
}
}
静态int Main(字符串[]args)
{
列表图片列表=新列表();
CheckPictures thread1=新的CheckPictures(PictureBox1,picturesList);
thread1=新线程(thread1.start));
}
如果要将字符串“Me”添加到pictureList,我希望它能够动态更改PictureBox1中的图片。上面的代码不像我希望的那样工作。我原以为,通过传递实际的PictureBox和List,对其他地方的List的任何更改都会被线程捕获。所以我的第一个问题是:这可能吗?如果是这样,我需要对代码做什么更改才能实现它?您肯定不想执行无限循环,这只会消耗cpu:
while(true)
{
if (aCheck[0] == "Me")
{
update.Image = Image.fromFile("");
}
}
我认为你应该调查一下这个班
这里的基本思想是,您需要在线程上停止执行一段时间,并在满足某个条件时(可能在另一个线程上)恢复执行
换句话说,您将拥有一个计数器,在特定条件下减小其值,每当它变为零时,您就会触发事件,执行一些代码,然后重新开始(停止执行并等待计数器变为零)
在您的情况下,您可以将计数器设置为1,并在设置aCheck[0]=“Me”时减小其值代码>这样就不会浪费CPU
伪代码:
初始化计数器:
CountdownLatch latch = new CountdownLatch(1);
使线程等待:
public void start()
{
while(true)
{
latch.Wait(); //execution stops
{
//execution resumes once the latch counter is zero.
if (aCheck[0] == "Me") //double check you have what you need
{
update.Image = Image.fromFile("");
latch = new CountdownLatch(1); //reset if you need to do it again
}
}
}
}
只要满足您的条件(即aCheck[0]=“Me”
),就向您的闩锁发出信号:
latch.Signal();
最后一行将使线程恢复执行。好东西。你肯定不想做无限循环,这只会消耗cpu:
while(true)
{
if (aCheck[0] == "Me")
{
update.Image = Image.fromFile("");
}
}
我认为你应该调查一下这个班
这里的基本思想是,您需要在线程上停止执行一段时间,并在满足某个条件时(可能在另一个线程上)恢复执行
换句话说,您将拥有一个计数器,在特定条件下减小其值,每当它变为零时,您就会触发事件,执行一些代码,然后重新开始(停止执行并等待计数器变为零)
在您的情况下,您可以将计数器设置为1,并在设置aCheck[0]=“Me”时减小其值代码>这样就不会浪费CPU
伪代码:
初始化计数器:
CountdownLatch latch = new CountdownLatch(1);
使线程等待:
public void start()
{
while(true)
{
latch.Wait(); //execution stops
{
//execution resumes once the latch counter is zero.
if (aCheck[0] == "Me") //double check you have what you need
{
update.Image = Image.fromFile("");
latch = new CountdownLatch(1); //reset if you need to do it again
}
}
}
}
只要满足您的条件(即aCheck[0]=“Me”
),就向您的闩锁发出信号:
latch.Signal();
最后一行将使线程恢复执行。好东西。您可能需要使用事件。您注册了一个eventhandler,当一个线程中的某些内容发生更改时,它会调用另一个线程中的事件处理程序来执行该工作。忙等待会浪费cpu。您可能需要使用事件。您注册了一个eventhandler,当一个线程中的某些内容发生更改时,它会调用另一个线程中的事件处理程序来执行该工作。繁忙的等待会浪费cpu。创建一些对象,当添加新图片时,这些对象将引发事件。例如,代表图片集合的类:
public class PicturesCollection
{
public event EventHandler<PictureAddedEventArgs> PictureAdded;
private List<string> _pictures = new List<string>();
public void Add(string name)
{
_pictures.Add(name);
if (PictureAdded != null)
PictureAdded(this, new PictureAddedEventArgs(name));
}
public IEnumerable<string> Pictures
{
get { return _pictures; }
}
}
您现在所需的一切-创建图片集并订阅该活动:
static int Main(string[] args)
{
PicturesCollection pictures = new PicturesCollection();
pictures.PictureAdded += Pictures_PictureAdded;
}
static void Pictures_PictureAdded(object sender, PictureAddedEventArgs e)
{
if (e.Name == "Me")
PictureBox1.Image = Image.fromFile("");
}
如果您在应用程序的某个位置向集合添加新图片,它将引发PictureReaded事件,您可以处理并更新PictureBox。在这种情况下不会浪费CPU。创建一些对象,当添加新图片时,该对象将引发事件。例如,代表图片集合的类:
public class PicturesCollection
{
public event EventHandler<PictureAddedEventArgs> PictureAdded;
private List<string> _pictures = new List<string>();
public void Add(string name)
{
_pictures.Add(name);
if (PictureAdded != null)
PictureAdded(this, new PictureAddedEventArgs(name));
}
public IEnumerable<string> Pictures
{
get { return _pictures; }
}
}
您现在所需的一切-创建图片集并订阅该活动:
static int Main(string[] args)
{
PicturesCollection pictures = new PicturesCollection();
pictures.PictureAdded += Pictures_PictureAdded;
}
static void Pictures_PictureAdded(object sender, PictureAddedEventArgs e)
{
if (e.Name == "Me")
PictureBox1.Image = Image.fromFile("");
}
如果您在应用程序的某个位置向集合添加新图片,它将引发PictureReaded事件,您可以处理并更新PictureBox。在这种情况下不会浪费CPU。谢谢大家!很有帮助,更不用说快了!回答得好!做需要做的事情,而不需要安全、多线程编程的微妙之处。谢谢大家!很有帮助,更不用说快了!回答得好!在没有安全、多线程编程的微妙之处的情况下执行请求。