从任务调用以下代码 t = new task(() => StartScanAsync(path)); 但计时器事件没有触发,计时器在 StartTimer 中启动。
private void StartScanAsync(string path)
{
StartTimer();
foreach (string fsPath in Alphaleonis.Win32.Filesystem.Directory.EnumerateFileSystemEntries(path, "*", SearchOption.AllDirectories, true))
{
Alphaleonis.Win32.Filesystem.FileInfo finfo = new Alphaleonis.Win32.Filesystem.FileInfo(fsPath);
fileClass.AddFile(finfo);
// UpdateStatus(fsPath);
UpdateTotalFilesScanned();
TotalFileSize += finfo.Length;
UpdateTotalFileSize(TotalFileSize);
IdentifyFileType.FileType ft = FileType.LoadPathRet(fsPath);
// Application.DoEvents();
}
UpdateStatus("Done Scanning.");
StopTimer();
}
Run Code Online (Sandbox Code Playgroud)
我也尝试了 Parallel.ForEach 并遇到了同样的问题。
以下代码应该启动一个计时器,在一个大集合上运行Parrallel.ForEach,然后停止计时器。但是,计时器事件没有被触发...有什么想法以及如何解决这个问题吗?
StartTimer();
Parallel.ForEach(Alphaleonis.Win32.Filesystem.Directory.EnumerateFileSystemEntries(path, "*", SearchOption.AllDirectories), fsPath =>
{
Alphaleonis.Win32.Filesystem.FileInfo finfo = new Alphaleonis.Win32.Filesystem.FileInfo(fsPath);
fileClass.AddFile(finfo);
UpdateStatus(fsPath);
UpdateTotalFilesScanned();
TotalFileSize += finfo.Length;
UpdateTotalFileSize(TotalFileSize);
// IdentifyFileType.FileType ft = FileType.LoadPathRet(fsPath);
Application.DoEvents();
});
StopTimer(); …Run Code Online (Sandbox Code Playgroud) 因此,我需要创建一个特定的任务,在需要时将通过 POST 请求调用该任务,因此我使用放置向导的任务接口,我创建了一个扩展任务的类,将其注册在 /tasks/ 接口中,然后调用它通过这个电话:
curl -X POST http://localhost:8081/tasks/StoreMigration?maxRows=4&date=2014-02-2T15:23:00Z
Run Code Online (Sandbox Code Playgroud)
无论我放在第一个参数,该参数都会出现在我的任务中,第二个参数会消失。知道如何处理吗?
以下是我的Task初始化
final Task<Void> vt=voiceTask();
Button btn = new Button();
btn.setText("Say");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
new Thread(vt).start();
}
});
Run Code Online (Sandbox Code Playgroud)
这是任务编码
public Task<Void> voiceTask() {
return new Task<Void>(){
@Override
protected Void call() throws Exception {
HelloWorld hw=new HelloWorld();// HelloWorld is simple .java class getting voice through sphinx
updateMessage(hw.Hello());
return null;
}
};
}
Run Code Online (Sandbox Code Playgroud)
现在,第一次单击 btn 按钮时,任务正常运行,但随后单击时,不会调用任务。
我希望每次点击时都调用任务。请建议我如何修改我的代码来做到这一点......
我有这个问题,我不知道如何解决它。这是我的 CF 面板中唯一不起作用的事情:
以下信息供网站开发人员用于调试目的。处理请求时发生错误 系统尝试使用未定义的值,这通常表示您的代码或某些系统代码中存在编程错误。空指针是未定义值的另一个名称。
资源:启用可靠的异常信息以提供有关错误来源的更多详细信息。在管理器中,单击“调试和日志记录”>“调试输出设置”,然后选择“鲁棒异常信息”选项。检查 ColdFusion 文档以验证您使用的语法是否正确。搜索知识库以找到问题的解决方案。浏览器 Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML,如 Gecko) Chrome/36.20.20.20 Safari/20.36 远程地址 20.20.20.20 引用者 http://www.example.com/CFIDE/administrator/navserver。 cfm 日期/时间 2014 年 8 月 5 日 04:18 PM
有人可以帮助我吗?我不知道如何或因何原因发生
花了几个小时在这上面后我感到不知所措。
受保护类型声明:
protected type AdditionMachine is
procedure setTask (Item : in WorkerTask);
procedure getTask (Item : out WorkerTask);
procedure calculate;
private
machineType : String (1 .. 1) := "A";
job : workerTask;
end AdditionMachine;
Run Code Online (Sandbox Code Playgroud)
任务:
task body SimpleTask1 is
Available : Natural := ADDITION_MACHINES;
Elements : array (1 .. ADDITION_MACHINES) of AdditionMachine;
begin
loop
select
when Available > 0 =>
accept getMachine (machine : out AdditionMachine) do
machine := Elements(Available);
Available := Available - 1;
end getMachine;
or
accept addMachine …Run Code Online (Sandbox Code Playgroud) 我正在尝试执行预期返回类型为 的任务double。作为参数,我还向任务传递了一个类型的函数double。下面是代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace Tasks
{
class Program
{
private static double Dobro(double x)
{
return 2 * x;
}
static void Main(string[] args)
{
Task<double> dobro = new Task<double>(Dobro(15));
Console.ReadKey();
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我有以下编译时错误作为回报:
CS1503 C# 参数 1:无法从“double”转换为“System.Func”
如何解决这个问题?
我有一个非常简单的代码片段,用于测试如何在 Main() 中调用 Task<> 方法
using System;
using System.Threading;
using System.Threading.Tasks;
class Program
{
private static async Task<int> F1(int wait = 1)
{
await Task.Run(() => Thread.Sleep(wait));
Console.WriteLine("finish {0}", wait);
return 1;
}
public static async void Main(string[] args)
{
Console.WriteLine("Hello World!");
var task = F1();
int f1 = await task;
Console.WriteLine(f1);
}
}
Run Code Online (Sandbox Code Playgroud)
它无法编译,因为:
(1) F1 是异步的,所以 Main() 必须是“异步的”。
(2) 编译器说:
error CS5001: Program does not contain a static 'Main' method suitable for an entry point
Run Code Online (Sandbox Code Playgroud)
所以如果我删除 Main 的“async”,编译器会说: …
假设我有一个任务列表,我想并行运行它们。但是我不需要所有这些都完成才能继续,我可以只做一个。以下代码等待所有任务完成才能继续。有没有办法让我在等待其他任务完成的同时单独继续完成已完成的任务?
List<string>[] x = await Task.WhenAll(new Task<List<string>>[] { task1, task2 })
// when task1 finishes, I want to process the result immediately instead of waiting on task2.
Run Code Online (Sandbox Code Playgroud)
提前致谢!
所以,我正在使用 JavaFX 创建蛇游戏,我似乎无法让游戏正确暂停,即它偶尔会暂停,而其他时候,游戏只是忽略了暂停。所以,基本上我有一个Main类,我在其中初始化所有 GUI 组件,它还充当 javafx 应用程序的控制器。
我有一个启动/暂停游戏的Button命名gameControl,一个Boolean pause跟踪游戏状态(新/暂停/运行)的变量,以及方法startGame, pauseGame。
该gameControl按钮的EventHandler情况如下:
gameControl.setOnClicked(event->{
if(paused == null) startGame(); //new game
else if(paused) continueGame(); //for paused game
else pauseGame(); //for running game
});
Run Code Online (Sandbox Code Playgroud)
该startGame函数看起来像这样:
void startGame(){
paused = false;
Snake snake = new Snake(); //the snake sprite
//following gameLoop controls the animation of the snake
gameLoop = new AnimationTimer(){
@Override
public void handle(long now){
drawSnake(); //draws …Run Code Online (Sandbox Code Playgroud) 考虑以下方法:
public async Task<string> GetNameOrDefaultAsync(string name)
{
if (name.IsNullOrDefault())
{
return await GetDefaultAsync();
}
return name;
}
Run Code Online (Sandbox Code Playgroud)
当name被提供,不等待将发生在方法执行的,但这种方法会正确编译。
但是,此方法将产生如下所示的构建警告:
public async Task<string> GetDefaultAsync()
{
return "foobar";
}
Run Code Online (Sandbox Code Playgroud)
[CS1998] This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
为什么它GetNameOrDefaultAsync可以在不等待的情况下返回,并且不会导致编译器警告,但GetDefaultAsync必须等待才能编译?
执行以下操作会有所改进吗?:
public async Task<string> GetNameOrDefaultAsync(string name)
{
if (name.IsNullOrDefault())
{
return await GetDefaultAsync();
} …Run Code Online (Sandbox Code Playgroud) task ×10
c# ×5
async-await ×2
asynchronous ×2
java ×2
ada ×1
coldfusion ×1
dropwizard ×1
java-threads ×1
javafx ×1
javafx-2 ×1
procedure ×1
timer ×1
uri ×1