本文整理汇总了C#中Future.SetResult方法的典型用法代码示例。如果您正苦于以下问题:C# Future.SetResult方法的具体用法?C# Future.SetResult怎么用?C# Future.SetResult使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Future
的用法示例。
在下文中一共展示了Future.SetResult方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: IssueRequest
public static IEnumerator<object> IssueRequest(HttpWebRequest request)
{
var fResponse = new Future<HttpWebResponse>();
ThreadPool.QueueUserWorkItem(
(__) => {
try {
request.BeginGetResponse(
(ar) => {
try {
var _ = (HttpWebResponse)request.EndGetResponse(ar);
fResponse.SetResult(_, null);
} catch (Exception ex) {
fResponse.SetResult(null, ex);
}
}, null
);
} catch (Exception ex_) {
fResponse.SetResult(null, ex_);
}
}
);
yield return fResponse;
if (fResponse.Failed)
throw new RequestFailedException(fResponse.Error);
using (var response = fResponse.Result) {
var fResponseStream = Future.RunInThread(
() => response.GetResponseStream()
);
yield return fResponseStream;
Encoding encoding = AsyncTextReader.DefaultEncoding;
if (!string.IsNullOrEmpty(response.CharacterSet))
encoding = Encoding.GetEncoding(response.CharacterSet);
string responseText;
using (var stream = fResponseStream.Result)
using (var adapter = new AsyncTextReader(new StreamDataAdapter(stream, false), encoding)) {
var fText = adapter.ReadToEnd();
yield return fText;
responseText = fText.Result;
}
var cookies = new Cookie[response.Cookies.Count];
response.Cookies.CopyTo(cookies, 0);
yield return new Result(new Response {
Body = responseText,
ContentType = response.ContentType,
StatusCode = response.StatusCode,
StatusDescription = response.StatusDescription,
Cookies = cookies
});
}
}
示例2: TestGetResultMethodNeverThrows
public void TestGetResultMethodNeverThrows () {
var f = new Future<object>();
object result;
Exception error;
Assert.IsFalse(f.GetResult(out result, out error));
f.SetResult(5, null);
Assert.IsTrue(f.GetResult(out result, out error));
Assert.AreEqual(5, result);
f = new Future<object>();
f.SetResult(null, new Exception("earth-shattering kaboom"));
Assert.IsTrue(f.GetResult(out result, out error));
Assert.IsTrue(error is Exception);
}
示例3: AsyncExecuteReader
public static Future<IDataReader> AsyncExecuteReader (this IDbCommand cmd) {
var f = new Future<IDataReader>();
ThreadPool.QueueUserWorkItem(
(WaitCallback)(
(state) => {
try {
IDataReader result;
result = cmd.ExecuteReader();
f.SetResult(result, null);
} catch (Exception e) {
f.Fail(e);
}
}
)
);
return f;
}
示例4: AsyncExecuteScalar
public static IFuture AsyncExecuteScalar (this IDbCommand cmd) {
var f = new Future<object>();
ThreadPool.QueueUserWorkItem(
(WaitCallback)(
(state) => {
try {
object result;
result = cmd.ExecuteScalar();
f.SetResult(result, null);
} catch (Exception e) {
f.Fail(e);
}
}
)
);
return f;
}
示例5: ShowNew
public static IEnumerator<object> ShowNew(Game game, string description, IEnumerable<object> items, SpriteFont font = null)
{
var f = new Future<string>();
var menu = new Menu(game, description, f);
try {
menu.Font = font ?? menu.Font;
foreach (var item in items) {
if (item is IMenuItem) {
menu.Items.Add((IMenuItem)item);
} else if (item is string) {
var menuItem = new TextMenuItem {
Text = (string)item,
Handler = (i) => {
menu.Close();
f.SetResult(i.Text, null);
}
};
menu.Items.Add(menuItem);
} else {
throw new InvalidOperationException("Menu items must be strings or IMenuItem instances");
}
}
menu.Cancelled += () => { f.SetResult(null, null); };
while (!game.InputControls.Available)
yield return new WaitForNextStep();
game.Components.Add(menu);
yield return f;
} finally {
game.Components.Remove(menu);
menu.Dispose();
}
yield return new Result(f.Result);
}
示例6: ReadToEnd
public Future<byte[]> ReadToEnd()
{
var length = (int)(BaseStream.Length - BaseStream.Position);
var bytes = new byte[length];
var result = new Future<byte[]>();
var f = Read(bytes, 0, length);
f.RegisterOnComplete(
(_) => {
int bytesRead;
Exception error;
f.GetResult(out bytesRead, out error);
if ((error == null) && (bytesRead == bytes.Length))
result.SetResult(bytes, null);
else
result.SetResult(null, error);
}
);
return result;
}
示例7: FindProcessWindow
public static IntPtr FindProcessWindow(int processId, string className, string windowName)
{
var result = new Future<IntPtr>();
EnumWindowsProc callback = (hWnd, lParam) => {
int windowProcessId;
GetWindowThreadProcessId(hWnd, out windowProcessId);
if (windowProcessId != processId)
return true;
if ((windowName != null) && (GetWindowTextString(hWnd) != windowName))
return true;
if ((className != null) && (GetWindowClassString(hWnd) != className))
return true;
result.SetResult(hWnd, null);
return false;
};
EnumWindows(callback, IntPtr.Zero);
if (result.Completed)
return result.Result;
else
return IntPtr.Zero;
}
示例8: ConnectAsync
// FIXME: Move this into Task.IO or Task.Sockets, maybe?
private static Future<Socket> ConnectAsync(EndPoint endPoint)
{
var fResult = new Future<Socket>();
var socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.IP);
socket.BeginConnect(endPoint, (ar) => {
try {
socket.EndConnect(ar);
fResult.SetResult(socket, null);
} catch (Exception exc) {
fResult.SetResult(null, exc);
socket.Dispose();
}
}, null);
return fResult;
}