c# - 锁定main()线程

标签 c# multithreading

此处的术语不确定,但我基本上可以使用我的应用程序的main()线程来启动并调用两个线程,一个线程设置一个事件处理程序以等待特定的注册表项更改,而另一个启动计时器每5分钟左右将对xml文件所做的任何更改写入并连续运行。我的问题是,一旦调用的两个方法都初始化,它将返回到main并结束程序。我的相关代码部分可以在下面找到,所以任何帮助将不胜感激:

static void Main(string[] args)
    {
        runner one = new runner();
        runner two = new runner();

        Thread thread1 = new Thread(new ThreadStart(one.TimerMeth));
        Thread thread2 = new Thread(new ThreadStart(two.start));

        thread1.Start();
        thread2.Start();

        thread1.Join();
        thread2.Join();
    }

public void TimerMeth()
    {
        System.Timers.Timer timer = new System.Timers.Timer();

        timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);

        timer.Interval = 300000;

        timer.Enabled = true;
    }

    private void OnElapsedTime(object source, ElapsedEventArgs e)
    {
        file write = new file();
        write.write(RegKeys);
    }

public void start()
        {
            if (File.Exists("C:\\test.xml"))
            {
                file load = new file();
                RegKeys = load.read(RegKeys);
            }

            string hiveid = "HKEY_USERS";
            WindowsIdentity identity = WindowsIdentity.GetCurrent();
            string id = identity.User.ToString();

            string key1 = id + "\\\\Software\\\\Microsoft\\\\Windows NT\\\\CurrentVersion\\\\Windows Messaging Subsystem\\\\Profiles\\\\Outlook\\\\0a0d020000000000c000000000000046";
            List<string> value1 = new List<String> { "01020402", "test" };

            valuechange = new RegistryValueChange(hiveid, key1, value1);
            valuechange.RegistryValueChanged += new EventHandler<RegistryValueChangedEventArgs>(valuechange_RegistryValueChanged);

            try
            {
                valuechange.Start();

            }
            catch
            {
                StreamWriter ut;
                ut = File.AppendText("C:\\test.txt");
                ut.WriteLine("error occured in starting management");
                ut.Close();
            }

            file test = new file();
            test.checkfile("C:\\test.xml");

        }

void valuechange_RegistryValueChanged(object sender, RegistryValueChangedEventArgs e)
        {
// deals with the returned values
}

基本上所有代码都可以正常工作,我已经在Windows窗体应用程序中对其进行了测试,但是现在我需要在一个没有后台界面的独立应用程序中运行它,并且需要它继续写入xml文件和change事件,以便保留活。

最佳答案

您的方法将运行一次,然后线程将退出。没有什么可以让它们继续运行。

试试这个:

thread1.IsBackground = true;
thread2.IsBackground = true;

public void start()
{
 while(true)
 {
    // ... do stuff
    Thread.Sleep(1000*60*5) // sleep for 5 minutes
 }
}

public void TimerMeth()
{
    while(true)
    {
        file write = new file();
        write.write(RegKeys);

        Thread.Sleep(30000);
    }
}

正如其他张贴者所指出的那样,您还需要确保主方法不会退出。使应用程序成为Windows服务似乎是解决此问题的一种好方法。

您可能还想在线程上处理ThreadInterruptedExceptionThreadAbortException

如果您真的想深入了解线程的细节,请查看Joe Albahari的Free C# Threading E-Book

关于c# - 锁定main()线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1722932/

相关文章:

c# - 使用Windows搜索文件API

java - Collections.synchronizedList 与 vector

python - 为什么 `gevent.spawn` 与 monkeypatched `threading.Thread()` 不同?

java - Java 中的线程和事件处理

python - 如何在单个 PyQt GUI 实例中多处理多个绘图

c - 在 C 中实现线程屏障和屏障重置的正确方法是什么?

C# WPF - 切换某些内容的可见性不起作用

c# - 查询在 phpmyadmin 中工作在 C# 中不起作用

c# - LINQ - 左连接

c# - 连接中的 Entity Framework 子查询