Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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/4/json/13.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#_Wpf - Fatal编程技术网

C# 在线程中动态定义事件

C# 在线程中动态定义事件,c#,wpf,C#,Wpf,我创建了一个线程,并在其上创建了一个Dispatchermer。在这个线程中,我为Tick事件定义了一个方法,但它从未发生过 class Net { List<DispatcherTimer> Timers; Thread thread; Net() { thread = new Thread(method); Timers=new List<DispatcherTimer>(); thre

我创建了一个线程,并在其上创建了一个Dispatchermer。在这个线程中,我为Tick事件定义了一个方法,但它从未发生过

class Net
{
    List<DispatcherTimer> Timers;
    Thread thread;

    Net()
    {
        thread = new Thread(method);
        Timers=new List<DispatcherTimer>();
        thread.Start();
    }

    void method()
    {
        while(true)
            if(condition)
            {
                Timers.Add(new DispatcherTimer());
                Timers[Timers.Count - 1] = new DispatcherTimer();
                Timers[Timers.Count - 1].Interval = new TimeSpan(2000000);
                Timers[Timers.Count - 1].Tick += new EventHandler(Timers_Tick);
                Timers[Timers.Count - 1].IsEnabled = true;
                Timers[Timers.Count - 1].Start();
            }
    }
    private void Timers_Tick(object sender, EventArgs e)
    {
        //Some Code
    }
}
类网络
{
列出计时器;
螺纹;
净额()
{
线程=新线程(方法);
计时器=新列表();
thread.Start();
}
void方法()
{
while(true)
如果(条件)
{
Timers.Add(newdispatchermer());
Timers[Timers.Count-1]=新的调度程序();
计时器[Timers.Count-1]。间隔=新的时间跨度(2000000);
Timers[Timers.Count-1]。Tick+=新事件处理程序(Timers\u Tick);
计时器[Timers.Count-1]。IsEnabled=true;
计时器[Timers.Count-1].Start();
}
}
私有无效计时器(对象发送方、事件参数)
{
//一些代码
}
}

编辑:是的,在我的原始代码中,我启动了线程。

dispatchermer
是指在UI线程上运行
,但您试图从后台线程创建它,该线程的调度程序与UI调度程序不同

因此,您应该将UI dispatcher传递给该方法,并将该dispatcher用于dispatcher对象-

thread = new Thread(() => method(App.Current.Dispatcher));

void method(Dispatcher dispatcher)
{
   ....
   Timers.Add(new DispatcherTimer(DispatcherPriority.Background, dispatcher));
   Timers[Timers.Count - 1] = new DispatcherTimer(DispatcherPriority.Background,
                                                  dispatcher);
   ...
}

或者您应该使用而不是。

对于多行while语句,您需要使用
{
}
。如下所示:while(condition){//insert your code here}您正在
while(true)…
语句中运行一个繁忙的等待循环。请解释您试图实现的目标,从您的代码中很难理解。@jsve他的代码没有大括号是完全有效的。只有当您希望将
while
循环应用于多个语句而不是多行时,才需要在语句周围使用大括号(
{}
)。因为在本例中,它应用于单个
if
语句(它本身应用于一个代码块),所以它是完全有效的。其他问题,比如他的循环是否有意义,或者为了清晰起见,您是否可能希望包含大括号,都是独立的。