Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.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# 计时器访问类字段_C#_Multithreading_Timer - Fatal编程技术网

C# 计时器访问类字段

C# 计时器访问类字段,c#,multithreading,timer,C#,Multithreading,Timer,是否有任何可能的方法访问类程序中的字段str和main函数中的变量num class Program { string str = "This is a string"; static void Main(string[] args) { int num = 100; Debug.WriteLine(Thread.CurrentThread.ManagedThreadId); var timer = new System.T

是否有任何可能的方法访问类程序中的字段str和main函数中的变量num

class Program
{
    string str = "This is a string";
    static void Main(string[] args)
    {
        int num = 100;
        Debug.WriteLine(Thread.CurrentThread.ManagedThreadId);
        var timer = new System.Timers.Timer(10000);
        timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
        timer.Start();
        for (int i = 0; i < 20; i++)
        {
            Debug.WriteLine(Thread.CurrentThread.ManagedThreadId + " " + "current I is " + i.ToString());
            Thread.Sleep(1000);
        }
        Console.ReadLine();
    }

    static void timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        Debug.WriteLine(str);
        Debug.WriteLine(num);
        Debug.WriteLine(Thread.CurrentThread.ManagedThreadId + "  current is timer");
        //throw new NotImplementedException();
    }
}
类程序
{
string str=“这是一个字符串”;
静态void Main(字符串[]参数)
{
int num=100;
Debug.WriteLine(Thread.CurrentThread.ManagedThreadId);
var定时器=新系统定时器定时器定时器(10000);
timer.appeased+=新的ElapsedEventHandler(timer\u appeased);
timer.Start();
对于(int i=0;i<20;i++)
{
Debug.WriteLine(Thread.CurrentThread.ManagedThreadId++“当前I为”+I.ToString());
睡眠(1000);
}
Console.ReadLine();
}
静态无效计时器(对象发送器,ElapsedEventArgs e)
{
Debug.WriteLine(str);
Debug.WriteLine(num);
Debug.WriteLine(Thread.CurrentThread.ManagedThreadId+“current is timer”);
//抛出新的NotImplementedException();
}
}

致以最诚挚的问候,

只需填写字段即可

要访问
num
,您需要使用

您也可以使用

还有一个选择。该类允许您传递状态对象

var timer = new System.Threading.Timer((state) =>
{
     Debug.WriteLine(str);
     Debug.WriteLine(state);
     Debug.WriteLine(Thread.CurrentThread.ManagedThreadId + "  current is timer");
}, num, 10000, 10000);

str应该可以直接访问,如果您将其更改为静态,因为它现在是在类级别实现的。Num是main的内部对象,因此除非传递对它的引用,否则无法访问它。您可以将其移动到main外部,或者如果ElapsedEventArgs中支持它,则可以通过这种方式传递对它的引用并检索它。

刚刚选中,看起来ElapsedEventArgs没有要保存的对象状态。您需要提供一个全局引用(将num移动到类级别)或提供一个引用指针,该指针可以使用委托或类似方法访问它。我没有考虑使用匿名方法。如果您感兴趣,您可能需要了解闭包。
timer.Elapsed += new ElapsedEventHandler((s, e) =>
{
     Debug.WriteLine(str);
     Debug.WriteLine(num);
     Debug.WriteLine(Thread.CurrentThread.ManagedThreadId + "  current is timer");

});
timer.Elapsed += new ElapsedEventHandler(delegate(object sender, ElapsedEventArgs e)
{
     Debug.WriteLine(str);
     Debug.WriteLine(num);
     Debug.WriteLine(Thread.CurrentThread.ManagedThreadId + "  current is timer");

});
var timer = new System.Threading.Timer((state) =>
{
     Debug.WriteLine(str);
     Debug.WriteLine(state);
     Debug.WriteLine(Thread.CurrentThread.ManagedThreadId + "  current is timer");
}, num, 10000, 10000);