我的程序正在创建一些任务来完成一些工作.现在我想在退出程序之前等待所有任务完成.
我知道Task.WaitAll(...)方法,但在我的情况下,我想动态创建一些任务 - > Task.WaitAll()调用时它们不存在.
我现在的问题是:如何完成所有正在运行的任务?
这就是我现在正在做的事情.
它有效,但我想确保这也是一个很好的方法.
public class Test
{
public ConcurrentBag<Task> RunningTasks { get; set; }
public Test()
{
RunningTasks = new ConcurrentBag<Task>();
}
public void RunIt(){
//...create some Tasks asynchronously so they may be created in a few seconds and add them to the list like
//RunningTasks.Add(...);
//Just wait until all tasks finished.
var waitTask = Task.Run(() =>
{
while (true)
{
if (RunningTasks.All(t => t.IsCompleted))
break;
Thread.Sleep(1000);
}
});
waitTask.Wait();
}
}
Run Code Online (Sandbox Code Playgroud) 检查下面的jquery代码。在这里,我从 html 中抓取文件,然后通过 ajax 调用将其发布到我的 Controller Post 方法。从 Controller post 方法中,我成功地接收到该文件,files如您所见。但我的问题是我如何发送另外两个被调用的文本参数 -type并且id从这个 ajax 调用然后我怎样才能从带有该文件的控制器中获取该值?基本上我也必须用那些文本值来获取那个文件。这怎么可能?ajax 和控制器需要什么改变?
网址:
<div class="col-sm-3" style="float:left;">
<label class="btn btn-outline-dark btn-block">
Browse...
<span id="uploaded-file-name" style="font-style: italic"></span>
<input id="file-upload" type="file" name="file"
onchange="$('#uploaded-file-name').text($('#file-upload')[0].value);" hidden>
</label>
</div>
<div class="col-sm-2" style="float:left;">
<button class="btn btn-dark" id="start_upload">Start Upload</button>
</div>
Run Code Online (Sandbox Code Playgroud)
jQuery ajax:
//file upload
$("#start_upload").click(function (evt) {
var fileUpload = $("#file-upload").get(0);
var files = fileUpload.files;
var data = new FormData();
for (var i = 0; i < files.length; i++) …Run Code Online (Sandbox Code Playgroud) 我正在使用 AES 进行文本加密。我将密钥和加密文本保存在文件中,但是我应该如何处理 IV?