|
A good way to do this is to use a try-finally block like so:
bool acquired = Monitor.TryEnter(yourLockedObject, maximumMillisecondsToWait);
try
{
if (acquired)
{
}
}
finally
{
if (acquired) Monitor.Exit(yourLockedObject);
} This is similar to what the lock statement does (i.e., compiles as), only it uses Monitor.Enter which no maximum wait time.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
You can force a thread to temporarily exit its synchronization domain by calling Monitor.Wait and specifying true for the exitContext parameter. The call will block until Monitor.Pulse or Monitor.PulseAll is called from another thread. It will then reenter the synchronization domain. This will increase the complexity of your code dramatically. If at all possible I always try to design an app so that it doesn't require this type of complex synchronization.
|
|
|
|
|
I have a couple of crystal reports in a C# app that will be exported excel by the client. While there is almost always data for every column/field of the reports, there are occasions where a particular field in a record is blank/null.
In the Crystal report viewer, everything looks great - even the blank data is lined up in the appropriate columns, etc. However, when I export to excel, any field that did not have data seems to disappear and then all the columns to the right of it shift over.
Kind of messy...and very misleading because data is ending up in the wrong columns in the spreadsheet.
How do I solve this? I tried playing around with the "convert NULL values to default" but didn't get anywhere. Is there a way to just specify a default value of a blank string or something so some value is present and nothing shifts?
Thanks!
|
|
|
|
|
Just use a function (either CR's scripting language or, IIRC, VBScript) that uses the data for that field and basically performs the ol' IIF-type functionality, where you either display the data if not null, or "display" something else (like " ").
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I want to define an arry of instance of ArrayList.
For example
ArrayList MyArrayList1 = new ArrayList();
ArrayList MyArrayList2 = new ArrayList();
ArrayList MyArrayList3 = new ArrayList();
ArrayList MyArrayList4 = new ArrayList();
In deed ,I hope MyArrayList1,MyArrayList2,MyArrayList3,MyArrayList4 are in a Array. Could realize it?
Thank you very much.
vigorous
|
|
|
|
|
ArrayList master = new ArrayList();
for (int i =0; i < 20; ++i) master.Add(new ArrayList());
((ArrayList)master[0]).Add(0);
etc.
I'm not sure this is the best way to go about it. A 2 dimensional array will allow for much more logical access.
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
ArrayList myListOfLists = new ArrayList();
for(int i=0; i<4;i++)
myListOfLists.Add(new ArrayList());
This will create your four arrays inside another array. Is this what you are looking for?
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
Coming soon: The Second EuroCPian Event
|
|
|
|
|
Let me add my valuable two cents' worth to the wealth of opinion on your vital question.
So you want an array of ArrayLists, not an ArrayList of ArrayLists, yes? Here you go:
ArrayList[] arrayLists = new ArrayList[4];
for(int x = (arrayLists.Length - 1); x >= 0; x--) {
arrayLists[x] = new ArrayList();
}
Your choice is not that inappropriate if you definitely know how many shrinkable/growable arrays you'll be needing, need to access them based on index, don't want to write code to manage the size of the individual arrays, and don't want the performance hit of using an ArrayList for the first index access. Of course, if you really want speedy performance, you'll eschew the use of ArrayLists altogether and manage your own arrays completely.
Regards,
Jeff Varszegi
EEEP!
|
|
|
|
|
Jeff Varszegi's answer is the best one I actually need. Thanks a lot!!
And Thanks to Christian and Colin Angus Mackay. Both you taught me some valuable thing.
vigorous
|
|
|
|
|
You're welcome!
Regards,
Jeff Varszegi
EEEP!
|
|
|
|
|
How would I go about adding a dataset of new items to a table in a Database, is there a special feature in SqlClient that will allow me to do this, or do I have to iliterate through all of the rows in the dataset and add it to the database.
-Steven Hicks
CPACodeProjectAddict
|
|
|
|
|
See the SqlDataAdapter documentation in the .NET Framework SDK. Make sure the SelectCommand and InsertCommand properties are set (at least, for new items only) and call SqlDataAdapter.Fill on the DataSet . The table and column definitions must match up with your SQL tables or stored proc parameters, or use some aliasing (where applicable) in your commands. Typed DataSet can greatly help in this situation.
Also, make sure you do not call DataSet.AcceptChanges or DataTable.AcceptChanges before you call Fill , otherwise nothing is sent. If you want to improve performance by decreasing bandwidth loads, call DataSet.GetChanges to get a DataSet with just the changes excluding any unmodified data (which really wouldn't apply if your DataSet contains all new records, but this is handy to know).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Anyone know where I could acquire 3rd-party form controls to add to my toolbar?
I'm only interested in free ones at the moment.
Any links, or help will be appreciated.
Thanks in advance,
Jon
|
|
|
|
|
|
MORE!! I need more!
|
|
|
|
|
Actually, is there a place on www.codeproject.com that lists these downloads? Or is it mostly just searching around the site to find them?
|
|
|
|
|
You can browse by category. Go to the homepage to begin.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
You can also download (or order) VB resource kit from MS website. There are some visual basic tools and docs and couple FREE .NET components like ComponentOne Studio Enterprise (a lot of controls, improved datagrid, toolbar/menu/outlook bar component etc.), DundasCharts, Sax.NET and Infragistics WebNavigator.
I am using ComponentOne controls in my projects - they are worth their price (0$ )
but - be warned - it has some bugs.
|
|
|
|
|
I've just started to learn C# and am trying to use it to create a web service, and I have a question that I hope can be answered.
I have a set of instructions to be run before each instance of the service is started. What I'm trying to do is put some information from a file into memory and then the service will find the information it needs and return that to the user.
I currently have it working where it reads the files from disk every time a request is received, but that seems horribly inefficient, and was wondering if there was a way to have this done once?
Thanks for your help
|
|
|
|
|
It sounds like caching the data with a dependancy on the file may be the solution you're looking for. Take a peek at the System.Web.Caching namespace on MSDN[^].
Hope that helps.
--Jesse
|
|
|
|
|
Thanks for the help. However, I'm really new and have another question. Where would I put the CacheDependency?
|
|
|
|
|
Because you must check the cache each time you get the item from it, you'd want to encapsulate this in a method that the constructor (called when the Web Service class is instantiated) and your methods can call to get the object from the cache:
[WebService]
public class MyWebService : WebService
{
public MyWebService()
{
GetContent();
}
[WebMethod]
public void DoSomething()
{
string content = GetContent();
if (content != null)
}
private string GetContent()
{
string content = (string)Context.Cache[CacheName];
if (content == null)
{
string path = Server.MapPath("/path/to/virtual/file.txt");
using (StreamReader reader = new StreamReader(path))
{
content = reader.ReadToEnd();
reader.Close();
}
Context.Cache.Add(
CacheName,
content,
new CacheDependency(path),
Cache.NoAbsoluteExpiration,
Cache.NoSlidingExpiration,
CacheItemPriority.Default,
null);
}
return content;
}
private const string CacheName = "CONTENT";
} This places the content of a file in the cache. The CacheDependency makes sure that if the file is changed, the cache item is invalidated. This means that null would be returned when trying to get the item from the cache. In this case, you read-in the content of that file and re-add it to the cache.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Your question is not clear as you use "each instance" and yet you seem to want to have a global value. If what you are trying to accomplish is to save yourself from having to re-read a semi-static file, here is what I would try...
// THE RESULTANT DATA
object myData = null;
// THE NAME OF THE FILE
string myFileName = @Server.MapPath(@"\dir\filename");
// GET THE INFO
System.IO.FileInfo fileInfo = new System.IO.FileInfo(myFileName);
// DOES IT EXIST
if (fileInfo.Exists)
{
// THE NAME OF THE DATA KEY
string myDataKey = "DATA";
// THE NAME OF THE TIMESTAMP KEY
string myTimestampKey = "DATETIME";
// GET THE PREVIOUS TIME STAMP (IF ANY, ELSE A NULL)
DateTime myTimestamp = Application[myTimestampKey] as DateTime;
// GET THE TIMESTAMP
DateTime currentTimestamp = fileInfo.LastWriteTime;
// GET THE PREVIOUS DATA (IF ANY, ELSE A NULL)
myData = Application[myKey];
// ANY PREVIOUS DATA?
if (myData != null)
{
// DO WE HAVE A TIMESTAMP?
if (myTimestamp == null)
{
// SHOULD NOT HAPPEN, BUT JUST IN CASE (DATA AND NO TIMESTAMP)
myData = null;
}
else
{
// SAME?
if (!myTimestamp.Equals(currentTimestamp))
{
// FILE CHANGED, RE-READ
myData = null;
}
}
}
// ALREADY THERE?
if (myData == null)
{
// GET THE DATA
myData = ...;
// AND SAVE FOR NEXT TIME
Application[myDataKey] = myData;
// AND SAVE THE TIMESTAMP
Application[myTimestampKey] = currentTimestamp;
}
}
Have fun.....
|
|
|
|
|
Sorry for the confusion. What I want is a global value. I think I understand your example (thank you), however, I'm not sure where to put it in my code. The only place I can see to add it would be in the constructor, but I'm not sure if this would cause this code to be run each time the service received a request (I think it would).
If I understand everything correctly, then if I put your example in the constructor, then the first time the service handled a request (or when the file was changed) it would get the data, other wise is would skip the step to load the data. Is that right?
Thanks for your help!
|
|
|
|
|
When I try this:
DateTime tpmTimestamp = Application[tpmTimestampKey] as DateTime;
the compiler tells me "The as operator must be used with a reference type ('System.DateTime' is a value type).
I'm sorry if this seems really trivial, but I'm just learning C#
<edit>
I've found a way to fix this:
if(Application[tpmTimestampKey] == null)
tpmTimestamp = CurrentTime;
else
tpmTimestamp = (DateTime)Application[tpmTimestampKey];
Thanks for your help!
|
|
|
|