C# 是否允许读取Backgroundtask中的字段或属性?
背景 我有两个程序,它们都将通过命名管道进行通信,基于外部应用程序的当前状态,我的主应用程序将等待,但不会阻塞我的UI 在本教程()中,我必须使用调度器访问UI元素,但我使用MVVM模式将属性绑定到UI 问题:C# 是否允许读取Backgroundtask中的字段或属性?,c#,wpf,C#,Wpf,背景 我有两个程序,它们都将通过命名管道进行通信,基于外部应用程序的当前状态,我的主应用程序将等待,但不会阻塞我的UI 在本教程()中,我必须使用调度器访问UI元素,但我使用MVVM模式将属性绑定到UI 问题: 我的示例中是否也必须使用调度器?我直到现在才得到任何例外 我可以访问(读取)我的Backgroundtask中的字段或属性吗 public class MainWindowViewModel : BindableBase { // PropertyChanged is done
public class MainWindowViewModel : BindableBase
{
// PropertyChanged is done trough Fody.PropertyChanged
public int CurrentIndex { get; set; }
public DelegateCommand StartCommand { get; set; }
private Process _clientprocess;
private NamedPipeServer<string> _server;
private List<string> mylist;
enum State { WaitForConnection = 0, Connected = 1, WaitForProcessExit = 2, Exited = 4 }
private State _statemaschine;
public MainWindowViewModel()
{
StartCommand = new DelegateCommand(StartCommandMethod);
mylist = new List<string>();
}
private void StartCommandMethod()
{
mylist.Add("One");
mylist.Add("Two");
var currentindex = 0;
_statemaschine = State.Exited;
_server = new NamedPipeServer<string>("MyPipe");
_server.ClientConnected += ServerOnClientConnected;
_server.ClientDisconnected += ServerOnClientDisconnected;
_server.Start();
var info = new ProcessStartInfo
{
CreateNoWindow = false,
UseShellExecute = true,
FileName = @".\ClientTest.exe"
};
_clientprocess = new Process { StartInfo = info };
_clientprocess.Exited += Clientprocess_Exited;
_clientprocess.EnableRaisingEvents = true;
Task.Run((() =>
{
while (currentindex < mylist.Count)
{
switch (_statemaschine)
{
case State.Exited:
_clientprocess.Start();
_statemaschine++;
while (_statemaschine == State.WaitForConnection)
{
}
break;
case State.Connected:
_server.PushMessage(mylist[currentindex]);
while (_statemaschine == State.Connected)
{
}
_statemaschine = State.WaitForProcessExit;
break;
case State.WaitForProcessExit:
while (_statemaschine == State.Exited)
{
}
_statemaschine = State.Exited;
currentindex++;
break;
case State.WaitForConnection:
break;
}
}
}));
}
private void ServerOnClientDisconnected(NamedPipeConnection<string, string> connection)
{
Debug.WriteLine("Disconnected");
}
private void ServerOnClientConnected(NamedPipeConnection<string, string> connection)
{
_statemaschine = State.Connected;
Debug.WriteLine("Client connected");
}
private void Clientprocess_Exited(object sender, System.EventArgs e)
{
_statemaschine = State.Exited;
Debug.WriteLine("Exited");
}
公共类MainWindowViewModel:BindableBase
{
//PropertyChanged通过Fody.PropertyChanged完成
public int CurrentIndex{get;set;}
公共DelegateCommand StartCommand{get;set;}
私有进程\u客户端进程;
私有名称管道服务器(u server);
私人名单;
枚举状态{WaitForConnection=0,Connected=1,WaitForProcessExit=2,Exited=4}
私营国家(国家机器),;
公共主窗口视图模型()
{
StartCommand=新的DelegateCommand(StartCommand方法);
mylist=新列表();
}
私有void StartCommandMethod()
{
我的清单。添加(“一”);
mylist.添加(“两个”);
var currentindex=0;
_statemaschine=State.Exited;
_服务器=新名称管道服务器(“MyPipe”);
_server.ClientConnected+=ServerOnClientConnected;
_server.ClientDisconnected+=ServerOnClientDisconnected;
_server.Start();
var info=新流程StartInfo
{
CreateNoWindow=false,
UseShellExecute=true,
FileName=@“\ClientTest.exe”
};
_clientprocess=新进程{StartInfo=info};
_clientprocess.Exited+=clientprocess\u退出;
_clientprocess.EnableRaisingEvents=true;
Task.Run((()=>
{
而(currentindex这是“允许的”,您只需要小心操作,并将适当的字段标记为volatile,或者在修改字段时使用锁定。通常读取字段不是问题,当您在多个线程上修改字段时会出现问题。我在任务和UI中访问_statemchinestate,这意味着我将锁定此字段?是的,有一个c如果任务和UI同时更改
StateMachine
,则其中一个可能会丢失。您应该同步访问,但无法锁定枚举,您需要创建一个同步对象来锁定。但是,您似乎无法访问该任务中的任何UI元素,因此确实要使用Dispatcher吗?公共属性CurrentIndex绑定到我的UI。我将重新考虑我的代码以避免多线程问题。