How to avoid using() mistake in C#?

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.

Geometric analysis is the study of geometric objects using analytical tools and techniques. As with any field of study, errors can occur in the analysis of geometric objects. Some common errors in geometric analysis include: 1. Algebraic errors: These occur when mathematical calculations are performed incorrectly, leading to incorrect results. For example, a mistake in simplifying an equation or in solving a system of equations can result in an incorrect analysis. 2. Computational errors: These occur when numerical calculations are performed incorrectly, leading to incorrect results. For example, a rounding error or a mistake in entering data into a computer program can result in an incorrect analysis. 3. Geometric errors: These occur when the geometric object being analyzed is not properly defined or understood. For example, a geometric object may be incorrectly assumed to be symmetrical when it is not, leading to incorrect conclusions. 4. Assumption errors: These occur when assumptions are made about the geometric object being analyzed that are not true. For example, assuming that a geometric object is flat when it is actually curved can lead to incorrect conclusions. To avoid errors in geometric analysis, it is important to carefully define and understand the geometric object being analyzed, to perform mathematical and numerical calculations accurately, and to question any assumptions that are made about the object. Additionally, it can be helpful to double-check calculations and interpretations, and to seek feedback from others in the field.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值