本文介绍了如何让在&QUOT方法运行;背景" (线程?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前,我有一些code到一个文本文件中循环寻找一个特定的短语。然而,这种方法运行时,整个应用程序锁定。我想是因为它的循环,这正是我想要的。
我想这样的事情发生在后台,所以正常的方法和用户与应用程序交互还是可以完成的。
如何才能做到这一点/改进?
私人无效CheckLog()
{
而(真)
{
//让拿到一个破发
Thread.sleep代码(5000);
如果(!File.Exists(Command.bat))
{
继续;
}
使用(StreamReader的SR = File.OpenText(Command.bat))
{
字符串s =;
而((S = sr.ReadLine())!= NULL)
{
如果(s.Contains(MP4:生产/))
{
//输出它
的MessageBox.show(多个);
}
}
}
}
}
解决方案
使用
类Foo {
私人螺纹螺纹;
私人无效CheckLog(){...}
私人无效StartObserving(){
线程=新主题(this.CheckLog);
thread.Start();
}
}
或从调色板中BackgroundWorker组件。
I currently have some code that cycles through a text file looking for a specific phrase. However, when this method runs, the whole application locks up. I assume because it's looping, which is what I want.
I would like this to happen in the background, so normal methods and user interaction with the application can still be done.
How can this be done/improved?
private void CheckLog()
{
while (true)
{
// lets get a break
Thread.Sleep(5000);
if (!File.Exists("Command.bat"))
{
continue;
}
using (StreamReader sr = File.OpenText("Command.bat"))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
if (s.Contains("mp4:production/"))
{
// output it
MessageBox.Show(s);
}
}
}
}
}
解决方案
Use
class Foo {
private Thread thread;
private void CheckLog() {...}
private void StartObserving() {
thread = new Thread(this.CheckLog);
thread.Start();
}
}
or the backgroundworker component from the palette.
这篇关于如何让在&QUOT方法运行;背景" (线程?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!