|
Try Google[^]. There are some pages that might be helpful.
|
|
|
|
|
I'm trying to write an ASP.NET application with thread safety in mind (not one of my strength points). I use the static class shown below for the application settings (not the real code, just a simplified version):
public static class AppSettings
{
private static int _setting1;
public static int Setting1
{
get { return _setting1; }
}
private static string _setting2;
public static string Setting2
{
get { return _setting2; }
}
static AppSettings()
{
LoadSettings();
}
public static LoadSettings()
{
}
public static ReloadSettings()
{
LoadSettings();
}
}
As you can see in the code above, LoadSettings() is called from two locations, the first is from the static constructor (no problem here, the static constructor is thread safe) and from ReloadSettings() (where the problem is) what I'm trying to do is that I want to block all threads from accessing the settings in the private members (accessed through the properties) until all the settings are updated, is this possible using a static class?
I figured out that I could change the code to be as below and the problem will be solved:
public static class AppSettings
{
private static AppSettingsData _appSettingsData;
public static int Setting1
{
get { return _appSettingsData.Setting1; }
}
public static string Setting2
{
get { return _appSettingsData.Setting2; }
}
static AppSettings()
{
LoadSettings();
}
public static LoadSettings()
{
int setting1;
string setting2;
AppSettingsData settingsData = new AppSettingsData(setting1, setting2);
_appSettingsData = settingsData;
}
public static ReloadSettings()
{
LoadSettings();
}
}
public class AppSettingsData
{
public AppSettingsData(int setting1, string setting2)
{
_setting1 = setting1;
_setting2 = setting2;
}
private int _setting1;
public int Setting1
{
get { return _setting1; }
}
private string _setting2;
private string Setting2
{
get { return _setting2; }
}
}
Although this would solve the problem, I actually still prefer to use a static class for everything (unless of course it's not possible using a static class), and also out of curiosity I would like to know whether this is possible or not with only a static class.
Your help is very much appreciated...
modified on Friday, April 11, 2008 9:31 AM
|
|
|
|
|
When paranoia strikes I use lock .
|
|
|
|
|
Waleed Eissa wrote: what I'm trying to do is that I want to block all threads from accessing the settings in the private members (accessed through the properties) until all the settings are updated
You can use ReaderWriterLock[^] class in System.Threading namespace. When you acquire a writer lock, all the read requests will put into a queue and will be processed when the writer lock gets released. I guess it can solve your problem.
Waleed Eissa wrote: I'm trying to write an ASP.NET application with thread safety in mind
This is C# forum, this question should be in ASP.NET forum.
|
|
|
|
|
Don't use ReaderWriterLock , there are bugs in it which can cause writer starvation, performance issues, and resource leaks. Use the ReaderWriterLocksSlim class from the .NET 3 framework instead, or use one of the high-performance locks from the free Power Threading library.
|
|
|
|
|
Thanks for clearing that.
Judah Himango wrote: use one of the high-performance locks from the free Power Threading library.
Can you tell me which are these ? Do you mean Monitor.Enter type of lock ?
|
|
|
|
|
|
Thanks a lot for the reply, well, I mentioned that my application is an ASP.NET application but the question is about thread safety in C# not about ASP.NET, and the code in my post would look exactly the same in a Windows Forms application, right? Nothing ASP.NET about it .. so, objection overruled
|
|
|
|
|
Thank you guys for all your replies, actually I'm using .NET 2.0 and wasn't planning to use any external libraries, so it seems a little confusing now ..
|
|
|
|
|
I am creating a usre control which needs to filter a generic list object based on text entered by the user
the filter must be real time.
The problem is that i need to create a generic control which allows you to bind a custom object to the control and then
in visual studio display all the members of the class. you then select which members are avaliable for user criteria to
be searched on.
Thank you
|
|
|
|
|
I am writing a c# application where I need to use matlab functions from my c# sharp code. From the beginning I used the Matlab Automation Server, which worked fine on my computer which has Matlab 7.4.0 (R2007a) installed. However, the application needs to be usable on computers with older versions of Matlab, and it turned out that the automation server can't be used then.
Now I am trying to solve this problem using Matlab Compiler to create dll files of my matlab functions and then call them from c#, based on these two code examples: http://www.mathworks.com/support/solutions/data/1-X1PFC.html
http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=12987&objectType=file
My problem has to do with passing arrays to and from my matlab functions. As long as I only use scalar values or arrays containing only one value, everything works fine, but as soon as the array contains more than one value an error occurs.
This is my code:
The copy.m matlab function only returns a copy of the input argument:
function y = copy(a)<br />
y=a;
This is my c# code (the try and catch statements have been removed here to make more readable):
[DllImport(@"copy.dll")]<br />
private static extern bool copyInitialize();<br />
[DllImport(@"copy.dll")]<br />
private static extern void copyTerminate();<br />
[DllImport(@"copy.dll")]<br />
private static extern bool mlfCopy(int num, ref IntPtr output, [In]IntPtr input);<br />
<br />
[DllImport(@"mclmcrrt76.dll")]<br />
private static extern IntPtr mxCreateDoubleMatrix_700([In]Int32 numRows, [In]Int32 numCols, [In]Int32 mxComplexity);<br />
[DllImport(@"mclmcrrt76.dll")]<br />
private static extern IntPtr mxGetPr([In]IntPtr mxArray);<br />
[DllImport(@"mclmcrrt76.dll")]<br />
private static extern bool mclInitializeApplication(string options, int count);<br />
[DllImport(@"mclmcrrt76.dll")]<br />
private static extern void mclTerminateApplication();<br />
<br />
static void Main(string[] args)<br />
{<br />
double[] copyInput = { 1.0};<br />
<br />
mclInitializeApplication("NULL", 0);<br />
copyInitialize();<br />
<br />
IntPtr inValA = mxCreateDoubleMatrix_700(copyInput.Length, 1, 0);<br />
<br />
int size = Marshal.SizeOf(copyInput [0]) * copyInput.Length;<br />
<br />
IntPtr outVal = Marshal.AllocHGlobal(size);<br />
<br />
Marshal.Copy(copyInput , 0, inValA, copyInput .Length);<br />
<br />
mlfCopy(1, ref outVal, inValA);<br />
<br />
double[] copyAns = new double[copyInput .Length];<br />
Marshal.Copy(outVal, copyAns, 0, copyInput.Length);<br />
<br />
copyTerminate();<br />
mclTerminateApplication();
When running this code the returned value is identical to the value in copyInput, however, as soon as the size of copyInput is changed (double[] copyInput = { 1.0, 2.0}; ) an error occurs (without any more information than a standard windows error message). I have also tried to use
[DllImport(@"copy.dll")]<br />
private static extern bool mlxCopy(int numOut, ref IntPtr output, int numIn, [In]IntPtr input);
and
mlxCopy(1, ref outVal, 1, inValA);
Instead of the mlfCopy, but this doesn't work either.
Is there anyone who has an idea of how to solve this problem? What I really need is to be able to pass two-dimensional arrays to and from matlab functions.
Thanks in advance!
|
|
|
|
|
can any one help me!!
I want to create a application like a photoshop in .net so please help me
jeny
|
|
|
|
|
Hmmm... Where do you start with this one? Codeproject? If you don't understand how ridiculous a question this is....
Try getting involved with an open source project that is making a photoshop clone.
Scott
"Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand
|
|
|
|
|
Download the source to paint.net and have a look at it. Although, this seems a little bit of an ambitious project for you. Are you sure you want to attempt this on your own.
|
|
|
|
|
|
BWAHAHAHA
|
|
|
|
|
prachi14 wrote: I want to create a application like a photoshop in .net so please help me
Since I'm currently developing a new OS , like Windows XP in .NET , I have no spare time to help you.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
|
|
|
|
|
Surely this should have been posted on 1st April.
If you don't know where to start I suspect you have absolutely no chance of success.
Bob
Ashfield Consultants Ltd
|
|
|
|
|
|
Hey
I have created a metode to create RadiobuttonList dynamically but when i show it in my page end i chec them to false or true and when i click the button all radiobutton is coming back to true.
I have a function to read wgat user have selected :
Hope i can help. her is som code to create radiobuttonlist:
foreach (DataRow dr in ds.Tables[0].Rows)
{
Label li = new Label();
System.Web.UI.WebControls.ListItem ls = new System.Web.UI.WebControls.ListItem();
RadioButtonList rl = new RadioButtonList();
rl.Items.Add(new System.Web.UI.WebControls.ListItem("Inkl", dr["Type"].ToString()));
rl.Items.Add(new System.Web.UI.WebControls.ListItem("Eks", dr["Type"].ToString()));
rl.RepeatDirection = RepeatDirection.Horizontal;
Panel2.Controls.Add(li);
Panel2.Controls.Add(rl); //tilføjer dem til panelet
}
then when i click a button hwo called checkthevalue, i called this metode:
foreach (Control cr in Panel2.Controls)
{
if (cr is RadioButtonList)
{
RadioButtonList rb = (RadioButtonList)cr;
foreach (System.Web.UI.WebControls.ListItem ls in rb.Items)
{
if (ls.Selected && rb.SelectedItem.Text == "Eks")
{
//this ad a line to pdf file
document.Add(new Paragraph("-" + rb.SelectedItem.Value.ToString(), FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12)));
}
}
}
}
Hope i can help me.
|
|
|
|
|
I am designing a custom component (Non visible componenent). I inherited my class from System.ComponentModel.Component class. I know Component implements IDisposable . But how could I release resources used by my control ? Component class doesn't allow overriding Dispose . So how to dispose the objects used in my custom control ? Do I need to implement IComponent directly in my custom control ?
|
|
|
|
|
Declare the Dispose in your derived class with the new qualifier.
Scott
"Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand
|
|
|
|
|
carbon_golem wrote: Declare the Dispose in your derived class with the new qualifier.
Thanks for the reply. But it won't work when ((IDisposable)componentInstance).Dispose() called as it's not a IDisposable member. And I think it's not a good practice to use new keyword which hides the base method.
|
|
|
|
|
Ok, Implement IDisposable on your derived class and forward the call to the base class's Dispose method. As for using the new modifier, FxCop doesn't complain about it.
Scott
"Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand
|
|
|
|
|
Thanks, I will give a try
carbon_golem wrote: FxCop doesn't complain about it.
FxCop ? I am pretty new to that. Let me try using that. Thanks again
|
|
|
|