在.Net 4.5中,Microsoft添加了新Async/Await功能以简化异步编码.但是,我想知道
Async/Await完全取代旧的使用方式
Threads吗?Async/Await能够做任何一个的Thread可以异步呢?Async/Await只与像一些方法来使用WebClient.DownloadStringAsync或者我可以将任何同步方法,使其使用Async/Await和不阻塞主线程?据我所知,我可以使用TCPListener和Socket创建一个服务器,那么它们之间的区别是什么?
插座
private Socket MainSock;
MainSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
MainSock.Bind(new IPEndPoint(IPAddress.Any, port));
MainSock.Listen(500);
MainSock.BeginAccept(AcceptConnections, new Wrapper());
Run Code Online (Sandbox Code Playgroud)
的TCPListener
Int32 port = 13000;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
TcpListener server = new TcpListener(localAddr, port);
server.Start();
Run Code Online (Sandbox Code Playgroud)
我真的很困惑.他们俩听取了联系,那么它们之间的区别是什么?
更新的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Net.Security;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.IO;
public class Wrapper
{
public byte[] buffer;
public SslStream sslStream;
public object connector;
}
public class Sock
{
private Dictionary<string, byte> Connections; …Run Code Online (Sandbox Code Playgroud) 在WinForms应用程序中,将CheckForIllegalCrossThreadCalls设置为FALSE以避免调试期间的交叉线程错误是否安全?
CheckForIllegalCrossThreadCalls = false;
Run Code Online (Sandbox Code Playgroud) 使用Firebug登录登录过程时,我发现它是这样的
POST //The normal post request
GET //Automatically made after the login
GET //Automatically made after the login
GET //Automatically made after the login
Run Code Online (Sandbox Code Playgroud)
使用下面的代码发出帖子请求时,它没有发出浏览器正在进行的自动GET请求.
我的WebClient处理程序
using System;
using System.Net;
namespace Test
{
class HttpHandler : WebClient
{
private CookieContainer _mContainer = new CookieContainer();
protected override WebRequest GetWebRequest(Uri address)
{
var request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
(request as HttpWebRequest).CookieContainer = _mContainer;
}
return request;
}
protected override WebResponse GetWebResponse(WebRequest request)
{
var response = base.GetWebResponse(request);
if (response …Run Code Online (Sandbox Code Playgroud) 我有一个Windows窗体应用程序,其中一些控件添加到设计器中.当我想要更改某些内容(LIKE)时从Form1.cs中启用一个文本框
我只是用
textBox1.Enabled = true;
Run Code Online (Sandbox Code Playgroud)
但现在我有一个名为class1.cs的分离类
我如何从静态函数class1.cs启用textBox1?
{注意}我没有尝试任何代码,因为我完全不知道这样做.
好吧,我正在尝试创建一个小应用程序,以节省一些员工姓名,年龄和工资.所以我决定用Dictionary它来设定每个员工的工资,我想出了那个代码
var employeeSalaryDictionary = new Dictionary<Employee, int>();
employeeSalaryDictionary.Add(new Employee { Name = "Chuck", Age = 37 }, 1000);
employeeSalaryDictionary.Add(new Employee { Name = "Norris", Age = 37 }, 2000);
employeeSalaryDictionary.Add(new Employee { Name = "Rocks", Age = 44 }, 3000);
Employee employeeToFind = new Employee { Name = "Chuck", Age = 37 };
//or even
Employee employeeToFind = new Employee { Name = "Chuck"};
//Always False...
bool exists = employeeSalaryDictionary.ContainsKey(employeeToFind);
Run Code Online (Sandbox Code Playgroud)
public class Employee
{
public string …Run Code Online (Sandbox Code Playgroud) 我的控制台应用程序正在创建一些运行时文件,所以我想要做的是在应用程序启动时删除所有这些文件.我试过这个:
public static void Empty(string targetDir)
{
var directory = new DirectoryInfo(targetDir);
if (!directory.Exists) return;
foreach (var file in directory.GetFiles()) file.Delete();
foreach (var subDirectory in directory.GetDirectories()) subDirectory.Delete(true);
}
Run Code Online (Sandbox Code Playgroud)
...只是为了查找给定路径中的所有文件/文件夹(位于程序执行路径的子目录中),然后删除它们.但是,我得到以下异常:
访问路径"文件"被拒绝.
我试图以管理员身份运行该程序而没有运气; 但是,我想要一个不使用管理员权限的解决方案.
备注:
在我的应用程序中,我必须使用ExpandoObject才能在运行时创建/删除属性; 但是,我必须将函数的返回的ExpandoObject映射到相应的对象/类.所以我想出了一个小型的Mapper来完成这项工作但有3个问题:
public string Property;.码:
I-实施:
public static class Mapper<T> where T : class
{
#region Properties
private static readonly Dictionary<string, PropertyInfo> PropertyMap;
#endregion
#region Ctor
static Mapper() { PropertyMap = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).ToDictionary(p => p.Name.ToLower(), p => p); }
#endregion
#region Methods
public static void Map(ExpandoObject source, T destination)
{
if (source == null)
throw new ArgumentNullException("source");
if (destination == null)
throw new ArgumentNullException("destination");
foreach (var kv in source)
{
PropertyInfo p; …Run Code Online (Sandbox Code Playgroud) 好吧,我正在使用SQLite,但当我开始执行非查询可能10k +命令[.sql文件].我发现它很慢,最多可能需要10分钟才能结束将信息添加到数据库中.
无论如何这是我的ExecuteNonQuery代码.
public int ExecuteNonQuery(string sql)
{
var cnn = new SQLiteConnection(_dbConnection);
cnn.Open();
var mycommand = new SQLiteCommand(cnn) {CommandText = sql};
int rowsUpdated = mycommand.ExecuteNonQuery();
cnn.Close();
return rowsUpdated;
}
Run Code Online (Sandbox Code Playgroud)
我希望有一种方法可以让它花费几秒钟才能完成.
我有一个List的Task<bool>,我要重复它,这取决于期待已久的结果,我决定是否继续或中断,但具有讽刺意味的foreach只是执行的任务和等待关键字不起作用
这是我的代码
private async void Execute(object sender, EventArgs e)
{
var tList = new List<Task<bool>> { Method1(), Method2()};
foreach (var task in tList)
{
var result = await task;
if(!result)
break;
}
}
public async Task<bool> Method1()
{
await Task.Delay(1000);
Console.WriteLine("Method1");
return false;
}
public async Task<bool> Method2()
{
await Task.Delay(1000);
Console.WriteLine("Method2");
return true;
}
Run Code Online (Sandbox Code Playgroud)
结果:两个函数都是执行.
问题:我怎么能在里面使用foreach?
并提前感谢.
c# ×8
.net ×3
client ×1
dictionary ×1
file ×1
foreach ×1
mapping ×1
serversocket ×1
sockets ×1
sqlite ×1
tcp ×1
tcplistener ×1
webclient ×1
winforms ×1