我在用春天,
我需要向近10000名用户发送邮件.现在我在那个页面等待所有邮件发送.但我不想等待.我需要将这个任务交给某个班级,这个班级将在移交后自动执行,然后我需要继续我的任务.
我怎么能这样做?
提前致谢...
在我的Android应用程序中,我有一个Singleton类.当应用程序任务存活时,它就像一个魅力.但是在应用程序任务被杀死之后,Singleton类被破坏了,这显然是.我需要让这堂课永远活着.可能吗?什么是使这个类从未被Android系统杀死的最佳方法,在这种情况下我是否需要使用后台服务?
我正在尝试异步运行一些东西.这是我的代码:
private async void SearchButton_Click(object sender, EventArgs e)
{
await Search();
}
// Inside search there is no async operation
public Task Search()
{
// search for data in DB
// update DataGridView and other elements on UI
// How to return null/void/nothing?
return ???;
}
Run Code Online (Sandbox Code Playgroud)
等待同步方法是非法/不良做法Search()吗?我知道我可以使用Task.Run(Search)而不是然后Search()将在另一个线程(不是在UI线程)上执行,我必须经常Invoke()在里面使用Search().
我将Search()方法的返回值从'void'更改为'Task',所以它等待但是我必须返回什么?
HttpClient.GetAsync在Linq中使用或者任何异步方法或任何BCL异步方法Select可能会导致一些奇怪的两次射击.
这是一个单元测试用例:
[TestMethod]
public void TestTwiceShoot()
{
List<string> items = new List<string>();
items.Add("1");
int k = 0;
var tasks = items.Select(d =>
{
k++;
var client = new System.Net.Http.HttpClient();
return client.GetAsync(new Uri("http://testdevserver.ibs.local:8020/prestashop/api/products/1"));
});
Task.WaitAll(tasks.ToArray());
foreach (var r in tasks)
{
}
Assert.AreEqual(1, k);
}
Run Code Online (Sandbox Code Playgroud)
测试将失败,因为k为2.不知何故,程序运行GetAsync两次触发的委托.为什么?
如果我删除foreach (var r in tasks),测试通过.为什么?
[TestMethod]
public void TestTwiceShoot()
{
List<string> items = new List<string>();
items.Add("1");
int k = 0;
var tasks = items.Select(d =>
{
k++;
var …Run Code Online (Sandbox Code Playgroud) 我一直在寻找一些关于如何创建多个任务并让它们一个接一个地运行的时间.这是java代码:
public class Main extends Application {
DropShadow shadow = new DropShadow();
Button button = new Button("Start");
Task<Integer> task;
Task<Integer> task2;
Label label1 = new Label("Status: NOT STARTED");
ProgressBar bar = new ProgressBar(0);
ProgressIndicator pi = new ProgressIndicator(0);
Thread th = new Thread();
public void start(Stage stage) {
task = new Task<Integer>() {
protected Integer call() throws Exception {
int iterations;
for (iterations = 0; iterations < 1001; iterations++) {
if (isCancelled()) {
updateMessage("Cancelled");
break;
}
updateProgress(iterations, 1001);
updateMessage("Test");
try …Run Code Online (Sandbox Code Playgroud) 我肯定错过了什么,
var t2 = new Task<bool>(() =>
{
return UserName == "Admin";
});
bool x = await t2;
Run Code Online (Sandbox Code Playgroud)
将bool x = await t2;永远不会结束,x没有收到任何东西(而我确信,用户名等于"管理"),不知道到底是怎么回事,请人能向我解释.
我正在使用下面的查询从数据库加载一些数据.我可以使用此方法等待此查询完成使用await-async吗?
public static void LoadData()
{
using (MyEntities entities = new MyEntities())
{
List<Employee> Employees = (from d in entities.Employees
where d.Id > 100
select new Employee
{
Name = d.LastName + ", " + d.FirstName
DoB = d.dob
}).ToList();
}
}
Run Code Online (Sandbox Code Playgroud) public async Task<Foo> Execute(int id)
{
var parameters = new { id};
using (var con = new SqlConnection(this.connectionString))
{
await con.OpenAsync();
return await con.QueryAsync<foo>(
"dbo.uspGetMeFoo",
parameters,
commandType: CommandType.StoredProcedure,
commandTimeout: int.Parse(ConfigurationManager.AppSettings["SqlCommandTimeout"]))
.ContinueWith(task => task.Result.FirstOrDefault());
}
}
Run Code Online (Sandbox Code Playgroud)
在调用类中等待这个方法,其中 - 在一段时间之后 - 我想在不明确阻塞的情况下使用结果.
我可以解决这个问题不使用ContinueWith上述,通过返回Task<IEnumerable<Foo>>和FirstOrDefault()调用代码这个代替.
从我所看到的上面的阻塞是臭的,更糟糕的将导致问题,可能是一个僵局.我对么?
我是并行编程的新手,我试图找出为什么偶尔会得到一个EmonitorLockException:当我增加要运行的并行任务的数量时,不拥有对象锁.是这样的情况,线程变得纠结我运行的任务越多.或者我的代码不正确?
{$APPTYPE CONSOLE}
uses
System.SysUtils, System.Threading, System.Classes, System.SyncObjs, System.StrUtils;
const
WorkerCount = 10000; // this is the number of tasks to run in parallel note:when this number is increased by repeated factors of 10
// it takes longer to produce the result and sometimes the program crashes
// with an EmonitorLockException:Object lock not owned . Is it that my program is not written correctly or efficiently to run
// a large number of parallel taks and the Threads become entagled. …Run Code Online (Sandbox Code Playgroud) 我有一个简单的wpf应用程序,只有一个按钮和一个文本块.按钮单击事件处理程序运行下一个代
var resultTask = webClient.DownloadStringTaskAsync("http://google.com");
textBlock.Text = resultTask.Result;
Run Code Online (Sandbox Code Playgroud)
在我使用await之前,这不起作用(它是挂起)
var result = await ebClient.DownloadStringTaskAsync("http://google.com");
textBlock.Text = result;
Run Code Online (Sandbox Code Playgroud)
但在简单的控制台应用程序中它工作正常
var resultTask = webClient.DownloadStringTaskAsync("http://google.com");
Console.WriteLine(resultTask.Result);
Run Code Online (Sandbox Code Playgroud)
你能解释一下为什么第一个变体在wpf中不起作用吗?