我想用一个CancellationToken
取消的呼叫HttpClient.PostAsJsonAsync
.然而,以下设置调用PostAsJsonAsync
无限期挂起(我离开它运行数分钟).
CancellationTokenSource source = new CancellationTokenSource();
source.Cancel();
HttpClient client = new HttpClient();
try
{
var task = client.PostAsJsonAsync<MyObject>("http://server-address.com",
new MyObject(), source.Token);
task.Wait();
}
catch (Exception ex)
{
//Never gets hit.
}
Run Code Online (Sandbox Code Playgroud)
请注意,我传递一个已经取消了CancellationTokenSource
-我有同样的问题,如果我取消使用令牌Task.Delay
有短暂的延迟.
我知道我可以简单地检查是否该令牌之前已经电话取消,但即便如此,我也有同样的问题,如果令牌被短暂延迟后取消,也就是说,它不是在方法调用之前取消,但因此后不久变得它.
所以我的问题是,是什么原因造成这一点,我可以做些什么来解决什么/解决这个问题?
编辑
对于那些寻找一个变通方法,通过@Darrel米勒的回答启发,我想出了下面的扩展方法:
public static async Task<HttpResponseMessage> PostAsJsonAsync2<T>(this HttpClient client, string requestUri, T value, CancellationToken token)
{
var content = new ObjectContent(typeof(T), value, new JsonMediaTypeFormatter());
await content.LoadIntoBufferAsync();
return await client.PostAsync(requestUri, content, token);
}
Run Code Online (Sandbox Code Playgroud) c# task-parallel-library cancellationtokensource dotnet-httpclient
我陷入了困境,我尝试在网上找到解决方案,但没有成功。我是 MVC 与实体框架的新手,当我尝试运行应用程序时它抛出异常:
传入字典的模型项的类型为“System.Data.Entity.Infrastructure.DbQuery
1[<>f__AnonymousType1
2[UnRelatedEntity.Models.t_AbortReason,UnRelatedEntity.Models.t_Activity]]”,但该字典需要类型为“UnRelatedEntity.Models”的模型项.MobilePhoneXchangeEntities1'
我使用一个实体作为模型,它分别从两个表中获取数据,它们之间没有关系。
控制器:
public ActionResult Index()
{
MobilePhoneXchangeEntities1 ent = new MobilePhoneXchangeEntities1();
var result = from foo in ent.t_AbortReason
from bar in ent.t_Activity
where foo.AbortReasonCategoryId != null && bar.ActivityId != null
select new { Foo = foo, Bar = bar };
return View(result);
}
Run Code Online (Sandbox Code Playgroud)
看法
@model UnRelatedEntity.Models.MobilePhoneXchangeEntities1
Run Code Online (Sandbox Code Playgroud)
在视图中,我只是在写上面的行,我的意思是我只是继承了模型,没有别的,但我仍然对如何输入模型和模型的类型感到困惑,但我很无助。
任何人都可以为此提供帮助,但请记住,我在我的模型中使用了两个不相关的表。
我正在尝试创建一个ProgressBar表单,该表单使用BackgroundWorker在显示进度条的同时在不同的线程上执行操作.
目前,我的ProgressBar类包含一个ProgressBarControl,下面是代码的样子:
public partial class QTProgressBar : DevExpress.XtraEditors.XtraForm
{
private BackgroundWorker m_backgroundWorker;
private AutoResetEvent m_resetEvent;
public QTProgressBar()
{
InitializeComponent();
InitializeProgressBar();
m_backgroundWorker = new BackgroundWorker();
m_backgroundWorker.WorkerReportsProgress = true;
m_backgroundWorker.WorkerSupportsCancellation = true;
m_backgroundWorker.DoWork += new DoWorkEventHandler(m_backgroundWorker_DoWork);
m_backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(m_backgroundWorker_ProgressChanged);
m_backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(m_backgroundWorker_RunWorkerCompleted);
m_resetEvent = new AutoResetEvent(false);
}
void m_backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
m_resetEvent.Reset();
CloseProgressBar();
}
void CloseProgressBar()
{
if (InvokeRequired)
{
Invoke( new MethodInvoker(CloseProgressBar));
return;
}
this.Close();
}
void m_backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
Action operation = …
Run Code Online (Sandbox Code Playgroud)