这是一个针对线程迷的人.我有这个方法:
public void RefreshMelts()
{
MeltsAvailable.Clear();
ThreadPool.QueueUserWorkItem(delegate
{
Dispatcher.BeginInvoke((ThreadStart)delegate
{
eventAggregator.GetEvent<BusyEvent>().Publish(true);
eventAggregator.GetEvent<StatusMessageEvent>().Publish(
new StatusMessage("Loading melts...", MessageSeverity.Low));
});
try
{
IList<MeltDto> meltDtos = meltingAppService.GetActiveMelts();
Dispatcher.Invoke((ThreadStart)delegate
{
foreach (MeltDto availableMelt in meltDtos)
{
MeltsAvailable.Add(availableMelt);
}
OnPropertyChanged("MeltsAvailable");
eventAggregator.GetEvent<BusyEvent>().Publish(false);
eventAggregator.GetEvent<StatusMessageEvent>().Publish(
new StatusMessage("Melts loaded", MessageSeverity.Low));
});
}
catch (ApplicationException ex)
{
log.Error("An error occurred in MeltsViewModel when attempting to load melts", ex);
Dispatcher.Invoke((ThreadStart)delegate
{
MeltsAvailable.Clear();
eventAggregator.GetEvent<StatusMessageEvent>().Publish(
new StatusMessage("Melt data could not be loaded because an error occurred; " +
"see the application log for detail", …Run Code Online (Sandbox Code Playgroud) 我希望能够以快速的速度向数据网格添加项目,而不会导致UI延迟.
这是我现在正在做的事情:我正在使用绑定到数据网格的ObservableCollection.
我使用后台线程,只有在从observable集合中插入/删除时,才会循环并调用当前调度程序上的Invoke.调用BeginInvoke会产生不良结果.
我知道在调度员上调用那么多导致延迟,但我不知道还能做什么.我之前使用过背景工作者,但我认为这不适用于我的场景.
我该怎么做才能保持用户界面的响应速度?
如果我更新一个属性,该属性在绑定控件的调度程序以外的线程上抛出NotifyPropertyChanged,则强制为该调度程序编组的更新?
BackgrounWorker.Run() => { blah.Blahness = 2; // notifies property changed on BW, is this marshalled to the dispatcher? }
Run Code Online (Sandbox Code Playgroud) 我需要在正确的工作线程上调用在计时器线程上运行的方法。Invoke/BeginInvoke 流程对我有用。有2个线程共享一个线程间数据容器进行数据交换。一种是填充队列,一种是处理队列。如果队列在空状态后已满,则会引发事件。所有问题都是由计时器引起的,计时器在其经过的事件上打开新线程。我正在使用调度程序在正确的线程上调度,但除了这个调度程序之外,一切正常。:-)
请问有人看出问题出在哪里了吗?
完整的测试代码在这里: http: //pastebin.com/jqYbR9PS。
调试输出是这样的:
App Thread ID: 9
Processor Thread ID: 10
Processor Dispatcher Thread ID: 10
The thread '<No Name>' (0x888) has exited with code 0 (0x0).
Processor QueueListener caller Thread ID: 12
Processor Dispatcher Thread ID: 10
Processor invoking ProcessQueue.
...here shut be processing output...
Processor invoked ProcessQueue.
App Thread ID on end: 9
The thread 'vshost.RunParkingWindow' (0x17c4) has exited with code 0 (0x0).
The thread '<No Name>' (0x820) has exited with code 0 …Run Code Online (Sandbox Code Playgroud) 首先,我要说的是,很难详细解释我的问题,但我会尽力。我将更新详细的解释或我使用的可能导致异常的更多代码。如果我的代码很混乱,我很抱歉。
我读过很多同名的问题,但我一点运气都没有。我对线程/任务/调度程序了解甚少,所以如果您发现我的代码有问题,请指导我。
简介
我的应用程序每n分钟通过计时器执行一次后台任务。
后台任务:从API获取数据,然后生成Window元素作为包含数据的表单,然后打印它们。
问题:目前该异常已发生两次,导致无法生成两个表单文档,因此无法打印它们。
获得的详细异常TaskScheduler.UnobservedTaskException是:
通过等待任务或访问其 Exception 属性都没有观察到任务的异常。结果,未观察到的异常被终结器线程重新抛出。
System.Collections.ObjectModel.ReadOnlyCollection`1[System.Exception]
这是我的一段代码,可能对您查找问题根源有用:
public void BackgroundTask(object sender, EventArgs e)
{
Application.Current.Dispatcher.Invoke(
new Action(GetInvoiceData),
DispatcherPriority.Background,
null
);
}
Run Code Online (Sandbox Code Playgroud)
...哪里GetInvoiceData:
public async void GetInvoiceData()
{
try
{
JsonData = await ApiHelperInstance.Post(ApiParam);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (!string.IsNullOrEmpty(JsonData))
{
var apiReturn = new ApiReturn();
try
{
apiReturn = JsonConvert.DeserializeObject<ApiReturn>(JsonData);
}
catch (JsonException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (apiReturn.Result != …Run Code Online (Sandbox Code Playgroud) 如果您有异步方法,如下所示:
private async Task DoAsync()
{
Console.WriteLine(@"(1.1)");
Thread.Sleep(200);
Console.WriteLine(@"(1.2)");
await Task.Delay(1000);
Console.WriteLine(@"(1.3)");
}
Run Code Online (Sandbox Code Playgroud)
并使用Dispatcher异步调用它:
Console.WriteLine(@"(1)");
await Application.Current.Dispatcher.InvokeAsync(DoAsync);
Console.WriteLine(@"(2)");
Run Code Online (Sandbox Code Playgroud)
你得到的输出将是:(1)(1.1)(1.2)(2)(1.3)
如果你使用Dispatcher.BeginInvoke()并且你将等待Completed事件,效果将是相同的(这是预期的):
Console.WriteLine(@"(1)");
var dispatcherOp = Dispatcher.BeginInvoke(new Func<Task>(DoAsync));
dispatcherOp.Completed += (s, args) =>
{
Console.WriteLine(@"(2)");
};
Run Code Online (Sandbox Code Playgroud)
我们在这里看到的是DoAsync()方法中的await使得Dispatcher相信操作已经结束.
我的问题:这是一个错误还是一个功能?你知道任何描述这种行为的文件吗?我找不到任何东西.
我不是要求解决方法.
我读到:.NET Dispatcher,适用于 .NET Core?
我无法应用这两种解决方案。
知道如何从 .net-core 3.1 访问调度程序吗?
重要信息克莱门斯解决方案效果很好。此外,直接从 Visual Studio 重新加载项目的优点是可以告诉您有关 .csproj 文件中隐藏的问题的更多信息。我的 UseWPF 与 UseWpf 不匹配!
我有类似的东西
<?php
class AccountController extends Zend_Controller_Action
{
public function init()
{
if(!Zend_Auth::getInstance()->hasIdentity()) {
//need to forward to the auth action in another controller,
//instead of dispatching to whatever action it would be dispatched to
}
}
...
Run Code Online (Sandbox Code Playgroud)
我不能使用$ this - > _ forward("action"),因为auth操作不在同一个控制器中,而在init中,转发必须是在同一个控制器中的动作.有任何想法吗?
$request->setModuleName('module')
->setControllerName('controller')
->setActionName('action');
Run Code Online (Sandbox Code Playgroud)
也行不通.我试图清除这些参数,但我仍然没有.
model-view-controller routing zend-framework controller dispatcher
我按照这个网站的说明:http://colorblindprogramming.com/cronjobs-in-cakephp-2-in-5-steps,但我的cronjob不起作用.
这是我的/app/cron_dispatcher.php:
<?php
if (!defined('DS')) {
define('DS', DIRECTORY_SEPARATOR);
}
if (!defined('ROOT')) {
define('ROOT', dirname(dirname(__FILE__)));
}
if (!defined('APP_DIR')) {
define('APP_DIR', basename(dirname(__FILE__)));
}
if (!defined('WEBROOT_DIR')) {
define('WEBROOT_DIR', basename(dirname(__FILE__)));
}
if (!defined('WWW_ROOT')) {
define('WWW_ROOT', dirname(__FILE__) . DS);
}
if (!defined('CAKE_CORE_INCLUDE_PATH')) {
if (function_exists('ini_set')) {
ini_set('include_path', ROOT . DS . 'lib' . PATH_SEPARATOR . ini_get('include_path'));
}
if (!include('Cake' . DS . 'bootstrap.php')) {
$failed = true;
}
} else {
if (!include(CAKE_CORE_INCLUDE_PATH . DS . 'Cake' . DS . 'bootstrap.php')) …Run Code Online (Sandbox Code Playgroud) 有什么区别
Dispatcher.CurrentDispatcher.Invoke(somemethod);
Run Code Online (Sandbox Code Playgroud)
和
Application.Current.Dispatcher.Invoke(somemethod);
Run Code Online (Sandbox Code Playgroud)
当我使用第一个方法时,某些方法的执行速度比第二个方法快.我使用秒表并测量经过的毫秒数.我使用此方法根据来自外部线程的一些数据更新一些UI控件.
dispatcher ×10
c# ×7
wpf ×7
.net ×1
.net-core ×1
async-await ×1
cakephp ×1
cakephp-2.3 ×1
controller ×1
cron ×1
nmock ×1
php ×1
routing ×1
task ×1
testing ×1