C# 来自Selfhost WebApi控制器的WPF调度程序

C# 来自Selfhost WebApi控制器的WPF调度程序,c#,wpf,asp.net-web-api,owin,C#,Wpf,Asp.net Web Api,Owin,我想知道在Owin自托管webapi控制器中,调度器的正确用法是什么?我将ICommand与NotifyTaskbarIcon一起使用 public HttpResponseMessage ShowPortalWindow() { var showPortalCommand = (App.Current as App).TaskbarCommands.ShowPortalCommand; var dispather = Request.Properties["Dispatcher"] as D

我想知道在Owin自托管webapi控制器中,调度器的正确用法是什么?我将ICommand与NotifyTaskbarIcon一起使用

public HttpResponseMessage ShowPortalWindow()
{

var showPortalCommand = (App.Current as App).TaskbarCommands.ShowPortalCommand;
var dispather = Request.Properties["Dispatcher"] as Dispatcher;

if (!dispather.Invoke(() => showPortalCommand.CanExecute(url)))
    return Request.CreateResponse(HttpStatusCode.Unauthorized);

    dispather.Invoke(() => showPortalCommand.Execute(url));
}
我需要调用第一个“CanExecute”方法吗?它不会改变任何用户界面

起初,我希望简单地获得如下主应用程序(应用程序)线程:

var mainApp = dispather.Invoke(() => (App.Current as App));

但这导致了“线程已被另一个进程使用..”。

var mainApp=dispather.Invoke(()=>(App.Current as App));大致翻译为:“在调度程序的线程上同步调用表达式X”。这个线程已经被使用了,所以通过选择“Invoke”,您可以尝试强制使用该线程的人等待表达式X完成-这就是为什么您会得到异常。试着改用BeginInvoke,如果它有效,我会将我的评论升级为一个答案,并充实一下。你发布的代码给我留下了很多问题,所以我想在回答之前了解更多。(例如:获取不同进程的线程?)感谢您的评论。我不能使用BeginInvoke,因为它需要不同的参数。而榜样是最受欢迎的。我试图使用selfhost webapi中的端点作为触发器来更改窗口中的某些UI。我很乐意提供帮助,但我仍然不清楚您到底有什么。简单图?:)