我正在编写一个应用程序,它将在UIImageView中显示在我的服务器上找到的图像.
我需要一些异步下载图像并将其缓存在UIImageView中的东西.当我按下按钮时,也需要能够取消下载.
谁能指出我可以做到这一点的方向?
我是F#的新手,我已经从我在网上找到的各种例子中明确了下面的代码,试图更好地理解我如何使用它.目前,下面的代码从文件中读取机器列表并ping每台机器.我不得不将初始数组从文件分成25个机器的较小数组,以控制并发操作的数量,否则需要很长时间才能映射出机器列表.我希望能够使用线程池来管理线程,但我还没有找到让它工作的方法.任何指导都会很棒.我无法做到这一点:
let creatework = FileLines|> Seq.map (fun elem -> ThreadPool.QueueUserWorkItem(new WaitCallback(dowork), elem))
Run Code Online (Sandbox Code Playgroud)
这是完整的代码:
open System.Threading
open System
open System.IO
let filePath = "c:\qa\machines.txt"
let FileLines = File.ReadAllLines(filePath)
let count = FileLines.Length/25
type ProcessResult = { exitCode : int; stdout : string; stderr : string }
let executeProcess (exe,cmdline) =
let psi = new System.Diagnostics.ProcessStartInfo(exe,cmdline)
psi.UseShellExecute <- false
psi.RedirectStandardOutput <- true
psi.RedirectStandardError <- true
psi.CreateNoWindow <- true
let p = System.Diagnostics.Process.Start(psi, EnableRaisingEvents = true)
let output = new System.Text.StringBuilder()
let error …Run Code Online (Sandbox Code Playgroud) 编辑:我们可以关闭.是不是真的异步,非阻塞javascript不可能?
var PATH = require ("path");
var URL = require ("url");
var sleep = function (ms){
var start = new Date ().getTime ();
while ((new Date ().getTime () - start) < ms);
}
require ("http").createServer (function (req, res){
if (URL.parse (req.url).pathname === "/1"){
console.log ("tab 1: I'm in");
PATH.exists ("test", function (exists){
sleep (5000);
res.writeHead (200, {"Content-Type": "text/plain"});
res.end ("1");
console.log ("tab 1: I'm done");
});
}else{
console.log ("tab 2: I'm in");
res.writeHead (200, {"Content-Type": "text/plain"});
res.end ("2");
console.log …Run Code Online (Sandbox Code Playgroud) 我有一个控制台应用程序执行类方法(在另一个项目中).此方法异步执行对Web服务的POST(这是3分钟的冗长操作)并且具有回调,该回调返回受POST影响的记录数.与此同时,程序继续执行其他不到3分钟的事情,程序在回调返回之前退出.
在过去,我使用了如下所示的ManualResetEvent来防止完成,但在这种情况下,我的方法是在一个不同的类库中,我希望尽可能保持干净.
static ManualResetEvent resetEvent = new ManualResetEvent(false)
static void Main()
{
CallAsyncMethod();
// Do other things...
resetEvent.WaitOne(); // Blocks until "set"
}
void AsyncMethodCallback()
{
// Do processing on completion...
resetEvent.Set(); // Allow the program to exit
}
Run Code Online (Sandbox Code Playgroud)
如果没有用执行标志来污染被调用的类,我会感激任何帮助,想出一个干净的模式来完成这个任务.
尝试使用Blocking Queue和此处给出的代码实现读写器问题.整个事情已编译并运行,但reader()和writer()函数中async {...}内的部分不会产生任何输出.我很确定它们被正确调用,因为即使我是F#的菜鸟,带有此代码的教程页面看起来也很真实.
这是整个代码:
open System
open System.IO
open System.Collections.Generic
//open System.Runtime.Serialization.Formatters.Binary
///defining Agent
type Agent<'T> = MailboxProcessor<'T>
///defining Message
type internal BlockingAgentMessage<'T> =
| Get of AsyncReplyChannel<'T>
| Add of 'T * AsyncReplyChannel<unit>
/// Agent-based implementation of producer/consumer problem
type BlockingQueueAgent<'T>(maxLength) =
let agent = Agent.Start(fun agent ->
let queue = new Queue<_>()
//let queue = new Queue()
// State machine running inside the agent
let rec emptyQueue() =
agent.Scan(fun msg ->
match msg with
| Add(value, …Run Code Online (Sandbox Code Playgroud) 在我的WCF服务合同中,我已经声明了一个返回List的asyn方法,但是当我在我的客户端收到该方法的返回时,我收到一个数组.我的合同如下:
[OperationContract(AsyncPattern = true)]
IAsyncResult BeginGetUsers(Paramusers paramUserParameters, AsyncCallback callback, object state);
List<Users> EndGetUsers(IAsyncResult result);
Run Code Online (Sandbox Code Playgroud)
在我的客户端,我有以下代码:
Task<List<Users>> task = Task<List<Users>>.Factory.FromAsync(_proxy.Proxy.BeginGetUsers, _proxy.Proxy.EndGetUsers, myParameters, null);
List<Users> myUsers = await task;
Run Code Online (Sandbox Code Playgroud)
在客户端中,我在FromAsync方法的第二个参数中收到错误,因为它表示EndGetUsers方法返回一个数组,而不是一个列表.
我尝试使用一组用户并且工作正常,但我想从异步方法接收列表,而不是数组.可能吗?
谢谢.Daimroc.
据我了解,MVC AsyncController专门介绍了在创建新线程时避免从ASP.NET线程池中窃取线程的问题.新的Web API没有类似的东西AsyncApiController.它的继承/实现签名ApiController也Controller和AsyncController.
问题: Web API是否已经处理了在创建新线程时避免从ASP.NET线程池中窃取线程的问题?我错过了一些新的自动处理这个的东西吗?
以供参考:
我正在Android中开发一个聊天应用程序并遇到了一个大问题.我需要一个线程在后台不断运行(轮询服务器),并通过Handle将它附加到我的主进程.
主要问题是:只要这个后台线程正在运行,前台的线程就会完全停止!
这是一个不完整的代码块(因为完整版本更长/更丑)...
public class ChatActivity extends Activity {
...
private Thread chatUpdateTask;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
...
chatUpdateTask = new ChatUpdateTask(handler);
chatUpdateTask.start();
}
public void updateChat(JSONObject json) {
// ...
// Updates the chat display
}
// Define the Handler that receives messages from the thread and update the progress
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
// Get json from the sent Message and display it
updateChat(json);
}
}; …Run Code Online (Sandbox Code Playgroud) 从文档中我意识到我可以使用Caliburn Micro的协同程序进行异步操作.开箱即用,没有额外的技术.所以我在Windows Phone应用程序中实现了下一个代码:
public class SimpleViewModel : Screen
{
// ...
protected override void OnViewLoaded(object view)
{
base.OnViewLoaded(view);
Coroutine.BeginExecute(RunTask());
}
public IEnumerator<IResult> RunTask()
{
yield return new SimpleTask();
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
SimpleTask:
public class SimpleTask : IResult
{
public void Execute(ActionExecutionContext context)
{
Thread.Sleep(10000);
}
public event EventHandler<ResultCompletionEventArgs> Completed;
}
Run Code Online (Sandbox Code Playgroud)
我希望Execute方法中的代码将运行异步.但这不会发生.我的UI线程被阻止了10秒钟.
我犯了哪个错误?或者我对协同程序的异步性质的假设是错误的?
asynchronous ×10
c# ×3
f# ×2
.net ×1
android ×1
arrays ×1
asp.net-mvc ×1
c#-4.0 ×1
c++ ×1
c++11 ×1
callback ×1
coroutine ×1
http ×1
image ×1
ios ×1
java ×1
javascript ×1
list ×1
node.js ×1
objective-c ×1
threadpool ×1
wcf ×1
xcode ×1