473,799 Members | 3,350 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

struct/IDisposable/MessageBox.Show ()

Hi,

this is what I did:

public struct SWaitCursor:IDi sposable {
public SWaitCursor (int i) {
Cursor.Current = Cursors.WaitCur sor;
}
void System.IDisposa ble.Dispose() {
Cursor.Current = Cursors.Default ;
MessageBox.Show ("SWaitCursor.D ispose");
}
}

private void btnCursorTest_C lick(object sender, System.EventArg s e) {
SWaitCursor WC = new SWaitCursor(1);
System.Threadin g.Thread.Sleep( 1000);
}

what I want to know:

- Is it definite that in btnCursorTest_C lick the object WC will be disposed of (calling the Dispose method) always AND exactly at the same time the method returns?
- Wondering why MessageBox.Show () in Dispose doesn't execute, breakpoints don't work either??

It seems to work though.
Nov 17 '05 #1
5 3189
haha, it works without working...
Dispose is never called, the cursor gets reset to previous state without Dipose being responsible... sorry
"Robert Heuvel" <ro***********@ isopass.com> schrieb im Newsbeitrag news:e1******** ******@tk2msftn gp13.phx.gbl...
Hi,

this is what I did:

public struct SWaitCursor:IDi sposable {
public SWaitCursor (int i) {
Cursor.Current = Cursors.WaitCur sor;
}
void System.IDisposa ble.Dispose() {
Cursor.Current = Cursors.Default ;
MessageBox.Show ("SWaitCursor.D ispose");
}
}

private void btnCursorTest_C lick(object sender, System.EventArg s e) {
SWaitCursor WC = new SWaitCursor(1);
System.Threadin g.Thread.Sleep( 1000);
}

what I want to know:

- Is it definite that in btnCursorTest_C lick the object WC will be disposed of (calling the Dispose method) always AND exactly at the same time the method returns?
- Wondering why MessageBox.Show () in Dispose doesn't execute, breakpoints don't work either??

It seems to work though.
Nov 17 '05 #2
Robert,

You are implementing the pattern incorrectly. First, your structure should look like this:


public struct SWaitCursor:IDi sposable {
public SWaitCursor (int i) {
Cursor.Current = Cursors.WaitCur sor;
}
void Dispose() {
Cursor.Current = Cursors.Default ;
MessageBox.Show ("SWaitCursor.D ispose");
}
}

The reason for this is that if you don't declare it as a public method (instead of an explicit interface implementation) , it will incur a boxing operation (in .NET 2.0 it will, not in 1.1 and before, I believe), which you don't want.

Furthermore, your event handler should look like this:

private void btnCursorTest_C lick(object sender, System.EventArg s e) {
using (SWaitCursor WC = new SWaitCursor(1))
{
System.Threadin g.Thread.Sleep( 1000);
}
}

If you implement IDisposable, then you need to use it in a using statement to get the deterministic behavior. The way you have it, the structure is just popped off the stack, which is why your call to MessageBox never occurs.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Robert Heuvel" <ro***********@ isopass.com> wrote in message news:e1******** ******@tk2msftn gp13.phx.gbl...
Hi,

this is what I did:

public struct SWaitCursor:IDi sposable {
public SWaitCursor (int i) {
Cursor.Current = Cursors.WaitCur sor;
}
void System.IDisposa ble.Dispose() {
Cursor.Current = Cursors.Default ;
MessageBox.Show ("SWaitCursor.D ispose");
}
}

private void btnCursorTest_C lick(object sender, System.EventArg s e) {
SWaitCursor WC = new SWaitCursor(1);
System.Threadin g.Thread.Sleep( 1000);
}

what I want to know:

- Is it definite that in btnCursorTest_C lick the object WC will be disposed of (calling the Dispose method) always AND exactly at the same time the method returns?
- Wondering why MessageBox.Show () in Dispose doesn't execute, breakpoints don't work either??

It seems to work though.
Nov 17 '05 #3
HI,

one question:

void Dispose() instead of void System.IDisposa ble.Dispose() (as VS2003 generated itself) would prevent boxing?
Could you explain that one please?

As to using the using clause, sure thing, but then it would make more sense to use a class because the constructor wouldn't need a useless parameter.

And what about the cursor behaviour? Is it normal for it to revert to Cursor.Default after the completion on an event handling call?
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> schrieb im Newsbeitrag news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
Robert,

You are implementing the pattern incorrectly. First, your structure should look like this:


public struct SWaitCursor:IDi sposable {
public SWaitCursor (int i) {
Cursor.Current = Cursors.WaitCur sor;
}
void Dispose() {
Cursor.Current = Cursors.Default ;
MessageBox.Show ("SWaitCursor.D ispose");
}
}

The reason for this is that if you don't declare it as a public method (instead of an explicit interface implementation) , it will incur a boxing operation (in .NET 2.0 it will, not in 1.1 and before, I believe), which you don't want.

Furthermore, your event handler should look like this:

private void btnCursorTest_C lick(object sender, System.EventArg s e) {
using (SWaitCursor WC = new SWaitCursor(1))
{
System.Threadin g.Thread.Sleep( 1000);
}
}

If you implement IDisposable, then you need to use it in a using statement to get the deterministic behavior. The way you have it, the structure is just popped off the stack, which is why your call to MessageBox never occurs.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Robert Heuvel" <ro***********@ isopass.com> wrote in message news:e1******** ******@tk2msftn gp13.phx.gbl...
Hi,

this is what I did:

public struct SWaitCursor:IDi sposable {
public SWaitCursor (int i) {
Cursor.Current = Cursors.WaitCur sor;
}
void System.IDisposa ble.Dispose() {
Cursor.Current = Cursors.Default ;
MessageBox.Show ("SWaitCursor.D ispose");
}
}

private void btnCursorTest_C lick(object sender, System.EventArg s e) {
SWaitCursor WC = new SWaitCursor(1);
System.Threadin g.Thread.Sleep( 1000);
}

what I want to know:

- Is it definite that in btnCursorTest_C lick the object WC will be disposed of (calling the Dispose method) always AND exactly at the same time the method returns?
- Wondering why MessageBox.Show () in Dispose doesn't execute, breakpoints don't work either??

It seems to work though.
Nov 17 '05 #4
Robert,

In .NET 2.0, if you implement a method on an interface implicitly, the compiler will generate an IL instruction which will call the method on the structure, without a cast to IDisposable. Otherwise, a cast to IDisposable will take place, causing a boxing operation.

If you look at the attached piece of code in Reflector, you will see what I mean.

To get around the parameter issue, you could do this:

public struct SWaitCursor:IDi sposable {
public static SWaitCursor Create() {
// Set the cursor.
Cursor.Current = Cursors.WaitCur sor;

return new SWaitCursor();
}

void Dispose() {
Cursor.Current = Cursors.Default ;
MessageBox.Show ("SWaitCursor.D ispose");
}
}

And then use it in the using statement.

I'm not sure about the Cursor being set after an event handler. It is possible, but I can't say for sure.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Robert Heuvel" <ro***********@ isopass.com> wrote in message news:uR******** ******@TK2MSFTN GP15.phx.gbl...
HI,

one question:

void Dispose() instead of void System.IDisposa ble.Dispose() (as VS2003 generated itself) would prevent boxing?
Could you explain that one please?

As to using the using clause, sure thing, but then it would make more sense to use a class because the constructor wouldn't need a useless parameter.

And what about the cursor behaviour? Is it normal for it to revert to Cursor.Default after the completion on an event handling call?
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> schrieb im Newsbeitrag news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
Robert,

You are implementing the pattern incorrectly. First, your structure should look like this:


public struct SWaitCursor:IDi sposable {
public SWaitCursor (int i) {
Cursor.Current = Cursors.WaitCur sor;
}
void Dispose() {
Cursor.Current = Cursors.Default ;
MessageBox.Show ("SWaitCursor.D ispose");
}
}

The reason for this is that if you don't declare it as a public method (instead of an explicit interface implementation) , it will incur a boxing operation (in .NET 2.0 it will, not in 1.1 and before, I believe), which you don't want.

Furthermore, your event handler should look like this:

private void btnCursorTest_C lick(object sender, System.EventArg s e) {
using (SWaitCursor WC = new SWaitCursor(1))
{
System.Threadin g.Thread.Sleep( 1000);
}
}

If you implement IDisposable, then you need to use it in a using statement to get the deterministic behavior. The way you have it, the structure is just popped off the stack, which is why your call to MessageBox never occurs.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Robert Heuvel" <ro***********@ isopass.com> wrote in message news:e1******** ******@tk2msftn gp13.phx.gbl...
Hi,

this is what I did:

public struct SWaitCursor:IDi sposable {
public SWaitCursor (int i) {
Cursor.Current = Cursors.WaitCur sor;
}
void System.IDisposa ble.Dispose() {
Cursor.Current = Cursors.Default ;
MessageBox.Show ("SWaitCursor.D ispose");
}
}

private void btnCursorTest_C lick(object sender, System.EventArg s e) {
SWaitCursor WC = new SWaitCursor(1);
System.Threadin g.Thread.Sleep( 1000);
}

what I want to know:

- Is it definite that in btnCursorTest_C lick the object WC will be disposed of (calling the Dispose method) always AND exactly at the same time the method returns?
- Wondering why MessageBox.Show () in Dispose doesn't execute, breakpoints don't work either??

It seems to work though.
Nov 17 '05 #5
Oops, here is the code.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Robert Heuvel" <ro***********@ isopass.com> wrote in message news:uR******** ******@TK2MSFTN GP15.phx.gbl...
HI,

one question:

void Dispose() instead of void System.IDisposa ble.Dispose() (as VS2003 generated itself) would prevent boxing?
Could you explain that one please?

As to using the using clause, sure thing, but then it would make more sense to use a class because the constructor wouldn't need a useless parameter.

And what about the cursor behaviour? Is it normal for it to revert to Cursor.Default after the completion on an event handling call?
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> schrieb im Newsbeitrag news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
Robert,

You are implementing the pattern incorrectly. First, your structure should look like this:


public struct SWaitCursor:IDi sposable {
public SWaitCursor (int i) {
Cursor.Current = Cursors.WaitCur sor;
}
void Dispose() {
Cursor.Current = Cursors.Default ;
MessageBox.Show ("SWaitCursor.D ispose");
}
}

The reason for this is that if you don't declare it as a public method (instead of an explicit interface implementation) , it will incur a boxing operation (in .NET 2.0 it will, not in 1.1 and before, I believe), which you don't want.

Furthermore, your event handler should look like this:

private void btnCursorTest_C lick(object sender, System.EventArg s e) {
using (SWaitCursor WC = new SWaitCursor(1))
{
System.Threadin g.Thread.Sleep( 1000);
}
}

If you implement IDisposable, then you need to use it in a using statement to get the deterministic behavior. The way you have it, the structure is just popped off the stack, which is why your call to MessageBox never occurs.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Robert Heuvel" <ro***********@ isopass.com> wrote in message news:e1******** ******@tk2msftn gp13.phx.gbl...
Hi,

this is what I did:

public struct SWaitCursor:IDi sposable {
public SWaitCursor (int i) {
Cursor.Current = Cursors.WaitCur sor;
}
void System.IDisposa ble.Dispose() {
Cursor.Current = Cursors.Default ;
MessageBox.Show ("SWaitCursor.D ispose");
}
}

private void btnCursorTest_C lick(object sender, System.EventArg s e) {
SWaitCursor WC = new SWaitCursor(1);
System.Threadin g.Thread.Sleep( 1000);
}

what I want to know:

- Is it definite that in btnCursorTest_C lick the object WC will be disposed of (calling the Dispose method) always AND exactly at the same time the method returns?
- Wondering why MessageBox.Show () in Dispose doesn't execute, breakpoints don't work either??

It seems to work though.
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2450
by: Saso Zagoranski | last post by:
Hi! I'm trying to make my own MessageBox... What I would like to know is, how the MessageBox class is implemented? I could have something like: new MyMessageBox().ShowDialog(); but I would like the solution used by MessageBox, where you call the static Show() method, which returns a DialogResult value...
3
2450
by: Angel | last post by:
Hello again (and again, and again...) I think I'm getting closer to solving my initial problem of calling unmanaged code. I managed to call the functions with user-defined structs w/o getting any run-time exceptions. The problem is that I'm receiving a Failure return (it returns 1 = "Failure"). I have a feeling it may be that the function's not reading the struct correctly. This time I decided to display all the code (it's very short)....
2
1125
by: Rene | last post by:
Please help me, add a button to a new for and copy and paste the simple code below. According to me, the last message box in the Click event of the button should popup a "123" but instead it pops up a "0". Why?? Is this a bug?? Thank you. ---------------- CODE TO COPY ---------------- public struct struct1 { public int a;
6
10820
by: SB | last post by:
I feel dumb to ask because I bet this is a simple question... Looking at the code below, can someone please explain why I get two different values in my Marshal.SizeOf calls (see the commented lines)? TIA! sb
0
2330
by: Alvaro Enriquez de Luna | last post by:
Hello everybody. I am trying to use in C# a dll developed in C. I am founding problems with C structs. While my C struct does not include anything related with char or char *, everyting works ok, but when I introduce a char or char * variable in the struct, I obtain a message like this: "Method's type signature is not PInvoke compatible." Here is my code in both languages:
9
1322
by: Lloyd Dupont | last post by:
I have an object which is just a thin wrapper over an other object and might be created in big quantities. something like that: // ===== pseudo-code cample ======= class TheObject { int data; TheWrapper Data { get { return new TheWrapper(data); } }
11
2323
by: Mark Rae | last post by:
Hi, Following on from the recent thread about why HttpWebRequest doesn't implement the IDisposable interface, I got to wondering whether people make their custom classes inherit IDisposable or not as a general rule, or only under certain circumstances... Since it's an easy enough thing to do (http://msdn2.microsoft.com/en-us/library/system.idisposable.aspx), is there any good reason not to make every custom class IDisposable, the same...
16
2706
by: alexia.bee | last post by:
Hi all, In some weird reason, excel instance won;t die if i remove the comment from 4 lines of setting values into struct. here is a snipcode public System.Collections.Generic.List<frmMain.sDBTest> LoadTestSet(string TestSetFile, System.Collections.Generic.List<frmMain.sDBTestDBviewList)
0
1046
by: Zidan26 | last post by:
Hi Everybody! I need help. I want Import to C# struct written in C++. C++ code: typedef struct {
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9544
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10490
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10259
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10238
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5589
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4145
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.