假设以下代码:
using (SqlConnection conn = new SqlConnection(connectionString))
{
...
using (SqlCommand comm = new SqlCommand(...))
{
.. do stuff ..
if(condition) Response.Redirect("somepage.aspx");
}
}
Run Code Online (Sandbox Code Playgroud)
Response.Redirect() 从 using 块退出会导致它处理所有连接吗?
或者,有没有办法退出不会导致处置的 using 块?
编辑:我不想在没有处置的情况下退出。我想知道任何会导致它不起作用的陷阱。-- 当然,除非崩溃,但我很确定所有对象都被处理了 -- 在这种情况下,很难
我接受了一个答案,基本上说“我不知道”,但这是一个经过充分研究的“我不知道”
同时,我将假设 Response.Redirect 中止 using 语句和代码并考虑到这一点。——除非另有证明。
我正在对推文进行文本挖掘,我遇到了重复推文的问题,如下所示:
“aeCERT:aeCERT 为选民、执法部门、学术部门和公众提供持续的信息安全意识计划。”
“Salim_aeCERT:aeCERT 为选民、执法部门、学术部门和公众提供持续的信息安全意识计划。”
我从两个不同的帐户收到了相同的推文,如何从我的数据集中删除一条推文?我已经尝试过这段代码,但重复的推文仍然出现:
tweets1.df <- do.call("rbind", lapply(tweets, as.data.frame))
tweets2.df <- tweets1.df[duplicated(tweets1.df ) == FALSE,]
dim(tweets2.df)
Run Code Online (Sandbox Code Playgroud)
如何删除不同帐户中的重复推文?
我有一段代码,简化了一点,相当于以下编译和正常工作.
template <typename Interface, typename... Args>
struct factory_function {
typedef function<shared_ptr<Interface> (Args...)> type;
};
template <typename Interface, typename Implementer, typename... Args>
shared_ptr<Interface> create_function(Args... args) {
return make_shared<Implementer>(args...);
}
template <typename Interface, typename... Args>
int register_factory(identifier id, typename factory_function<Interface, Args...>::type factory) {
}
int main(int argc, char *argv[]) {
register_factory<Iface>(1000, create_function<Iface, Impl>);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是当尝试using ... =在这样的结构中使用较新的构造而不是typedef时:
template <typename Interface, typename... Args>
using factory_function = function<shared_ptr<Interface> (Args...)>;
Run Code Online (Sandbox Code Playgroud)
然后typename factory_function<Interface, Args...>::type改成factory_function<Interface, Args...>,我得到一个编译错误:
foo.cc: In function …Run Code Online (Sandbox Code Playgroud) 我想使用using语句,但如果它应该指向的对象不存在,则可能需要更改我“使用”的变量的值。
我想到了这样的事情(用于注册表访问和 32/64 窗口 - 尽管这是我当前的用例,但这是一个普遍问题):
using (var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\MS\Platform"))
{
if (key == null)
key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\MS\Platform");
// use key
}
Run Code Online (Sandbox Code Playgroud)
上面的代码不能编译:
error CS1656: Cannot assign to 'key' because it is a 'using variable'
Run Code Online (Sandbox Code Playgroud)
我可以通过不使用using而是 try/catch/finally 和/或在使用之前测试注册表项是否存在来解决此问题。
有没有办法继续使用using,然后处理正确的对象?
当以下裸机代码正在发送或接收数据时,客户端会断开连接。
我的理解是 using 块处理它创建的对象,即 NetworkStream 对象,但为什么 TcpClient Socket 断开连接?
控制台输出是... True False
class Program
{
static void Main(string[] args)
{
Console.Title = "Client";
Process p = Process.Start(@"C:\Users\Teddy\Documents\visual studio 2015\code\TesyingNetworkStream\Server\bin\Debug\server.exe");
Thread.Sleep(1000);
IPEndPoint EP = new IPEndPoint(
IPAddress.Parse("192.168.1.10"), 4000
);
TcpClient cli = new TcpClient();
cli.Connect(EP);
UseClient(cli);
Console.ReadLine();
p.Kill();
p.Close();
}
private static void UseClient(TcpClient cli)
{
using (NetworkStream ns = cli.GetStream())
{
Console.WriteLine(cli.Connected);//True
}
Console.WriteLine(cli.Connected);//False
}
}
Run Code Online (Sandbox Code Playgroud)
如果重要,这里是服务器代码。
class Program2
{
static void Main(string[] args)
{
Console.Title = "Server";
TcpListener lis …Run Code Online (Sandbox Code Playgroud) 当我想在 python 中导入一个包时,我可以给它起别名:
import package_with_a_very_long_nameeeeee as pl
Run Code Online (Sandbox Code Playgroud)
在该语句之后,我可以通过它的别名来引用该包:
pl.foo()
Run Code Online (Sandbox Code Playgroud)
朱莉娅允许我做:
using PackageWithAVeryLongName
pl = PackageWithAVeryLongName
pl.foo()
Run Code Online (Sandbox Code Playgroud)
但这感觉就像是一个丑陋的黑客,可能有我不明白的含义。
在 julia 中是否有一种惯用的方法来为导入的包添加别名?
那么,问题是为什么在 using 块中使用 HttpClient 是错误的,但在 WebApi 上下文中呢?
我一直在阅读这篇文章“不要阻止异步代码”。其中我们有以下示例:
public static async Task<JObject> GetJsonAsync(Uri uri)
{
// (real-world code shouldn't use HttpClient in a using block; this is just example code)
using (var client = new HttpClient())
{
var jsonString = await client.GetStringAsync(uri);
return JObject.Parse(jsonString);
}
}
// My "top-level" method.
public class MyController : ApiController
{
public string Get()
{
var jsonTask = GetJsonAsync(...);
return jsonTask.Result.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
评论// (real-world code shouldn't use HttpClient in a using …
可能令人尴尬的问题,但显然有一些我想要/需要知道的东西.
我希望以下代码创建一个新的表行,其中包含稍后要呈现的新单元格.而这就是它所做的......正如你所期望的那样.
using (TableRow tr = new TableRow())
{
using (TableCell td = new TableCell())
{
td.Text = "Column A";
tr.Cells.Add(td);
}
using (TableCell td = new TableCell())
{
td.Text = "Column B";
tr.Cells.Add(td);
}
tbl.Rows.Add(tr);
}
Run Code Online (Sandbox Code Playgroud)
但....但是,一旦它们走出'使用'范围,使用语句中创建的TD是否会失效?此行引用的TD对象现在是否无效,并且在尝试使用它时该行是否应该失败?TR由'tbl'对象渲染时也可以这样说.
我不明白处置?
我不明白使用?
TableRow.Cells.Add()实际上是在做一个深层复制而不仅仅是一个ref ptr复制吗?
TableCell实际上可以在它被处理后使用吗?
是什么赋予了?
我看到了这个:
using (StreamWriter sw = new StreamWriter("file.txt"))
{
// d0 w0rk s0n
}
Run Code Online (Sandbox Code Playgroud)
我尝试查找信息的所有内容都没有解释这是做什么的,而是给了我关于命名空间的东西.
有很多像这样的代码块:
public class SomeController : Controller
{
DbEntities entity = new DbEntities();
public ActionResult Add()
{
entity.someOperations...
return View();
}
public ActionResult Edit()
{
entity.someOperations...
return View();
}
public ActionResult Detail()
{
entity.someOperations...
return View();
}
public ActionResult Detail()
{
entity.someOperations...
return View();
}
.....
Run Code Online (Sandbox Code Playgroud)
我应该改变这样的方法吗?:
public class SomeController : Controller
{
public ActionResult Add()
{
using(DbEntities entity = new DbEntities())
{
entity.someOperations...
}
return View();
}
.....
Run Code Online (Sandbox Code Playgroud)
不使用using-statementEF 有什么问题?或者最好的方法是什么?另外,如果我们使用using-statement代码块增长.
谢谢...
using ×10
c# ×5
.net ×3
asp.net-mvc ×1
c++ ×1
c++11 ×1
disconnect ×1
dispose ×1
httpclient ×1
julia ×1
r ×1
templates ×1
typedef ×1
vb.net ×1
webapi ×1