|
d@nish wrote: Finally is executed always.
Errm. No it's not. In the case of a StackOverflowException and an OutOfMemoryException , the program terminates abnormally and the exception cannot be caught, so the finally does not get executed.
|
|
|
|
|
Also not if you call System.Environment.FailFast (which you shouldn't).
|
|
|
|
|
As d@nish has said the finally block takes care of them.
However, as both of the classes implement IDisposable , you should consider using the using construct which would mean that you will not need the Finally block.
using (SqlConnection myConnection = new SqlConnection(...............))
{
...............................
...............................
...............................
myConnection.whatever = ?????
try
{
...............................
...............................
...............................
using (SqlDataReader myReader = new SqlDataReader(.......))
{
while (myReader.Read())
{
...............................
...............................
}
}
}
catch (....)
{
...............................
}
}
The using[^] construct guarantees disposal.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Right. That fully stops finally...
|
|
|
|
|
Or, it stops finally fully.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
So what is the real benefit of a using statement vs doing something like this:
SqlConnection conn = new SqlConnection("blah");
SqlCommand cmd = new SqlCommand("blah", conn);
try
{
SqlDataReader r = cmd.ExecuteReader();
while (r.Read())
{
}
r.Close();
}
catch (Exception ex)
{
}
finally
{
cmd.Dispose();
conn.Close();
conn.Dispose();
}
It was my understanding that the using statement after it has been compiled just automatically generated a try, catch, finally statement. The only thing is you can't really control much of what happens when the exception is catch (like write to a custom log file, or system events)
|
|
|
|
|
Please understand that I am by no means an expert in this regard (well, almost any regard, actually ) but my understanding is that it does the equivalent of a nested try-finally block, whether the IL is identical I'm not sure, so that the resource are released earlier rather than waiting for the main t-c-f block to end. I think that anything that avoids nested t-c-f is preferable as they are difficult to read and just plain ugly.
From the MSDN page
The using statement allows the programmer to specify when objects that use resources should release them.
Also from MSDN
A using statement can be exited either when the end of the using statement is reached or if an exception is thrown and control leaves the statement block before the end of the statement.
I have always taken this to mean that disposal will take place even if one of the Exceptions mentioned by PO'H above occurs. It is always possible that I am wrong on that count though.
[Edit]
I have just found this[^] article which, would seem to confirm your assertion that using generates a try-finally in the IL code. So the only real benefit would be tighter control over when resources are disposed and slightly neater, IMO, code.
[/Edit]
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
modified on Sunday, November 14, 2010 8:47 PM
|
|
|
|
|
Jacob D Dixon wrote: It was my understanding that the using statement after it has been compiled just automatically generated a try, catch, finally statement. The only thing is you can't really control much of what happens when the exception is catch (like write to a custom log file, or system events)
You're correct that you can't control what happens in the catch - which I suppose is why a catch is *not* generated. A try/finally is generated. The overloaded "using" keyword in this context is a shorthand for not having to code a try/finally block. But if you have an exception to catch, you still have to catch it.
|
|
|
|
|
Ahh kk. Thanks! I guess if you were not worried about catching any exceptions then this would be pretty good to use. Personally I like to do something with any exception that is throw.. even if it is just logging it for debugging purposes.
|
|
|
|
|
Jacob D Dixon wrote: Personally I like to do something with any exception that is throw.. even if it is just logging it for debugging purposes.
You mean for "debugging" of release code? Yeah, that is helpful for problems in the field that are not easily recreateable at your desk. Or possibly for giving the user a somewhat prettier crash. There are situations where the app simply has to crash, and in fact is better off crashing than continuing.
|
|
|
|
|
what does it mean by pixel's intensities in grayscale image?
|
|
|
|
|
The intensity of each pixel is variable. In color image systems, a color is typically represented by three or four component intensities such as red, green, and blue, or cyan, magenta, yellow, and black.
Black, white and gray pixels are also made of these components.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
Visit the Hindi forum here.
|
|
|
|
|
|
For a simple game I'm developing I would like to play background music and sound effects.
I have tried a different methods to play two sounds at the same time with .NET SoundPlayer class, but with no result. At best, with use of .NET BackgroundWorker class, I could get the background music to play with no sound effect. Or only sound effects and no background music.
Anyway, I googled and searched for some solutions and found SDK library which I have implemented in my test application.
This is my test application:
* A Windows Form Application with two buttons
* Background music button, (btnBackgroundTrack_Click)
* Sound effect button, (btnEffectTrack_Click)
* The two music tracks are stored as UnmanagedMemoryStream type objects in a resource file
* "Resource.Background", the background music .wav-file, plays for 48 seconds
* "Resource.Item", the sound effect .wav-file, plays less than 1 second
This is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;
using Microsoft.DirectX.DirectSound;
public partial class Form1 : Form
{
BackgroundWorker effectPlayer;
SoundPlayer effect;
public Form1()
{
InitializeComponent();
effectPlayer = new BackgroundWorker();
effectPlayer.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
effect = new SoundPlayer(Resource.Item);
}
void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
effect.PlaySync();
}
private void btnBackgroundTrack_Click(object sender, EventArgs e)
{
Device device = new Device(DSoundHelper.DefaultPlaybackDevice);
device.SetCooperativeLevel(this, CooperativeLevel.Normal);
SecondaryBuffer secondaryBuffer = new SecondaryBuffer(Resource.Background, device);
secondaryBuffer.Play(0, BufferPlayFlags.Default);
}
private void btnEffectTrack_Click(object sender, EventArgs e)
{
if(!effectPlayer.IsBusy)
effectPlayer.RunWorkerAsync();
}
}
This solution kinda works. The background music is played when I hit the "background" button, and if I hit the effect button it's plays as well.
Problem 1: If I click the effect button very fast and often while the background music is playing, all sounds from the program is termenated.
Problem 2: I would like the the background music to repeat it self when it's over.
Any solutions?
|
|
|
|
|
hey guys..i try to make a login panel and authorization..first i created a StoredProcedure like below
ALTER proc [dbo].[sp_KullaniciYetkiDegistir]
(
@KullaniciNo int,
@Yetki0 int,
@Yetki1 int,
@Yetki2 int,
@Yetki3 int,
@Yetki4 int
)
As
Begin
update Izin set ErisebilirMi = @Yetki0 where KullaniciNo = @KullaniciNo and YetkiNo = 1
update Izin set ErisebilirMi = @Yetki1 where KullaniciNo = @KullaniciNo and YetkiNo = 2
update Izin set ErisebilirMi = @Yetki2 where KullaniciNo = @KullaniciNo and YetkiNo = 3
update Izin set ErisebilirMi = @Yetki3 where KullaniciNo = @KullaniciNo and YetkiNo = 4
update Izin set ErisebilirMi = @Yetki4 where KullaniciNo = @KullaniciNo and YetkiNo = 5
End
in here i give some authority according to the KullaniciNo(here Erisebilirmi column is set as bit..so i can give or not give authority to the user)...and in my button's click i wrote like that
cmd = new SqlCommand("sp_KullaniciYetkiDegistir", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@KullaniciNo", System.Data.SqlDbType.Int).Value = KullaniciNo;
cmd.Parameters.AddWithValue("@Yetki0", System.Data.SqlDbType.Bit).Value = chkAdmin.Checked;
cmd.Parameters.AddWithValue("@Yetki1", System.Data.SqlDbType.Bit).Value = chkKullaniciEkle.Checked;
cmd.Parameters.AddWithValue("@Yetki2", System.Data.SqlDbType.Bit).Value = chkKullaniciSil.Checked;
cmd.Parameters.AddWithValue("@Yetki3", System.Data.SqlDbType.Bit).Value = chkYekilendir.Checked;
cmd.Parameters.AddWithValue("@Yetki4", System.Data.SqlDbType.Bit).Value = chkProjeGor.Checked;
if (cmd.ExecuteNonQuery() == 1)
MessageBox.Show("Authorities Are Set", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
else
MessageBox.Show("Authorization Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
here cmd.ExecuteNonQuery() drops directly Else block..what is wrong here??
any help will be appreciated
|
|
|
|
|
Your sproc updates a number of things, and that would cause ExecuteNonQuery to return an int larger than 1. How about assigning the result to a variable and displaying that, just to make sure?
I are Troll
|
|
|
|
|
thanks friend..i did what u said and i noticed that i made mistake in my if block..so it was working already but it is seen like it gives errror because of my wrong idea
thanks for help
|
|
|
|
|
You're welcome
|
|
|
|
|
Guys,
I hope this is the community , where people having experience in developing products, I am a Rookie coming slowly into Industry. I am developing a product for free ware on C#-Windows, I want some skins for better apperance, I googled a lot but no result, Please help me some skins for my UI, or some links. so that my UI looks rich...
Thanks in Advance
|
|
|
|
|
|
Hi,
try to check following link:
http://www.sunisoft.com/irisskin/
but unfortunately this components are not free. when you want to make you application much more "attractive" consider to migrate on WPF.
Regards
Robert
|
|
|
|
|
|
|
Write a macro, and start the macro from within the IDE
I are Troll
|
|
|
|
|
Hello,
how is it possible to check, that the client is connected, before i response to that? I have a lot time-intensive tasks, doing with HttpRequests and i don't want to work in my application and the client is disconnected.
Is there any way?
Thank you
|
|
|
|