我看到CreateFile函数接受FILE_FLAG_OVERLAPPED参数以使文件不阻塞。但是,如何使CreateFile调用本身不阻塞?
答案 0 :(得分:2)
不幸的是,public static List<AccountCurrencies> GetAccountCurrencies()
{
if (Settings.Default.APIKey == null || Settings.Default.APISecret == null) return new List<AccountCurrencies>();
var nonce = (int) (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds; // same as time() in PHP, need to integrate it
var encoding = Encoding.UTF8;
var urlForAuth = @"https://bittrex.com/api/v1.1/account/getbalances?apikey=" + Settings.Default.APIKey + "&nonce=" + nonce;
var result = Gethmacsha512(encoding, Settings.Default.APISecret, urlForAuth);
// some var for the request
var account = new List<AccountCurrencies>();
// sending it to get the response
var request = (HttpWebRequest)WebRequest.Create(urlForAuth);
request.Headers.Add("apisign",result);
//request.Headers.Add("nonce", nonce.ToString());
request.ContentType = "application/json";
var response = (HttpWebResponse)request.GetResponse();
var stream = response.GetResponseStream();
Resp.GetValue(stream, account);
return account;
}
private static string Gethmacsha512(Encoding encoding, string apiSecret, string url)
{
// doing the encoding
var keyByte = encoding.GetBytes(apiSecret);
string result;
using (var hmacsha512 = new HMACSHA512(keyByte))
{
hmacsha512.ComputeHash(encoding.GetBytes(url));
result = ByteToString(hmacsha512.Hash);
}
return result;
}
static string ByteToString(IEnumerable<byte> buff)
{
return buff.Aggregate("", (current, t) => current + t.ToString("X2"));
}
是同步的。如果您需要它是非阻塞的,那么您可能正在尝试在UI线程中执行I / O.避免这样做。
你没有提到编程语言,所以我认为它是C ++。您可以使用the standard library's threading facilities将I / O密集型工作卸载到工作线程中。例如,您可以将其包装在packaged_task或async。
中