I'm working on a WPF-MVVM project and I need to implement asynchronous infinite loops in some background threads. What I have done in the ViewModel is
 public TestVM()
 {
    LineIO_Task();
    //some other work
 }
and LineIO_Task is defined as
public void LineIO_Task()
{
    for (int i = 0; i < 7; i++)
    {
         Task GetP = new Task(() => { EnPost(Lines[i]); }, TaskCreationOptions.LongRunning);
         GetP.Start();
    }
}
Lines is an ObservableCollection that is initialized in TestVm. And EnPost is defined as
public async void EnPost(Line l)
{           
    int last = 0;               
    while (true)
    {
        // do the work in the loop
        int pno = l.Com.ReadPostNo();//read a serial port
        if (pno != 0 && pno != last)
        {
            log.WriteLog(pno + " to " + l.ToString());
            Dispatcher.Invoke(() =>
            {   
                // update the UI                     
                l.Posts.First(x => x.IsValid).Num = pno;
                l.Posts.First(x => x.IsValid).IsValid = false;
                LocalDb.InsertPost(l.Num, AssignedPost.ToList().Find(x => x.Num == pno));                            
            });
            pno = last;
        } 
        await Task.Delay(500);                   
    }        
}
I've tried Task.Run(() => Method()), Task.Factory.StartNew(() => Method()),,async Task EnPost() and using a System.Timers.Timer. But no matter which way I use, the EnPost method just doesn't run. I put break-points in the method. It doesn't hit there. Am I using Task wrong?
 
    