[转载]How to avoid using() mistake in C#?

转载自http://blog.csdn.net/mikechenhua/article/details/42568893

As we know that in C# using() is suggested to avoid resourceleak or other good purposes, but in some cases it may be not suitable to use it,let me describe some of such kind of scenarios:

Assume that we have one MainForm which is the main windows of our App, which has 3 buttons on the UI and there is one child form ChildForm, which has one close button on the UI:

Design MainForm.cs as following code:

public partial class MainForm : Form

{

public MainForm()

{

InitializeComponent();

}

private void usingChildForm_Click(object sender, EventArgs e)

{

using (ChildForm myForm = new ChildForm())

{

myForm.Show();// Inthis case, the child form will be disposed immediately, the user will have nochance to click Close button on it.

}

}

public static void SyncThreadMethod()

{

Thread.Sleep(10000);

MessageBox.Show("I am in Sync thread method.");

}

private void syncThreadButton_Click(object sender, EventArgs e)

{

using (DispoableThread thrd1 = new DispoableThread(new ThreadStart(SyncThreadMethod)))

{

thrd1.Start();

thrd1.Join();// Inthis case, using() will wait until Sync thread calling is completed, thendispose thrd1. So “I am in Sync thread method.” will befirst, then “I am in Thread Dispose().”

}

}

private void asyncThreadButton_Click(object sender, EventArgs e)

{

using (DispoableThread thrd1 = new DispoableThread(new ThreadStart(SyncThreadMethod)))

{

thrd1.Start();// Inthis case, using will also dispose(abort) thrd1 immediately, so only “I am in Thread Dispose().” is showed.

}

}

}

public class DispoableThread : IDisposable

{

Thread trd = null;

public DispoableThread(ThreadStart ts)

{

trd = new Thread(ts);

}

public void Start()

{

trd.Start();

}

public void Join()

{

trd.Join();

}

public void Dispose()

{

trd.Abort();

MessageBox.Show("I am in Thread Dispose().");

}

}

ChildForm.cs has:

public partial class ChildForm : Form
{
    public ChildForm()
    {
        InitializeComponent();
    }
    private void closeButton_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}

You will found only when button “Using Sync Thread”isclicked, the using() can work correctly as we expected. In the other 2 cases,the using() will dispose the object declared in its scope no matter if the tasksinside its brackets is done or not.

转载自http://blog.csdn.net/mikechenhua/article/details/42568893

时间: 2024-10-10 10:12:49

[转载]How to avoid using() mistake in C#?的相关文章

C++ Primer Chapter 1

When I start reviewing, I thought Chapter is useless. Because the title is "Getting Start" . I thought that is useless. But I found something I miss before, so I review it and found I understand something that confused me. So every parts is usef

MySQL Temporary Table

Summary: in this tutorial, we will discuss about MySQL temporary table and show you how to create, use and drop temporary tables. Introduction to MySQL temporary table In MySQL, a temporary table is a special type of table that allows you to store a

Understanding the Bias-Variance Tradeoff

Understanding the Bias-Variance Tradeoff When we discuss prediction models, prediction errors can be decomposed into two main subcomponents we care about: error due to "bias" and error due to "variance". There is a tradeoff between a m

物化视图的刷新(转载)

转载源自于:http://czmmiao.iteye.com/blog/1827254 物化视图 物化视图是包括一个查询结果的数据库对像,它是远程数据的的本地副本,或者用来生成基于数据表求和的汇总表.物化视图存储基于远程表的数据,也可以称为快照.物化视图可以基于表查询,视图和其它的物化视图.通常情况下,在复制环境下,物化视图被称为主表,在数据仓库中称为明细表.对于复制,物化视图允许你在本地维护远程数据的副本,这些副本是只读的.如果你想修改本地副本,必须用高级复制的功能.当你想从一个表或视图中抽取

Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle) instead

“Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle)instead” 出現這個問題時 使用Window->Android->Lint Error Checking 在 Correctness類別裡,找到ValidFragment ,設定為Ingore就可以了. 转载:http://www.dotblogs.com.tw/newmonke

你应当如何学习C++(以及编程)(转载)

你应当如何学习C++(以及编程)(rev#1) By 刘未鹏(pongba) C++的罗浮宫(http://blog.csdn.net/pongba) Javascript是世界上最受误解的语言,其实C++何尝不是.坊间流传的错误的C++学习方法一抓就是一大把.我自己在学习C++的过程中也走了许多弯路,浪费了不少时间. 为什么会存在这么多错误认识?原因主要有三个,一是C++语言的细节太多.二是一些著名的C++书籍总在(不管有意还是无意)暗示语言细节的重要性和有趣.三是现代C++库的开发哲学必须用

转载Aaron ---- jQuery 2.0.3 源码分析core - 选择器

jQuery 2.0.3 源码分析core - 选择器(02) 声明:本文为原创文章,如需转载,请注明来源并保留原文链接Aaron,谢谢! 打开jQuery源码,一眼看去到处都充斥着正则表达式,jQuery框架的基础就是查询了,查询文档元素对象,所以狭隘的说呢,jQuery就是一个选择器,并这个基础上构建和运行查询过滤器! 工欲善其事,必先利其器,所以先从正则入手 我们来分解一个表达式 // A simple way to check for HTML strings // Prioritize

MVC之前的那点事儿系列(8):UrlRouting的理解(转载)

MVC之前的那点事儿系列(8):UrlRouting的理解 文章内容 根据对Http Runtime和Http Pipeline的分析,我们知道一个ASP.NET应用程序可以有多个HttpModuel,但是只能有一个HttpHandler,并且通过这个HttpHandler的BeginProcessRequest(或ProcessRequest)来处理并返回请求,前面的章节将到了再MapHttpHandler这个周期将会根据请求的URL来查询对应的HttpHandler,那么它是如何查找的呢?

探秘Java中的String、StringBuilder以及StringBuffer(转载)

探秘Java中String.StringBuilder以及StringBuffer 相信String这个类是Java中使用得最频繁的类之一,并且又是各大公司面试喜欢问到的地方,今天就来和大家一起学习一下String.StringBuilder和StringBuffer这几个类,分析它们的异同点以及了解各个类适用的场景.下面是本文的目录大纲: 一.你了解String类吗? 二.深入理解String.StringBuffer.StringBuilder 三.不同场景下三个类的性能测试 四.常见的关于