Q:
Closing the form
When I run my program, I need to close the form after two hours, in the timer event, I use:
var form = Application.OpenForms[0] as frmOperacao;
form.Close();
But, my problem is that, when I run my program the first time, I receive the error, in the timer event:
Object reference not set to an instance of an object.
My error is in this line:
var form = Application.OpenForms[0] as frmOperacao;
I'm using.NET 4.0
A:
As a suggestion, you should not use Application.OpenForms.
This collection is an enumeration of the forms in the current application. It represents a snapshot of the collection at a particular time; you can't add forms to this collection or remove them from it. It is not necessarily a snapshot of the current state of the application. The snapshot is taken before any code in the application has a chance to modify the forms collection.
It is better to use Application.OpenForms directly. If there are no forms open, then OpenForms will be null.
This code is better:
private void timer1_Tick(object sender, EventArgs e)
{
if (Application.OpenForms.Count == 0)
Application.Exit();
var form = Application.OpenForms[0] as frmOperacao;
form.Close();
}
Q:
Expression for months in which.NET Framework shipped with a version
I'm looking for a way to define months in which.NET Framework shipped with a version. Something like this:
1.0
2.0
3.5
4.5
So far, I've defined periods that did the trick for me (1.0, 2.0 and 3.5), but this seems to be quite limited, since I had to manually check the version number in this definition.
Any ideas?
A:
You can do that by checking the assembly version numbers. For example:
[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.0 be359ba680
Related links:
Comments