我的项目在源代码中有一个 .zip 文件,我想将其放入文件夹 1.1.0/deployment 下的 appserver.zip 文件中。此 .zip 文件再次捆绑在 .tar 文件中。
注意:此项目不是 JAVA/Groovy 项目,即没有 Java 程序或 groovy 程序
以下是我的 build.gradle,它正在创建正确的 .zip(其中包含正确的文件夹/源 .zip 文件)和包含 appserver.zip 的 .tar 文件:
// Let say $projName, $folderArtifactoryVersion or etc variables used in the following script - are set correctly to a valid value.
task createZipFile( type: Zip) {
// Create artifact directory
def dirName = "build/folderDist"
def dirDist = new File( dirName )
dirDist.mkdirs()
destinationDir dirDist
archiveName "appserver.zip"
println ''
println 'bundleArchiveName: ' + archiveName …Run Code Online (Sandbox Code Playgroud) 我的应用程序使用脚本。到目前为止,我已经将 C# 用于脚本并使用CodeDomProvider. 但是,我一直在考虑使用NLuafork( 的 fork LuaInterface)切换到 Lua ,因为编写脚本要容易得多,而且我熟悉语法。
但是,我面临一个问题。目前,我有一个asynchronous方法可以返回一个Task<bool>. 它使用一个TaskCompletionSource对象并返回它的Result. 这样一来,我可以阻止脚本的执行,因为这里等到Result了的TaskCompletionSource目标已定,然后才返回此Result。
现在,有了 Lua - 它是不同的。我显然不能使用await运算符,因为它是 C# 5.0 的语法,而你不能在 Lua 中使用它。所以这就是为什么我要问是否有解决方法。我希望能够获得与我的旧代码(发布在这篇文章下方)相同的结果,而无需使用await运算符。有人告诉我,我可以用 来做到这一点Task.ContinueWith,但我不熟悉这个,而且网上的例子很乏味。如果有人可以用我的代码向我展示一个示例,那就太好了。
这是我的方法:
public async Task<bool> ReturnResult()
{
this.Response = new TaskCompletionSource<bool>();
return await this.Response.Task;
}
Run Code Online (Sandbox Code Playgroud)
这是我在脚本中使用它的方式:
var result = await ReturnResult();
Run Code Online (Sandbox Code Playgroud)
在Result该的TaskCompletionSource目标是通过我的代码另一部分设置。
基本上,如果您仍然无法理解我想要实现的目标 - 一种停止执行的方法,直到代码的另一部分设置了响应。但是,它必须是asynchronous,因为我不希望我的主线程卡住。
编辑:尝试了 JonSkeet …
我有一些代码要运行,直到我请求取消为止。
Task.Run(() =>
{
while (!token.IsCancellationRequested)
{
GetFeedbackTask();
}
}, token);
Run Code Online (Sandbox Code Playgroud)
然后我执行这个方法token.Cancel()。这将按预期取消任务,按预期取消我的 while 循环。问题是当我在取消token.IsCancellationRequested属性后再次尝试运行 Task 时true。是什么设置了属性false?我需要Dispose令牌吗?
我正在为我的应用程序使用 JavaFX,我正在使用 JavaFX 中的任务来上传文件。
public class Upload extends Task<UploadFile> {
private UploadFile uploadFile;
public Upload(UploadFile uploadFile){
this.uploadFile= uploadFile;
}
@Override
protected synchronized UploadFile call() throws Exception {
....
hcl.invokeMethodUploadFile(uploadFile);
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
方法“initiate”创建一个 UploadFile-Container 并将其添加到 Threadpoolexecutor 的队列中。此方法将对每个文件执行一次。此类还包含在文件上传完成后继续进行的其他必要信息。
public void initiate(){
UploadFile uc = new UploadFile(file);
// Set other informations as well
// add Container to queue
main.getUploadFiles().add(uc);
}
Run Code Online (Sandbox Code Playgroud)
UploadFile 是一个包含有关文件、服务器信息等信息的类。
除其他外,我想在上传完成后将 url 插入到数据库中。文件上传后如何执行一些代码?
我正在尝试使用一个小进度条在 JavaFX 中创建一个登录页面。但是每次我想检查凭据时,任务都会失败并将我扔到 setOnFailed() 方法中。我不知道是什么让它改变了它的状态或如何使它工作。
到目前为止,这是我的代码:
public class Login extends Base_controller implements Initializable{
private ProgressBar pb = new ProgressBar();
private TextField username;
private PasswordField password;
public void start(Stage primaryStage) throws Exception {
// Setting structure and components
Label username_lbl = new Label();
Label password_lbl = new Label();
username_lbl.setText("Username:");
password_lbl.setText("Password:");
username = new TextField();
password = new PasswordField();
//some code
VBox root = new VBox();
root.setAlignment(Pos.CENTER);
root.getChildren().addAll(container0, container1, container2, container3);
root.setSpacing(30);
// button action listener
test.setOnAction(e->{
Platform.runLater(() -> {
root.getChildren().add(pb);
}); …Run Code Online (Sandbox Code Playgroud) 我想弄清楚如何重新排队一些已超时的异步 DNS 请求(我正在使用 uvloop 和 aiodns 模块)。
这是我设置循环的代码:
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
loop = asyncio.get_event_loop()
resolver = aiodns.DNSResolver(loop=loop)
sem = asyncio.Semaphore(MAX_COUNT)
Run Code Online (Sandbox Code Playgroud)
此函数执行查找:
async def lookup(name):
with (await sem):
response = await resolver.query(name, 'A')
return response
Run Code Online (Sandbox Code Playgroud)
我读入了一个包含名称的文件以查找和设置任务,包括处理结果的回调:
for n in names:
host = '{}.{}'.format(n, domain)
task = asyncio.ensure_future(lookup(host))
tasks.append(task)
task.add_done_callback(functools.partial(got_result, host))
Run Code Online (Sandbox Code Playgroud)
并启动查找队列。
print("Looking up {} subdomains...".format(len(names)))
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
Run Code Online (Sandbox Code Playgroud)
在got_result回调中,我然后测试 future.exception() 并处理它,如果有,如果没有,则将结果打印到屏幕上。我可以接受一些例外情况(即找不到域名),但其他人喜欢超时,我想重新排队项目。是否有一种简单的方法可以将未来添加回循环以进行另一次尝试,还是我需要为此设置单独的功能并手动重新添加任务?
谢谢你的帮助。
我是 rx 的新手,并且一直在使用 dot net 中的反应式扩展来处理一些网络代码。我的问题是,当我通过我提供的令牌触发取消时,我使用异步函数创建的 tcpClients 的 observable 没有像我预期的那样完成。这是我遇到问题的代码的简化版本:
public static class ListenerExtensions
{
public static IObservable<TcpClient> ToListenerObservable(
this IPEndPoint endpoint,
int backlog)
{
return new TcpListener(endpoint).ToListenerObservable(backlog);
}
public static IObservable<TcpClient> ToListenerObservable(
this TcpListener listener,
int backlog)
{
return Observable.Create<TcpClient>(async (observer, token) =>
{
listener.Start(backlog);
try
{
while (!token.IsCancellationRequested)
observer.OnNext(await Task.Run(() => listener.AcceptTcpClientAsync(), token));
//This never prints and onCompleted is never called.
Console.WriteLine("Completing..");
observer.OnCompleted();
}
catch (System.Exception error)
{
observer.OnError(error);
}
finally
{
//This is never executed and my …Run Code Online (Sandbox Code Playgroud) 我得到了 JavaFX 主线程,在那里我创建了新的线程来扩展任务并排序和替换条。一切都很好,但我想在替换时进行一些延迟(例如 100 毫秒)以显示逐步排序,也许还有动画。问题是,当我使用 Thread.sleep() 或 TranslateTransition() 时,它只是将所有延迟毫秒加在一起,形成一个在更改条形之前发生的大延迟。如何使延迟在 UI 线程中正常工作?
在主类中:
Sorting sorting = new Sorting();
sortThread = new Thread(sorting, "sort");
sortThread.start();
sortThread.join();
Run Code Online (Sandbox Code Playgroud)
我的班级排序扩展了任务
public class Sorting extends Task<Void> {
//some stuff here
@Override
protected Void call() throws Exception {
taskThread = new Thread(counter, "time");
taskThread.setDaemon(true);
taskThread.start();
int n = array_tmp.length;
int temp;
for (int i = 0; i < n; i++) {
for (int j = 1; j < (n - i); j++) {
if (array_tmp[j - 1] …Run Code Online (Sandbox Code Playgroud) 如何为返回字符串的任务创建 lambda 表达式?
这是我尝试过的,但出现错误。
感谢您的任何帮助。
public static async Task<string> GetStringAsync(string path)
{
try
{
var task = new Task<string>(async () =>
{
var response = await Client.GetAsync(path);
var responsestring = await response.Content.ReadAsStringAsync();
return responsestring;
});
return await Task.WhenAny(task, Task.Delay(20000)) == task
? task.Result
: RequestTimeOutMessage;
}
catch (Exception e)
{
return e.GetBaseException().Message;
}
}
}
Run Code Online (Sandbox Code Playgroud) 参考:https : //github.com/dotnet/corefx/blob/master/src/System.Net.Mail/src/System/Net/Mail/SmtpClient.cs#L583
此方法被标记为异步但返回无效。它实际上是异步的,只是不是基于任务的?如果是这样,它是如何异步的?
task ×10
c# ×5
asynchronous ×4
java ×3
async-await ×2
javafx ×2
.net-core ×1
build ×1
cancellation ×1
compilation ×1
future ×1
gradle ×1
javafx-8 ×1
lua ×1
python ×1
sorting ×1
string ×1