|
The MouseHover event won't be fired when your mouse leaves the client area, but that doesn't mean the code inside your MouseHover event won't continue to run. It will run until it completes. If you want to stop it, you should consider using a state variable (like a bool field called stop or something) that your MouseLeave event handler sets (i.e., set to true ) and your MouseHover event checks, conditionally exits its loop, and resets (i.e., set to false ).
The real problem I see with your code, however, is what you're doing within MouseHover . If you read the documentation (always a good thing), it states that the MouseHover event is fired continuously (several times a second) - even when you're mouse is not moving. You should not be performing exhaustive routines like you are now. Not only are you hogging up CPU cycles but you're most likely exhausting memory (especially if you're not disposing objects that require it, like the original Image that was assigned to the PictureBox.Image property).
You should consider using a timer that you enable once (don't enable it again if it's already enabled and use state variables like I mentioned in the first paragraph) and lets it run - don't execute your code continuously if you don't need to, and always dispose objects that implement IDisposable (like the Image and Bitmap classes, among many others) if you need to free memory immediately (for classes that follow the disposable pattern, they will eventually be released but only when the GC decides to clean-up that generation).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
thanks for your help...
But I´m using a variable bool to stop the cycle that is running in MouveHover...
If you take a look, the only thing that mouseleave do is to put this variable bool to false in order to make that the if condition inside the cycles don´t execute.
The problem is that the mousehover continues to execute when I make the mouse leave the pictureBox.
I am usign the Visual Studio in DEbug mode and I put a breakpoint in the first instruction of mouseleave and this instruction is only executed when the cycle for in mouvehover ends =/
I think it´s a problem of two much memory occupied.
Your advice is to make a Dispose of the Image after the Refresh statement, isn´t it?!
I made that, you are right my program now occupies less memory, but the problem with the mousehover/leave persits =/
Thanks a lot
|
|
|
|
|
Sorry, I missed that the first time. It really helps if you post code correctly on this site. Use <PRE> tags (or the "pre" toolbar button below) for code blocks (that way you can tabify your code and use a fixed-width font) and <CODE> tags for inline code (like variables, methods, and even the tag names I mentioned, which are HTML-encoded).
As I mentioned before, the code inside MouseHover will continue to execute. The event will not fire anymore, but that doesn't stop the implementation from continuing. Unless you stop the code somehow it will continue.
The problem you're facing is because your handlers for both the MouseLeave and MouseHover events are running in the same thread. Your MouseLeave event handler won't execute until your MouseHover event handler completes.
There are many ways to solving this, but you really should read about threading applications if you're not familiar with it already. You can start by reading Threading[^] in the .NET Framework SDK on MSDN Online.
You execute your MouseHover event handler implement in a separate thread (move it into a new method). What you absolutely must do is use Control.Invoke to actually set the PictureBox.Image - you must make modifications - and most often, even read properties - from within the thread on which the control to be modified was created. If you don't, undefined problems may - and most often do - occur.
This way, since that code is executing in a separate thread your MouseLeave event handler is called at the proper time and your variable is set. When your MouseHover implementation loop reads that variable (that you don't need to invoke to get; only properties and methods that interop with the Win32 APIs for the control class, since every Windows Forms control encapsulates Win32 APIs and most encapsulate the Windows Common Controls) can stop and everything happens in the right order.
Another way is to start the MouseHover implementation in a separate thread, which you keep a reference to. In your MouseLeave event handler, call Abort on that thread reference, but make sure you handle any ThreadAbortException s that will be thrown.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
You were right...as always
I used two threads and the mouse hover/leave works very well \o/
Thank you very much.
PS. Just two little questions...
Do you know where I can read some information about exceptions?! (mutexes, semaphores, etc)
Do you know where I can read some information about how to build classes?! I have many difficulties in that... =/
|
|
|
|
|
ee99035 wrote:
Do you know where I can read some information about exceptions?! (mutexes, semaphores, etc)
As a developer, there's a resource you should get very familiar with: the .NET Framework SDK[^]. There's no excuse for not reading it. It contains topics about all different sorts of programming scenarios with .NET and documents the base class library[^] (BCL). Specifically, if you want to read about the thread classes, read about the members of the System.Threading[^] namespace. If you installed the defaults for Visual Studio .NET, you have this installed locally too (can also be downloaded as part of the .NET Framework SDK without the need for Visual Studio 7.0 or newer from http://msdn.microsoft.com/netframework[^]).
ee99035 wrote:
Do you know where I can read some information about how to build classes?! I have many difficulties in that
For information on object-oriented programming you should either take a class or pick up some good books. You can also learn by example but you should undersatnd the basics of objects first. Just because you encapsulate a bunch of functions in a class doesn't make it an object-oriented design. The ubiquitous case is the Company>-Employee classes that a lot of books use.
You can also find articles online.
If you're looking for examples of object-oriented programs, well, that's what this web site is full of.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Does anyone know of a class library/DLL of the Platform SDK RAS API being wrapped in C#??
Thanks,
-Mark
|
|
|
|
|
You can either try Google[^] or MSN Search[^], P/Invoke the functions yourself (they're quite easy), or visit http://pinvoke.net[^] for signatures to many, many functions documented in the Platform SDK (and even a few that aren't, like the cards.dll exports).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Actually there is a CP article (in purgatory) on this. www.codeproject.com/Purgatory/rasdemo.asp[^]
I believe one of my friends used this before. Personally haven't tested the code, but it might need some cleaning.
This posting is provided "AS IS" with no warranties, and confers no rights.
Alex Korchemniy
|
|
|
|
|
Hello,
I have some code which writes out an XML file. In the root element of the file I am trying to write the location of the schema file. However it isn't being written out as I expect.
An extract of my code is as follows
XmlElement rttElement = doc.CreateElement("roadTimeTable");
rttElement.SetAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
rttElement.SetAttribute("xsi:noNamespaceSchemaLocation",schemaFileName); but the result is
<?xml version="1.0" encoding="UTF-8"?>
<roadTimeTable xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" noNamespaceSchemaLocation="traffic_schema.xsd">
{omitted for brevity} As you can see the xsi: prefix on the attribute xsi:noNamespaceSchemaLocation is missing.
How do I get this to output properly? Or am I missing something and there is a better way to write out the location of the schema file?
Do you want to know more?
Vogon Building and Loan advise that your planet is at risk if you do not keep up repayments on any mortgage secured upon it. Please remember that the force of gravity can go up as well as down.
|
|
|
|
|
Use the overloaded SetAttribute that takes three string parameters: localName , namespaceURI , and value :
rttElement.SetAttribute("noNamespaceSchemaLocation",
"http://www.w3.org/2001/XMLSchema-instance", "traffic_schema.xsd"); You also should not add namespace URIs and prefixes how you're doing it. This is what the XmlNamespaceManager is for. The XML DOM classes keep track of elements and namespaces and write them out accordingly.
The documentation for the XmlNamespaceManager in the .NET Framework SDK (installed with VS.NET and the .NET Framework SDK by default, and available online at http://msdn.microsoft.com/library[^]) includes more information and examples.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
i got a header file written in c++ and the C# dont know how to use it
is there a way to convert the file and use it in .NET?
|
|
|
|
|
No, you must P/Invoke the functions and declare any constants and structs in managed code. It helps to know how unmanaged types map to managed types, like that an unmanaged long is a managed int (Int32 ) because they're both 32-bit integers.
Start be reading Consuming Unmanaged DLL Functions[^] and Marshaling Data with Platform Invoke[^].
Understand that C# does not use headers. In fact, pure Managed C++ doesn't need headers, either. Only when writing unmanaged applications in C++ or mixed-mode C++ (managed and unmanaged code) do you actually need headers (with regard to C++).
If you're looking for examples or signatures for common Win32 APIs (among a few others), take a look at http://pinvoke.net[^].
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hi there!
I have made some code in MFC previously and now I´m developing an aplication in C# and I need to use some functions that I define in MFC.
Can I integrate MFC code in C#, is this possible?! How can I do that?
Thanks very much, Sérgio
|
|
|
|
|
You have 2 options:
-recompile the MFC code with the /clr switch on the Visual C++ compiler, and make the MFC functions you want inside a Managed C++ class.
-Export your MFC functions as a native dll and use Platform Invocation Services (P/Invoke) to call them.
Any remotely useful information on my blog will be removed immediately.
Judah Himango
|
|
|
|
|
In addition to what Judah said, you may want to check out one of several replies I've posted to this forum about calling methods (functions declared in a class) using P/Invoke. It's not straight forward like calling global functions because you have a 'this pointer' that you need to take into account, i.e. methods (at least virtual methods) must be called in the context of a block of memory for your class ('this') and all instance member fields cannot be evaluated with knowing the base address of your class.
Take a look at http://www.codeproject.com/script/comments/forums.asp?msg=771919&forumid=1649#xx771919xx[^] for more information. If you have already declared static class factories you do not need to do so again.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hi,
is there a way to send an application into sleep for some Milliseconds?
Thanks
Ariadne
|
|
|
|
|
System.Threading.Thread.Sleep(5000);
Any remotely useful information on my blog will be removed immediately.
Judah Himango
|
|
|
|
|
System.Threading.Thread.Sleep( m );
where m is milliseconds
Gary
|
|
|
|
|
Hi,
I'm trying to write a [fairly basic] program that lets you send a message from one computer to another on the same network (either based on the name of the logged in user, or the name of the workstation itself -- undecided as of yet) and I'm struggling to figure out how to go about it.
---
The Scenario, or 'What I Want to Happen':
Joe Nobody loads up the application on his computer. He's presented with a drop-down menu that contains a list of users currently logged into the network, a text box for typing his message and a button that says 'Send!' He selects Katie Nobody from the list, types 'Hello Katie!' and sends it.
Katie, who also has the application running on her computer, sees Joe's message pop up. She reads it and decides to reply. She hits the 'reply' button on her incoming message window, types her reply in the messagebox provided for replying and sends her message back to him.
The two of them now have a window that connects them together without needed to re-state which user they're talking too and can continue their conversation for as long as they'd like to.
---
The purpose of writing the program is two-fold; I need a program that does this (and only requires minimal features) and I'm trying to learn more C#. I've been scouting around MSDN but, perhaps because I'm not sure where to start looking, I'm struggling to figure out how to begin. I thought about using MessageQueue -- is that a viable option?
If anyone can give me a couple of starting points for sending messages between networked machines or point me to references/articles, I would be very grateful!
Thanks in advance,
Keija.
|
|
|
|
|
Code generation for property 'MessageReadPropertyFilter' failed. Error was: 'Requested operation is not supported on this platform.'
(I'm running Windows 2000 on this machine.)
|
|
|
|
|
If both of your clients are .NET apps, and you're running within a local area network, .NET Remoting might be the solution for you.
There's some good articles on codeproject and on MSDN that show how to use .NET remoting. I'm certain I've seen an MSDN "instant messenger" like example that uses .NET remoting. A search on both sites would be useful.
Any remotely useful information on my blog will be removed immediately.
Judah Himango
|
|
|
|
|
Judah Himango wrote:
and you're running within a local area network
That's rather limiting for something that's pretty much limitless. Already the BCL provides both the TcpChannel and HttpChannel (and an internal channel for within a simgle process with multiple app domains) and the 2.0 BCL will define the IpcChannel . You can define any channel you want. Don't want to use TCP? Define a channel that uses IPX. Don't want to rely on an electronic transport layer? Define a channel that drives a small catapult that launches chicken eggs in defined patterns. It really doesn't matter; both the serialization and transport chains are independent and extensible.
So long as both applications are managed, .NET Remoting is your best option because it doesn't tie you down to so many restrictions (there's some, but that has more to do with serialization than anything).
...but please don't waste chicken eggs.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hehehe you know, when I was posting that I thought to myself, "Heath is gonna come by and chide me for saying 'within a LAN'" but hey IMO if you're doing remoting on a heterogeneous network like the internet, I would rather be working with Web Services. Just my opinion.
That said, we're using .NET remoting here at work and I absolutely love it. My only beef would be the problem with getting remote error messages on the server; don't know if it's a bug in 2.0 and 1.1, but often I cannot get remote error messages (occurring on the server) to be thrown on the client. With remote errors disabled, I get "For more info, enable remote errors on the server" returned to the client, and with remote errors enabled, the server locks up when throwing the exception to the client. Aside from that, and the few restrictions I've encountered (like you said, due to serialization), .NET Remoting seems pretty nifty. Wonder how it stacks up against Indigo...
Any remotely useful information on my blog will be removed immediately.
Judah Himango
|
|
|
|
|
You don't need Message Queue. All you need is Remoting.
You can implement this in two ways. Both would require that you have a central server application that your chat client checks in with to see who's logged in. This means that the Chat application is running on that persons computer(!), not that the machine is logged in!
You can have the central server dole out who's Chat application is logged in and what it's IP address is. This way, you can have direct communication between two clients without the need to have the server relay message for you. This would make GROUP chats difficult to implement though. It also greatly complicates the code on the client side, but simplifies the code on the server side.
The other method is to have the central server do all the message relaying for you. This would have the opposite effect on code complexity. The client side is simplified, but the server side gets complicated. This would give you the beneift of easier implementation of chat rooms though.
You could check out Picture Chat using C#[^] here on CP for a good example of how to implement chat.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
|