|
Hello,
I've an application which reads and writes to a shared excel using C#.
The application runs on different computers and whenever anybody writes to the excel a pop-up appears on all the computers in which the application is running and displays the recent message written to excel file.
I am using FileSystemWatcher and an event is raised whenever something is written to the file.
If this event occurs I am reading the excel file from each of the process and displaying the latest message.
Now, when I am reading the excel file from different computers(processes) at the same time. All the processes does not get the latest message.
I am opening the excel in readOnly and releasing the excel object successfully.
Also, I have found that when many processes are trying to read from the same excel at the same time. The excel tends to crash.
I have added a thread.sleep(3000) so that there is a delay in reading the excel file. This did help but still the excel throws error messages.
Is there anyway that I can read excel file one after the other from each process?
|
|
|
|
|
what is the error message? or COM exception code?
Is there any inner exception?
These are valuable tips on what the problem could be!
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.
|
|
|
|
|
|
It sounds like you are taking every step possible to overcome some very common problems with network shares.
The FileSystemWatcher and sleep(3000) may not be enough. Try creating a locked file (FileStream with Non-Sharing Mode) that your "listeners" try to access. Remove the lock once the message has been added and the excel file is written.
But my guess is that you will never get rid of this problem. File access using network shares are never reliable when accessed from multiple computers simultaneously (in my experience).
|
|
|
|
|
Hello.
iam trying to read a KeyValuePair but it seems that iam doing soemthing wrong here, iam including a small part of the code where iam trying to read the value and also a capture from the debug, the debug shows me in "Locals" the info that i want to read.
var dataSourceName = (
from entry in entityParams
where entry is KeyValuePair<string, string> && entry != null && ((KeyValuePair<string, string>)entry).Key.Equals("DataSourceName")
select ((KeyValuePair<string, string>)entry).Value).FirstOrDefault();
I did not find a way to upload and include a image in this post, so i added it external instead.
Link to image
In this debug case its the value "ps3" that i need.
I have added the .dll switchking.common to my references and also
using SwitchKing.Common.Entities;
But now i need help what i need to do more to have access to the value.
|
|
|
|
|
It's not clear what yout problem is, but it looks like your criteria is to look for KeyValuePair<string, string=""> whereas the data is another type (KeyValuePair)(no generic clause! different type!)
BTW a let statement will make things cleaner
(
from entry in entityParams
let kv = entry as KeyValuePair
where kv != null && kv.Key = "DataSourceName"
select kv.Value
)
.FirstOrDefault();
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.
|
|
|
|
|
Thanks for the reply.
this is the error message that i receivs if i runt the code you posted.
The as operator must be used with a reference type or nullable type ('SwitchKing.Common.Entities.KeyValuePair' is a non-nullable value type)
|
|
|
|
|
You forgot to mention it's a compiler error!
mm... KeyValuePair is a value type!
the 'as' operator doesn't work with value type, because
'obj as type' evaluate as null is obj is not of type type, which is not possible with value type (they can't be null!)
scrap the 'as' too bad, try
(
from entry in entityParams
where entry is KeyValuePair
let kv = (KeyValuePair)entry
where kv.Key == "DataSourceName"
select kv.Value
)
.FirstOrDefault();
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station....
_________________________________________________________
My programs never have bugs, they just develop random features.
|
|
|
|
|
Thanks for the help, that solved my issue.
|
|
|
|
|
hi guys
i want connect and disconnect to internet by exist connection in the c#
how can i connect and disconnect to internet by exist connection in the c# ?
please guide me
thanks a lot
|
|
|
|
|
What do you mean "connect to internet"? You need to know what destination you are going to connect to, and how you will communicate to it. Please give some more detail of your problem.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
apadana_1989 wrote: how can i connect and disconnect to internet by exist connection in the c# ?
You can't using the standard definition of the terms you are using.
By definition if you close a connection it is done. It can't be used again.
Guessing what you might really want...
If you want the client box to have a single connection then you could connect to another server, and use it as an intelligent proxy to issue commands for you. So that server does the connect/disconnect and just redirects traffic to the client. But the server MUST exist for this to work.
If you are referring to a wireless/VPN/modem 'connection' via which your client app tunnels IP traffic then normally it stays up anyways. But if you want to control it explicitly because it is closing then you would need to get the API for that specific thing (wireless/VPN/modem) and control it yourself.
|
|
|
|
|
Hello folks,
Happy New Year!!!
I have a class that has couple of properties.
Can I have a property in that class that can be a collection. Like say the MarketData. can I have a property say "FailedCurves" . It is a string property. But only thing is, this property is an ArrayList or Dictionary object that can have multiple string items in it.
How can I do that in C#? Am I thinking correct?
Thanks much
public class MarketData
{
#region "Private Data Members"
private int _countAdvanceCurve;
private int _countAdvancesSBCAgencyCurve;
private int _countAdvancesSBCAAACurve;
private int _countFhlbsfTreasuryCurve;
private int _countDNCOCurve;
#endregion "Private Data Members"
#region "Public Data Members"
public int CountAdvanceCurve
{
get { return _countAdvanceCurve; }
set { _countAdvanceCurve = value; }
}
}
|
|
|
|
|
You can have a property that is a List or Dictionary, no problem -- but you may want to make it read only.
What exactly is confusing you?
|
|
|
|
|
Yes you can have any sorts of collection in a properyt.
public ObservableCollection<int> CountAdvanceCurve
{
get { return _countAdvanceCurve; }
set { _countAdvanceCurve = value; }
}
or
public Dictionary<int,int> CountAdvanceCurve
{
get { return _countAdvanceCurve; }
set { _countAdvanceCurve = value; }
}
and so on.
|
|
|
|
|
Yes you can.
When you create a property, you can return anything you want - including objects you create as required.
For example, if you want to return only those values which are below 1000:
public Dictionary<string, int> LowValues
{
get
{
Dictionary<string, int> dict = new Dictionary<string, int>();
if (_countAdvanceCurve < 1000) dict.Add("AdvanceCurve", _countAdvanceCurve);
if (_countAdvancesSBCAgencyCurve < 1000) dict.Add("AdvancesSBCAgencyCurve", _countAdvancesSBCAgencyCurve);
if (_countAdvancesSBCAAACurve < 1000) dict.Add("AdvancesSBCAAACurve", _countAdvancesSBCAAACurve);
if (_countFhlbsfTreasuryCurve < 1000) dict.Add("FhlbsfTreasuryCurve", _countFhlbsfTreasuryCurve);
if (_countDNCOCurve < 1000) dict.Add("DNCOCurve", _countDNCOCurve);
return dict;
}
} This is one of the big advantages of Properties over fields - you do not have to store anything in a way that makes sense to the outside world. You can store in the best way for your class, and provide the outside world with the info it want, how it wants it, only when it asks for it.
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
Thanks much. Best forum ever. I don't think I ever posted here...
This is the finished code.
private List<string> _curveswithnodata = new List<string>();
public List<string> Curveswithnodata
{
get { return _curveswithnodata; }
set { _curveswithnodata = value; }
}
public method()
{
if (basedonsomecondition)
{
this.Curveswithnodata.Add(CurveName);
}
}
|
|
|
|
|
It's a good idea to create a new List if you are going to return it to the outside world - remember that if you return a List, you actually return a reference to the list, so any changes made to it outside the class affect the version the class is using, but without you being notified. If you return a copy of a list then the outside world can do what it likes with the list without it affecting the one your class is using and potentially buggering up your data!
private List<string> _curveswithnodata = new List<string>();
public List<string> Curveswithnodata
{
get { return new List<string>(_curveswithnodata); }
set { _curveswithnodata = value; }
} The other solution is to return a read only version:
get { return _curveswithnodata.AsReadOnly());
But that returns a different type: ReadOnlyCollection<T> rather than List<T> which generally makes it harder to work with outside the class.
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
I am building a webservice (not WCF). I need to store two values (name,value pairs).
Instead of using List<string>, I used Dictionary<string,int>. But it give me error message saying "Can't serialize the dictionary object.
Which collections object I can use (to be able to serialize) to store name value pair in a webservice .
Thanks
|
|
|
|
|
Dictionaries are not serializable via SOAP or XML serializers - but they are via DataContractSerializer[^]
The only other option is to use a List<KeyValuePair<string, yourtype>> which (frankly) is a PITA!
But, a serializable PITA!
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
Griff,
Thanks. But you can't use this as a property of class. Can't have getters and setters.
nath
|
|
|
|
|
Maybe I just need sleep to recover from recent festivities - but I can't think of a way to fix this code to comply with the FX Cop warning.
The only thing that comes to mind is to create protected setters (or setter methods) in the base class and call those from the derived constructors...
public abstract class BaseClass
{
private int id;
protected BaseClass()
{
Initialize(out id);
}
public int Id { get { return id; } }
protected abstract void Initialize(out int id);
}
public class ConcreteA : BaseClass
{
public ConcreteA()
: base()
{ }
protected override void Initialize(out int id)
{
ConcreteAIntializationData data = NativeMethods.ConcreteAInitialize();
id = data.Id;
}
}
public class ConcreteB : BaseClass
{
private string name;
public ConcreteB()
: base()
{ }
protected override void Initialize(out int id)
{
ConcreteBIntializationData data = NativeMethods.ConcreteBInitialize();
id = data.Id;
name = data.name;
}
}
public static class NativeMethods
{
public static ConcreteAIntializationData ConcreteAInitialize()
{
return new ConcreteAIntializationData();
}
public static ConcreteBIntializationData ConcreteBInitialize()
{
return new ConcreteBIntializationData();
}
}
public class ConcreteAIntializationData
{
private int id;
public ConcreteAIntializationData()
{
id = 1;
}
public int Id
{
get { return id; }
}
}
public class ConcreteBIntializationData
{
private int id;
public string name;
public ConcreteBIntializationData()
{
id = 2;
name = "Name";
}
public int Id
{
get { return id; }
}
public string Name
{
get { return name; }
}
}
|
|
|
|
|
Simple, don't use FX Cop.
I didn't read that too closely, but shouldn't the (derived) constructor pass whatever to the base constructor?
I'm not a fan of the Initialize pattern either.
Sooo... I guess just ignore me.
|
|
|
|
|
This is one of those 'well it depends' rules. Think about the order in which chained constructors are called. The first thing derived class B's constructor does is call base class A's constructor.
If A's constructor now calls a virtual instance method defined in class B, it is accessing a partially initialised class. Remember that B's constructor has not run yet and all of B's instance data will be in an unknown state but it will be perfectly safe to call the instance method as long as it does not rely on instance data.
Examples of situations where this technique is used can be seen in the System.Windows.Forms class. Here you will find the protected bool ShowWithoutActivation property which is called internally and in the base class returns the constant value false. Overriding to return constant true changes the behaviour of a derived class. Similarly the property CreateParams can also be overridden to supply non default parameters to the base class.
So my feeling is it's OK to ignore CA2214 as long as you treat the overridden method or property as if it were static and don't do anything naughty.
Alan.
|
|
|
|
|
Cheers Alan!
Alan N wrote: don't do anything naughty
The overridden method is purely setting instance fields unique to that class to their initial values (these are not changed anywhere after instanciation - that part of the derived classes is immutable) and returning initialization values that are common to all derived classes to the base's constructor.
It seems like I should be OK to suppress the warning in this scenario.
|
|
|
|