Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 试图在计时器内读取XML,但它';它不工作了_C#_Xml_Timer - Fatal编程技术网

C# 试图在计时器内读取XML,但它';它不工作了

C# 试图在计时器内读取XML,但它';它不工作了,c#,xml,timer,C#,Xml,Timer,我想每5秒打印一次数字1到6,当它到达列表末尾时停止计时器 打印结果: private static void Main(string[] args) { List<int> l = new List<int>() {1,2,3,4,5,6}; XElement xdoc = XElement.Load("../../XMLFile1.xml"); xdoc.Element("num").Value = "0"; xdoc.

我想每5秒打印一次数字1到6,当它到达列表末尾时停止计时器

打印结果:

private static void Main(string[] args)
{        
    List<int> l = new List<int>() {1,2,3,4,5,6};
    XElement xdoc = XElement.Load("../../XMLFile1.xml");
    xdoc.Element("num").Value = "0";
    xdoc.Element("max").Value = l.Count.ToString();
    xdoc.Save("../../XMLFile1.xml");
    Timer t = new Timer(printnum, null, 0, 5000);           
}

public static void printnum(Object o)
{
    try
    {
        XElement xdoc = XElement.Load("../../XMLFile1.xml");
        int num = int.Parse(xdoc.Element("num").Value);
        int max = int.Parse(xdoc.Element("max").Value);
        if (num<max)
        {
            Console.WriteLine(num);
            num += 1;
            xdoc.Element("num").Value = num.ToString();
            xdoc.Save("../../XMLFile1.xml");
        }          
    }
    catch (Exception e)
    {

    }
}
1
2
3
4
5
六,

代码:

private static void Main(string[] args)
{        
    List<int> l = new List<int>() {1,2,3,4,5,6};
    XElement xdoc = XElement.Load("../../XMLFile1.xml");
    xdoc.Element("num").Value = "0";
    xdoc.Element("max").Value = l.Count.ToString();
    xdoc.Save("../../XMLFile1.xml");
    Timer t = new Timer(printnum, null, 0, 5000);           
}

public static void printnum(Object o)
{
    try
    {
        XElement xdoc = XElement.Load("../../XMLFile1.xml");
        int num = int.Parse(xdoc.Element("num").Value);
        int max = int.Parse(xdoc.Element("max").Value);
        if (num<max)
        {
            Console.WriteLine(num);
            num += 1;
            xdoc.Element("num").Value = num.ToString();
            xdoc.Save("../../XMLFile1.xml");
        }          
    }
    catch (Exception e)
    {

    }
}

您的主要问题是应用程序在
main
方法存在后终止。这是因为计时器将在不同的线程上执行代码。要解决您的问题,只需通过执行类似于从控制台读取一行内容的操作来暂停主线程:

private static void Main(string[] args)
{        
    List<int> l = new List<int>() {1,2,3,4,5,6};
    XElement xdoc = XElement.Load("../../XMLFile1.xml");
    xdoc.Element("num").Value = "0";
    xdoc.Element("max").Value = l.Count.ToString();
    xdoc.Save("../../XMLFile1.xml");
    Timer t = new Timer(printnum, null, 0, 5000);    

    Console.ReadLine(); //Read a line from the console which forces the main thread to wait
}
或者更简单,您不需要计时器:

private static void Main(string[] args)
{
    for (int number = 1; number <= 6; number ++)
    {
        Console.WriteLine(number);
        Thread.Sleep(5000);
    }
}
private static void Main(字符串[]args)
{

对于(int number=1;number,我也不确定您试图做什么,我确定计时器没有触发,因为它在main结束时被删除。它只是一个局部变量。我还发现了一些其他问题,并尝试修复它:

class Test
{
    Timer timer = new Timer();

    public void DoIt()
    {
        List<int> l = new List<int>() { 1, 2, 3, 4, 5, 6 };
        XElement xdoc = XElement.Load("../../XMLFile1.xml");
        xdoc.Element("num").Value = "0";
        xdoc.Element("max").Value = l.Count.ToString();
        xdoc.Save("../../XMLFile1.xml");

        timer.Interval = 5000;
        timer.Tick += printnum;
        timer.Enabled = true;
    }

    public void printnum(object sender, EventArgs ev)
    {
        try
        {
            XElement xdoc = XElement.Load("../../XMLFile1.xml");
            int num = int.Parse(xdoc.Element("num").Value);
            int max = int.Parse(xdoc.Element("max").Value);
            num += 1;
            Console.WriteLine(num);
            if (num < max)
            {
                xdoc.Element("num").Value = num.ToString();
                xdoc.Save("../../XMLFile1.xml");
            }
            else
            {
                Console.WriteLine("Finish");
                timer.Enabled = false;
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
}
类测试
{
定时器=新定时器();
公共无效DoIt()
{
List l=新列表(){1,2,3,4,5,6};
XElement xdoc=XElement.Load(“../../XMLFile1.xml”);
xdoc.Element(“num”).Value=“0”;
xdoc.Element(“max”).Value=l.Count.ToString();
保存(“../../XMLFile1.xml”);
定时器间隔=5000;
timer.Tick+=printnum;
timer.Enabled=true;
}
public void printnum(对象发送方,事件参数ev)
{
尝试
{
XElement xdoc=XElement.Load(“../../XMLFile1.xml”);
int num=int.Parse(xdoc.Element(“num”).Value);
int max=int.Parse(xdoc.Element(“max”).Value);
num+=1;
控制台写入线(num);
如果(数值<最大值)
{
xdoc.Element(“num”).Value=num.ToString();
保存(“../../XMLFile1.xml”);
}
其他的
{
控制台。写入线(“完成”);
timer.Enabled=false;
}
}
捕获(例外e)
{
控制台写入线(e.Message);
}
}
}

希望这对……有帮助。

我会这样做

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static AutoResetEvent autoEvent = new AutoResetEvent(false);

        const string FILENAME = @"c:\temp\test.xml";
        private static void Main(string[] args)
        {
            State state = new State();
            state.l  = new List<int>() { 1, 2, 3, 4, 5, 6 };
            state.index = 0;
            state.xdoc = XDocument.Load(FILENAME);
            state.num = state.xdoc.Descendants("num").FirstOrDefault();
            state.xdoc.Descendants("max").FirstOrDefault().Value = state.l.LastOrDefault().ToString();
            Timer t = new Timer(printnum, state, 0, 5000);
            autoEvent.WaitOne();
            t.Dispose();

        }
        public class State
        {
            public XElement num { get; set; }
            public XDocument xdoc { get; set; }
            public List<int> l { get; set; }
            public int index { get; set; }
        }

        public static void printnum(Object o)
        {
            State state = o as State;
            try
            {
                int num = state.l[state.index++];
                Console.WriteLine(num);
                state.num.Value = num.ToString();
                state.xdoc.Save(FILENAME);
                if (state.index >= state.l.Count)
                {
                    autoEvent.Set();
                }
            }
            catch (Exception e)
            {

            }
        }
    }
}
​
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
使用系统线程;
命名空间控制台应用程序1
{
班级计划
{
静态自动恢复事件自动事件=新自动恢复事件(假);
常量字符串文件名=@“c:\temp\test.xml”;
私有静态void Main(字符串[]args)
{
状态=新状态();
state.l=新列表(){1,2,3,4,5,6};
state.index=0;
state.xdoc=XDocument.Load(文件名);
state.num=state.xdoc.substands(“num”).FirstOrDefault();
state.xdoc.substands(“max”).FirstOrDefault().Value=state.l.LastOrDefault().ToString();
定时器t=新定时器(printnum,state,0,5000);
autoEvent.WaitOne();
t、 处置();
}
公共阶级国家
{
公共XElement num{get;set;}
公共XDocument xdoc{get;set;}
公共列表l{get;set;}
公共int索引{get;set;}
}
公共静态void printnum(对象o)
{
状态=o为状态;
尝试
{
int num=state.l[state.index++];
控制台写入线(num);
state.num.Value=num.ToString();
state.xdoc.Save(文件名);
如果(state.index>=state.l.Count)
{
autoEvent.Set();
}
}
捕获(例外e)
{
}
}
}
}
​

您到底想做什么?为什么需要xml文件?我想每5秒打印一到六个数字,如何在func printnum()中知道下一个要打印的数字?因此每5秒打印一个新的数字?您可以添加为什么此代码当前不起作用/出现错误吗?当执行线程.Sleep(5000)时执行其他程序保持运行?哪个其他程序?你能解释一下吗?如果我在我的网站上实现循环,该网站将保持运行和工作?这是一个网站吗?这似乎是一个控制台应用程序。我想在我的网站上实现此功能
class Test
{
    Timer timer = new Timer();

    public void DoIt()
    {
        List<int> l = new List<int>() { 1, 2, 3, 4, 5, 6 };
        XElement xdoc = XElement.Load("../../XMLFile1.xml");
        xdoc.Element("num").Value = "0";
        xdoc.Element("max").Value = l.Count.ToString();
        xdoc.Save("../../XMLFile1.xml");

        timer.Interval = 5000;
        timer.Tick += printnum;
        timer.Enabled = true;
    }

    public void printnum(object sender, EventArgs ev)
    {
        try
        {
            XElement xdoc = XElement.Load("../../XMLFile1.xml");
            int num = int.Parse(xdoc.Element("num").Value);
            int max = int.Parse(xdoc.Element("max").Value);
            num += 1;
            Console.WriteLine(num);
            if (num < max)
            {
                xdoc.Element("num").Value = num.ToString();
                xdoc.Save("../../XMLFile1.xml");
            }
            else
            {
                Console.WriteLine("Finish");
                timer.Enabled = false;
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        static AutoResetEvent autoEvent = new AutoResetEvent(false);

        const string FILENAME = @"c:\temp\test.xml";
        private static void Main(string[] args)
        {
            State state = new State();
            state.l  = new List<int>() { 1, 2, 3, 4, 5, 6 };
            state.index = 0;
            state.xdoc = XDocument.Load(FILENAME);
            state.num = state.xdoc.Descendants("num").FirstOrDefault();
            state.xdoc.Descendants("max").FirstOrDefault().Value = state.l.LastOrDefault().ToString();
            Timer t = new Timer(printnum, state, 0, 5000);
            autoEvent.WaitOne();
            t.Dispose();

        }
        public class State
        {
            public XElement num { get; set; }
            public XDocument xdoc { get; set; }
            public List<int> l { get; set; }
            public int index { get; set; }
        }

        public static void printnum(Object o)
        {
            State state = o as State;
            try
            {
                int num = state.l[state.index++];
                Console.WriteLine(num);
                state.num.Value = num.ToString();
                state.xdoc.Save(FILENAME);
                if (state.index >= state.l.Count)
                {
                    autoEvent.Set();
                }
            }
            catch (Exception e)
            {

            }
        }
    }
}
​