|
((System.Windows.Forms.Button) sender).Tag ?
|
|
|
|
|
That's it
I was very close to working it out and was on the right track - it was changing the object sender to a button object that actually had me stumped.
Thanks heaps.
Glen Harvy
|
|
|
|
|
Glen Harvy wrote: changing the object sender
The thing to remember is that you're not changing it; it is a Button, but you need to tell the compiler that it is.
In some cases you may also need to test what type the sender is
if ( sender is System.Windows.Forms.Button ) { ... }
|
|
|
|
|
I would always use the 'as' keyword.
Button btn = sender as Button;
if (btn != null)
{
// process here
}
The other answer you got is correct, it *will* always be a button, and he mentioned 'as', which is also correct. But, for me, casting is a bad habit best avoided, even when I know I can get away with it.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Hi,
I have developed a web service and then I need to create a private UDDI registry for publishing that web service and allowing others to access UDDI for service consumption. Can some one help me that how can one create private UDDI registry (from start to end). I reed UDDI specification and lot of other stuff but I am not sure from where to start.
|
|
|
|
|
I have written a class that stores user options throughout the program. (Color preferences, startup windows, etc.) When the program initializes, a FileStream object is used to open then file then a binary formatter is used to deserialize the stream. The FileStream is then closed.
The problem is that when the program closes and the FileStream object trys to open the file to save the updated user options. A file exception is sometimes threw: "Cannot access file because it is being used by another proccess". Whats so annoying about the error is that it only happens SOMETIMES! A debugging nightmare! I assume that the object handle is not being released somewhere in memory. Can anybody give me any pointers on overcoming this problem?
may your code be error free
|
|
|
|
|
Have you tried using ApplicationSettings? Is there a reason why you need something different than what is already available?
only two letters away from being an asset
|
|
|
|
|
programmingAddict wrote: "Cannot access file because it is being used by another proccess"
Make sure you only have one instance of your application running at any one time. This is a shot in the dark, admittedly, but when I've run into similar problems, it's been when I'm running an application in debug mode, it appears to crash for whatever reason, and I start another instance of the application. Unbeknownst to me, the previous instance is still lurking somewhere, still running and has not released its resources.
Again, this may not have anything to do with your problem, but it's worth looking into. Use the Windows Task Manager to see what processes are running when you encounter this problem.
|
|
|
|
|
Telling from the symptomes, you are not closing the file stream properly. Sometimes the garbage collector is finalizing the object, which closes the file stream.
What does your code look like for reading the file?
---
single minded; short sighted; long gone;
|
|
|
|
|
I agree with Guffa.
Showing all the code involved with opening, reading, closing the options file
might give us a chance to give you some hint.
|
|
|
|
|
I would be happy to show you the code
This class is being used in a VB.NET project. But I plan to use the framework in C# apps.
'Here is the code for Initializing (reading) the options class:
If IO.File.Exists(_FileName) Then
Dim fs As FileStream
Try
fs = File.Open(_FileName, FileMode.Open)
Dim bf As BinaryFormatter = New BinaryFormatter()
userOp = CType(bf.Deserialize(fs, Nothing), ArrayList)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Try
fs.Close()
fs.Dispose()
Catch
End Try
End If
GC.Collect()
'************************
'Here is the code for saving. (Where the error sometimes occurs)
Public Sub SaveToFile()
Dim fs As FileStream = Nothing
Try
'where the error occurs
fs = File.Open(_FileName, FileMode.Create)
Dim bf As BinaryFormatter = New BinaryFormatter()
bf.Serialize(fs, userOp)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Try
fs.Close()
Catch
End Try
End Sub
Thanks for all the help!
may your code be error free
|
|
|
|
|
Hi,
I looked at your code (did not run it, I'm a C# guy) and have two suspicions
and several comments; here they are:
1.
it seldom is a good idea to have an empty catch block, since that really
hides any problems that might occur; you should fix that.
2.
rather than printing or showing Exception.Message I strongly advice everyone
to always use Exception.Totring since that gives a traceback plus additional
information depending on the kind of Exception, something Message cannot do
(e.g. the full file name if a file fails somehow, but also some inner exception
in case that occured)
3.
the reason your fs.Close() is in a try-catch with empty catch is, I guess,
that the file might not be open (because the open failed), hence now the
close will fail, and you dont want to see that. Fair enough, but then
you should test for it, as in:
if fs!=Nothing fs.Close() // I hope this is correct syntax for "fs is not nothing"
4.
I did not like GC.Collect() at the end of the reader. It seldom is wise to
attempt and help the GC, it normally is smart all by itself.
5.
suspicion 1:
I did not like ths fs.Dispose() that is present in the reader (but not in
the writer !?); I think it should not be there since fs.Close() takes care
of all resources, and MSDN says File.Dispose() is new since 2.0 but should
not be called ! Not sure how it would influence things tho.
6.
suspicion 2:
maybe, just maybe, your writing method is (sometimes) active while your reading
mehtod is active too, on a different thread. Make sure it is not. If both
do get called from different threads, try to devise a scheme in which the
reader gets aborted so the writer can succeed (a simple lock might not be
good enough since I guess you want to write upon app closure, and an ongoing
reader should not delay the closure)
You could inspect your code for threading stuff, and/or you could set a flag
while the reader is busy (and clear it when it's done), and test for that flag
inside the writer; doing so might confirm the occasional failure coincides with
concurrent reading/writing.
Hope this helps.
|
|
|
|
|
Hi
i am using a udp client for recieving..i used beginrecieve method of udp client
byte[] buff = new byte[1024];
soUdp.BeginReceive(new AsyncCallback(ReceiveCallback), buff);
but when i execute i am getting an error
An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full"} System.Exception {System.Net.Sockets.SocketException}
Why is that coming
Thanks in advance
Regards
DilipRam
|
|
|
|
|
Hi!
Seems as if you got more than 1K of data reaching the port you're listening to.
Try increasing buff to 4K and see if it's getting better.
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
mav is right, your buffer is too small.
there is a maximum packet size for TCP/IP (IIRD it is around 1500 B)
and there is one for UDP, but I dont recall, might be the same number.
Your buffer should be large enough to contain such packet.
|
|
|
|
|
Hi Luc!
Just to clear things up: I think you refer to the MTU, not the maximum packet size for TCP/IP.
The MTU (maximum transfer unit) is the largest chunk of data that can be transmitted unfragmented. For Ethernet, this is 1500 bytes (this leaves 1472 bytes of data for UDP because of 20 bytes of IP header and 8 bytes of UDP header).
Once the amount of data to send is larger than this, it's split into single, smaller packets and transmitted separately. IP then reassembles the packets automatically so that you can have UDP packets with > 1472 bytes of data.
I found a quite extensive overview of TCP/IP here[^], just in case you're interested.
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
Hi mav, yes that's what was simmering in my memory.
So there would be no maximum size at all ?
|
|
|
|
|
There is a maximum size, but it depends on the system you're working with; the way the TCP/IP stack is implemented.
I think the absolute maximum will be 64K, but I've read that Sun machines only can handle 32K.
But that's a rather theoretical limit because if you use such large UDP packets they are split into many single packages and if only a single packet gets altered or lost, the whole UDP datagram is discarded because it cannot be reassembled correctly.
So you should keep the data in a single UDP packet as small as possible.
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
Thanks for the info.
Regards.
|
|
|
|
|
hello,
hello is there anyway i can use ffmpeg to convert an uploaded video through asp.net and csharp n play the converted file..i found 1 way of streaming flv through asp.net what i want is convertion process
aneef
|
|
|
|
|
Is it possible to import a method from a DLL and run it as if it were in the same namespace?
The problem I am running into is this (Application = CA2, DLL = AnyDLL) :
1. CA2 references AnyDLL and invokes method in AnyDLL.
2. Method in AnyDLL tries to use GetType to get a class in CA2.
3. Exception happens because the DLL can't see the class in CA2.
Is there any way around this? I would think many different times a person would want to use a method in a DLL as if the code for the method was right there and not in the DLL?
Any help is much appreaciated!
|
|
|
|
|
Don't repost the same question so close after the first.
---
single minded; short sighted; long gone;
|
|
|
|
|
Taicho2k wrote: 1. CA2 references AnyDLL and invokes method in AnyDLL.
2. Method in AnyDLL tries to use GetType to get a class in CA2.
3. Exception happens because the DLL can't see the class in CA2.
Is there any way around this? I would think many different times a person would want to use a method in a DLL as if the code for the method was right there and not in the DLL?
Yeah, does sound like a circular reference. I try to avoid circular references like the plague. My advice would be to factor out the class in your executable to a second assembly. Then the first assembly can reference the second one to access the class. The executable can reference both assemblies to use the classes/methods it needs. This breaks the circular reference and makes things easier to manage and understand.
|
|
|
|
|
CA2 references AnyDLL and AnyDLL references CA2? What you have is a circle reference and not a very good design. You either need to redesign or do a better job of explaining what you are trying to do, or want to do.
only two letters away from being an asset
|
|
|
|
|
Thanks for the replies everyone, I'm probably not explaining myself too well...the method I am trying to use from my dll is a method that uses System.Reflection to be able to run any other method by using a string so when I say that the method in AnyDLL is referencing a method in my application pretend that the method could be referencing any method in any other application...essentially I am just trying to get the method in the DLL to run as if it were running directly in the application and not contained in some DLL...is that even possible? I was experimenting w/ DllImport and now I've begun playing around w/ Assembly.LoadFrom("dllname") thanks to an earlier suggestion... any more ideas?
|
|
|
|