锁定的main()线程 [英] Locking main() thread

查看:123
本文介绍了锁定的main()线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不就在这里,但这里的术语,我去基本上我有我的应用程序启动并调用两个线程的main()线程完全确定,一是设置了一个事件处理程序来等待特定的注册表键更改,而其他启动定时器写XML文件每5分钟左右,并持续运行所做的任何更改。我的问题是,一旦这两个方法调用初始化它返回到主并结束程序。我的相关代码段可以在下面找到,所以任何帮助,将不胜感激:

Not exactly sure on the terminology here but here I go basically I have the main() thread of my application that starts and calls two threads, one sets up an event handler to wait for specific registry keys to change, while the other starts a timer to write any changes made to an xml file every 5 mins or so and runs continuously. The issue I have is that once the two methods called are initialized it goes back to main and ends the program. My relevant code sections can be found below, so any help would be appreciated:

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文件和改变事件活路。

Basically all the code works fine I've been testing it in a windows form application but now I need to run it in a standalone app with no interface in the background and need it to keep writing to the xml file and the change event to stay alive.

推荐答案

您方法都会运行一次,那么该线程将退出。有什么可以让他们运行

Your methods will run once, then the thread will exit. There is nothing to keep them running.

试试这个:

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);
    }
}



至于其他海报指出的那样,你还会再需要确保你的主要方法不会退出。制作一个窗口服务好像你的情况来解决这个问题的好办法申请。

As other posters have noted, you will also then need to ensure your main method doesn't exit. Making the application a windows service seems like a good way to solve this in your case.

您可能还需要处理的 ThreadInterruptedException ThreadAbortException 在你的线程

You might also want to handle ThreadInterruptedException and ThreadAbortException on your threads.

如果你真的想进入线程的细节问题,看看这个的免费的C#线程电子书乔阿尔巴哈利的。

And if you really want to get into the nitty gritty of threading, check out this Free C# Threading E-Book by Joe Albahari.

这篇关于锁定的main()线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆