尝试理解TPL和async/ await在线程创建方面的区别.
我相信TPL(TaskFactory.StartNew)的工作方式类似于ThreadPool.QueueUserWorkItem它在线程池中的线程上排队工作.当然,除非您使用TaskCreationOptions.LongRunning哪个创建新线程.
我认为async/ await会以同样的方式工作:
TPL:
Factory.StartNew( () => DoSomeAsyncWork() )
.ContinueWith(
(antecedent) => {
DoSomeWorkAfter();
},TaskScheduler.FromCurrentSynchronizationContext());
Run Code Online (Sandbox Code Playgroud)
Async/ Await:
await DoSomeAsyncWork();
DoSomeWorkAfter();
Run Code Online (Sandbox Code Playgroud)
会是相同的.从我一直在阅读它似乎async/ await只有"有时"创建一个新的线程.那么什么时候创建一个新线程,什么时候不创建一个新线程呢?如果您正在处理IO完成端口,我可以看到它不必创建新线程,但我认为它必须.我想我FromCurrentSynchronizationContext对此的理解总是有点模糊.我总是认为它本质上是UI线程.
我的VS解决方案中有几个项目.每当我将"System.Net.Http"NuGet包添加到它显示为4.2.0.0版本的包时.然后我也这样做并添加相同的NuGet包,然而,另一个说版本.4.1.1.2
然后我收到警告:
发现System.Net.Http之间存在冲突
EDIT1:
Gathering dependency information took 1.7 sec
Attempting to resolve dependencies for package 'System.Net.Http.4.3.3' with DependencyBehavior 'Lowest'
Resolving dependency information took 0 ms
Resolving actions to install package 'System.Net.Http.4.3.3'
Resolved actions to install package 'System.Net.Http.4.3.3'
Retrieving package 'System.Net.Http 4.3.3' from 'nuget.org'.
Adding package 'System.Net.Http.4.3.3' to folder 'C:\...Service\packages'
Added package 'System.Net.Http.4.3.3' to folder 'C:\...Service\packages'
Added package 'System.Net.Http.4.3.3' to 'packages.config'
Successfully installed 'System.Net.Http 4.3.3' to ....Service
Executing nuget actions took 2.05 sec
Time Elapsed: 00:00:03.8937113
Run Code Online (Sandbox Code Playgroud)
请注意安装了正确的版本,但是=> Props =>版本说4.1.1.2
在Close上一个方法ICommunicationObject可以抛出两种类型的异常如MSDN概述这里.我理解为什么该Close方法可以抛出这些异常,但我不明白为什么Dispose服务代理上的Close方法在没有try它的情况下调用该方法.你的Dispose方法不是你想要的地方,确保你没有抛出任何例外吗?
我编写了一个应用程序,它需要下载*.png文件并将其设置为按钮的背景WPF.所以,当我运行这个程序时,它面临错误
没有找到适合完成此操作的成像组件."
我的代码如下:
第一个应用程序使用WebClient类的对象下载文件:
System.Net.WebClient wClient = new System.Net.WebClient();
Uri downloadUri = new Uri(MyUri, UriKind.Absolute);
wClient.DownloadFileAsync(downloadUri, "MyImage.png");
wClient.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(wClient_DownloadFileCompleted);
Run Code Online (Sandbox Code Playgroud)
当下载完成事件发生时:
ImageBtn.Dispatcher.Invoke(new Action(() =>
{
ImageBrush ib = new ImageBrush();
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri("MyImage.png", UriKind.Relative);
bi.EndInit();
ib.ImageSource = bi;
ImageBtn.Background = ib;
}
Run Code Online (Sandbox Code Playgroud)
注意
因为运行这些代码块BackgroundWorker,我Dispatcher用来设置按钮Background属性
所以,当我运行程序时,System.NotSupportedException发生如下:
来自HRESULT的异常:0x88982F50错误代码:-2003292336消息:未找到适合完成此操作的映像组件.来源:PresentationCore Stack Trace:在System.Windows.Media的System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream(Uri uri,Stream stream,BitmapCacheOption cacheOption,Guid&clsId,Boolean&isOriginalWritable,Stream&uriStream,UnmanagedMemoryStream&unmanagedMemoryStream,SafeFileHandle&safeFilehandle).在System.Windows.Media.Imaging的System.Windows.Media.Imaging.BitmapImage.FinalizeCreation()处的Imaging.BitmapDecoder.CreateFromUriOrStream(Uri baseUri,Uri uri,Stream stream,BitmapCreateOptions createOptions,BitmapCacheOption cacheOption,RequestCachePolicy uriCachePolicy,Boolean insertInDecoderCache). BitmapSource.CompleteDelayedCreation()
在System.Windows.Media.Imaging.BitmapSource.get_WicSourceHandle() …
我DataTable有三个从数据库中提取的列,而我只需将它的两列绑定到一个DataGridView.你能帮帮我吗?
首先,我是MVC的新手,所以请原谅这个问题是否基本.
我正在使用自定义路由创建以下URL(http:// mysite/subscriber/12345),其中12345是订户号.我希望它ShowAll在Subscriber控制器中运行动作.我的路由是使用Phil的路由调试器触发,当我传入上面的url时,路由调试器将ID显示为12345.我的控制器正在接受一个int为subscriberID.当它触发时,控制器抛出错误
参数字典包含非可空类型"System.Int32"的参数"id"的空条目.
为什么路由调试器显示一个值而控制器没有看到它?
这是我的路线(第一个是罪魁祸首)
routes.MapRoute(
"SubscriberAll",
"subscriber/{id}",
new { controller = "Subscriber", action = "ShowAll", id=0 },
new { id = @"\d+" } //confirm numeric
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Run Code Online (Sandbox Code Playgroud)
知道我为什么在ShowAll行动中得到空?这是动作方法签名:
public ActionResult ShowAll(int id)
Run Code Online (Sandbox Code Playgroud) 我想格式化标准的.NET DateTime字符串,如下所示
2014-01-01 => 2014年1月
我可以通过使用获得日期格式"2014年1月" ToString("y")但是如何缩短月份?
这甚至可能吗?
C#Visual Studio 2012 .NET 4.5
我的 ASP.NET MVC 核心应用程序应该通过实体框架连接到现有的 MSSQL LocalDB 文件。
数据库优先开发需要对现有数据库进行逆向工程。按照官方文档中的说明,我在 NuGet 包管理器控制台中运行以下命令:
Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=DatabaseName;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
Run Code Online (Sandbox Code Playgroud)
但是我得到
No design-time services were found.
System.Data.SqlClient.SqlException (0x80131904): Cannot open database "DatabaseName" requested by the login. The login failed.
Login failed for user 'MACHINE-NAME\UserName'.
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling)
Error Number:4060,State:1,Class:11
Cannot open database "DatabaseName" requested by the login. The login failed.
Login failed for user 'MACHINE-NAME\UserName'.
Run Code Online (Sandbox Code Playgroud) sql-server localdb entity-framework-core asp.net-core-mvc asp.net-core
这是我第一个使用WCF的Web应用程序.所以请指导我作为一个新人.
我正在尝试使用WCF回调来实现发布/订阅模式.我想将UserA的消息从UserB发送给UserB或UserA.我从这里得到了一个例子.
在我的应用程序中,我使用ASP.NET作为客户端来连接WCF服务,我在订阅WCF服务时发现了一个问题.
WCF服务不包含任何其他客户端对象.因此,当我打电话时GetAllClients(_guid),它将只返回一个本身的客户端.
这是ASP.NET页面中的代码(我把每个控件放在里面updatePanel)
public partial class _Default : System.Web.UI.Page, AlertServiceCallback
{
private AlertServiceClient _client;
private Guid _guid = Guid.NewGuid();
protected void Page_Load(object sender, EventArgs e)
{
InstanceContext context = new InstanceContext(this);
_client = new AlertServiceClient(context);
_client.RegisterClient(_guid);
}
protected void btnGetClients_Click(object sender, EventArgs e)
{
//Try to retrive all active clients
Client[] cs = _client.GetAllClients(_guid);
List<Client> list = new List<Client>(cs);
//Bind to dropDownList to display all active clients
ddlClients.DataSource = list;
ddlClients.DataBind();
} …Run Code Online (Sandbox Code Playgroud) c# ×3
.net ×2
wcf ×2
asp.net ×1
asp.net-core ×1
asp.net-mvc ×1
async-await ×1
binding ×1
c#-5.0 ×1
callback ×1
datagridview ×1
datatable ×1
format ×1
localdb ×1
nuget ×1
sql-server ×1
web-services ×1
wpf ×1