|
Thank you for this. I just edited the question a Little bit
modified 19-Jan-21 21:04pm.
|
|
|
|
|
I'm glad that's helpful; perhaps it's a bias of mine that I think copying a pointer is "cheap" ?
«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008
|
|
|
|
|
BillWoodruff wrote: we need Richard Deeming or Richard MacCutchan to step-up That's very kind but misguided I'm afraid. I could possibly give an answer, but I would need to do quite a bit of research and testing first.
|
|
|
|
|
I know it is your modesty that makes you suggest I may be kindly misguided to consider you (and brother Deeming) as guides non-pareil in this esoteric area, and I appreciate that quality as much as I do the depth of your technical knowledge Richard MacCutchan wrote: I could possibly give an answer
Duchess to Alice (Alice’s Adventures in Wonderland, Chapter 9) That’s nothing to what I could say if I chose.
«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008
|
|
|
|
|
BillWoodruff wrote: know it is your modesty But thank you anyway.
And, to be honest I could not have produced as good an explanation as Pete's below.
|
|
|
|
|
BillWoodruff wrote: we need Richard Deeming or Richard MacCutchan to step-up
Or Pete, who nailed the explanation below. But your link also answers the question, albeit in much greater detail.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Imagine your system has an event with one subscriber. This subscriber is on a background thread. Now, consider the case where the very last action before context switching over to the thread your subscriber is on is to check to see if you have any subscriptions. That's fine as the null check will tell you that you have a subscriber.
Okay, you're now processing on this other thread and the first thing it does is to remove the event subscription. You now have nothing hooked up to the event but when you context switch back, it's going to continue as though there are subscriptions. That's why you take the copy.
This space for rent
|
|
|
|
|
Thank you for your Feedback.
Yes I see this Point meanwhile. On the other Hand when the subscriber unsubscribes he still has to be aware to get notified and this fact (I hope I'm right on this, that it is a fact) I never found in a discussion.
Time Publisher Subscriber
---- --------- ---------
1 Take a copy -
2 - Unsubscribe
3 Invoke "Check for race condition needed before proceeding"
modified 19-Jan-21 21:04pm.
|
|
|
|
|
It's not the unsubscribed side that is the problem. If you think about it from the point of view of why you test the event in the first place, it gives you the hint that you could end with functionally identical code between not testing the subscription and throwing an exception because nothing is listening to the event and testing it and throwing an exception because you have ended up in a situation where nothing is listening to the event.
This space for rent
|
|
|
|
|
Pete O'Hanlon wrote: That's why you take the copy. Really appreciate your explanation, Pete ! From what you say, I infer that when you do something like:
var handler = PropertyChanged;
Then an actual copy of the underlying handler is made ? Not just a "duplicate of the pointer" ? My struggle to form some kind of practically useful "mental model" of EventHandlers and InvocationQueue ... continues, although I don't have any real problem using them, right now ... albeit my usage does not include dealing with possible threading issues, yet. What saved my read-end was learning that you can effectively switch a specific EventHandler instance in/out by doing a -= remove call which, praise be, does not throw an error if the EventHandler == null.
From now on, I believe I can summon your presence by simply invoking the names of the two Richards
«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008
|
|
|
|
|
As I understand it, it's a "duplicate of the pointer", not a deep clone of the delegate. When you add or remove a handler, the "pointer" is replaced with a new instance, because delegates are immutable.
Delegates are immutable; once created, the invocation list of a delegate does not change.
...
Combining operations, such as Combine and Remove, do not alter existing delegates. Instead, such an operation returns a new delegate that contains the results of the operation, an unchanged delegate, or null.
Again, Jon Skeet has some good information: C# in Depth: Delegates and Events[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I had the same question and so I made a test. In this test I made a copy of the handler as suggested var handler = PropertyChanged; , then let remove the handler from the subscriber (-=) and see, handler still holds the copied subscription(s). So here copy means more Clone I think.
I hope it helps
modified 19-Jan-21 21:04pm.
|
|
|
|
|
The delegate is immutable. So handler -= someHandler; is not modifying or updating the original delegate, it is creating a new delegate which is then stored in handler . So the copy-of-the-handler is indeed a copy of the reference to the (immutable) delegate.
|
|
|
|
|
Thank you very much for the this reply
modified 19-Jan-21 21:04pm.
|
|
|
|
|
I can't find simple solution for the case below.
How can I get time of some action in my code, and display it simpe through Console.WriteLine() in format [Date] [Time] [Action]?
For example I need time when "Ford" is created:
Car c= new Car();
c.createCar("Ford");
|
|
|
|
|
c.Created = DateTime.Now
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Why don't you modify the 'createCar Method in the 'Car class. Example:
public class Car
{
public void createCar(string carname)
{
var dt = DateTime.Now;
Console.WriteLine("\nCar {0} created at: {1} {2}", carname, dt.ToShortDateString(), dt.ToShortTimeString());
}
} If you need to keep track of all the 'Cars created by manufacturer, consider using another class (static ?) that maintains a collection of 'Cars. Or, you could use a static Dictionary<string, List<DateTime>> in the 'Car class.
public class Car
{
public static Dictionary<string, List<DateTime>> ManufacturerToCars = new Dictionary<string, List<DateTime>>();
public void createCar(string carname)
{
var dt = DateTime.Now;
if(! ManufacturerToCars.ContainsKey(carname))
{
ManufacturerToCars.Add(carname, new List<DateTime>());
}
ManufacturerToCars[carname].Add(dt);
Console.WriteLine("\nCar {0} created at: {1} {2}", carname, dt.ToShortDateString(), dt.ToShortTimeString());
}
}
«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008
|
|
|
|
|
Hi,
I am using MySQL for employee WinForm application. Some tables have images and Ia m currently saving it as blob which is killing the size of the database.
I want now to move it to a varchar field contains the file name or ID and save the physical file on the server instead of blob.
I want to know what's the best practice in this? Do I need to include FTP server and client in my application?
Is it possible to send the file with the database SqlConnection but receive it there in the server as physical file?
Please assist..
Thanks,
Jassim[^]
Technology News @ www.JassimRahma.com
|
|
|
|
|
No - the simplest solution is to create an "employee photo" folder on the server, and share it, giving read-only access. Your client apps then read the file name from the DB and access the share directly:
Access to remote folder[^]
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
but the folder will also have confidential documents such as contracts and warning letters for employee and giving read only access will allow users to view every file stored in it.
Technology News @ www.JassimRahma.com
|
|
|
|
|
You don't give the users the permissions to view the folder by default. Instead, have a server based application or a Web API retrieve the documents and have that with the appropriate permission. Then you back this up with access control for the user against that service.
This space for rent
|
|
|
|
|
Create a folder for specific document types, employee photos, contracts, correspondence etc
Or do as POH suggested and create a service, this is the preferred method as it removes the client access directly to the server. However this would require you to reengineer you data access
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hi!
I am able to send text messages to android using Google GCM by C# code.
But I am unable to send Images along with the text message.
Can anyone help in the same with some code.
--------------------------------
My code for sending message:
RegId = Id;
SENDER_ID = "1234567";
ApplicationID = "Google ID";
var value = txtnotofication.Text;
WebRequest tRequest;
tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send"); tRequest.Method = "post";
tRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
tRequest.Headers.Add(string.Format("Authorization: key={0}", ApplicationID)); tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message= {\"message\" : \"demo msg\",\"imgUrl\": \"http://justcash.co.in/img/logo0011.png \"}&data.time=" + System.DateTime.Now.ToString() + "®istration_id=" + RegId + "";
Console.WriteLine(postData);
Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
tRequest.ContentLength = byteArray.Length;
Stream dataStream = tRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse tResponse = tRequest.GetResponse(); dataStream = tResponse.GetResponseStream();
StreamReader tReader = new StreamReader(dataStream);
String sResponseFromServer = tReader.ReadToEnd();
tReader.Close(); dataStream.Close();
tResponse.Close();
|
|
|
|
|
See the answer to this[^] SO post.
/ravi
|
|
|
|
|
How to run the project with modules compiled on different platforms x86 and x64
or whether it possible to make a reference from x86 module to x64 attached DLL?
Actually there is no problem with compilation and assembly, but cannot run because of the following error
"An attempt was made to load a program with an incorrect format"
I'm aware that there is no way to run modules with different platforms in one process, but may be I can somehow reflect the methods from the dynamically linked DLL?
modified 22-Jul-16 3:59am.
|
|
|
|