|
Hi Guffa,
Thanks for your suggession. It is a bad news for me that you said I must use Locking of static variabls to make it thread safe. Now I will have to convert all my static variables to instance variables in my whole application as those variables are shared by threads. ((((((
If I use instance variable, then I dont need to lock as long as the instance is created within the thread, right ?
Is this locking required for only static variables ? or do I need to lock Static Methods too ?
I did not like to learn Lock chapter of C#. Is there any simple snippet you can show me to lock a variable ?
Thanks and regards
Emran.
|
|
|
|
|
Making the variables not static will not solve your problem. If your app is multi threaded, and the threads all access a variable that exists in a single instance, then you need to use locking. Static simply defines the fact that there is only one variable, but if you move the variable to an instance, and pass an instance between threads to access the variable, you've created a new problem ( passing a reference to the class ) and not solved the old one ( multi threads all hitting the same variable ).
bashiwala wrote: I did not like to learn Lock chapter of C
Why not ?
bashiwala wrote: Is there any simple snippet you can show me to lock a variable ?
Isn't it just the lock keyword and a block that does the work ?
bashiwala wrote: Is this locking required for only static variables ? or do I need to lock Static Methods too
Any code which changes a variable where that instance is shared across threads needs to be locked. Do you understand what the issue is ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Hi Christian,
Thanks a lot for detailed answer.
Ok, I tried to learn the lock but I did not understand from MSDN documentation.
Say for example, I have following variable, and method,
..............................................................
static int a = 8;
public static void myMethod(string myValue)
{
// do some change on XML.
}
..............................................................
Now, How can I just lock both of this variable and method ?
Will the following snuippet work ?
..........................................................
lock
{
static int a = 8;
}
lock
{
public static void myMethod(string myValue)
{
// do some change on XML.
}
}
................................................................
thanks and regards
Emran
|
|
|
|
|
No, you've got it wrong:
public static void myMethod(string myValue)
{
lock
{
// do some change on XML.
}
}
You're locking the portion of code that needs to be atomic ( it needs to all run before another thread tries to run any of it ).
You want to lock as much as you need, and as little as possible.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Hi Christian,
Thank a loot for the help.
I have some const variables in my form like,
public const string dataPassword = "abcd";
public static string myValue = "1234";
This variable is shared by all threads simultaneously but this value is not changed by any of the thread. So, do you think I must use lock to protect this kind of variables ?
Thanks and regards
Emran
|
|
|
|
|
I'm not sure that you're getting the concept. The concept is, if two threads change a value, it's possible that data will be lost if one is half way through doing something and the other one steps in. If values are not being changed, no locking is required.
you would, however, be better off doing something like this:
public static string DataPassword { get { return "abcd"; } }
or at least mark then all readonly. static values can still be changed.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Hi Christian,
thanks a lot.
Now I understood the concept.
|
|
|
|
|
Hi,
I am grateful for your previous guidances.
Would you please help me about a confusion.
In my Application, I have an XML file where only one thread WRITEs to this file. But multiple thread shares this file for READING. Do I need to apply LOCK in this case? Is there any chance of thread conflict while one thread is trying to write to the file and another thread is trying to read the file? (I thought confliction arises only when both of the threads are trying to write to the file).
Regards,
Emran
|
|
|
|
|
Hi All,
anyone know the left function in C#?
Thanks
|
|
|
|
|
|
String cMyString = "This is my string";
MessageBox.Show(cMyString.<code>Substring(0,5)</code>);
[edit]oops...Jon got here first...[/edit]
--EricDV Sig---------
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them.
- Laurence J. Peters
|
|
|
|
|
Fellows i didnt get it. why is it called "Left", further clarify me
thanks in advance
|
|
|
|
|
There is no "LEFT" function, but you can use Substring to return the left portion of a string:
The following will display: This
String cMyString = "This is my string";
MessageBox.Show(cMyString.Substring(0, 4));
The following will display: This is
MessageBox.Show(cMyString.Substring(0, 7));
--EricDV Sig---------
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them.
- Laurence J. Peters
|
|
|
|
|
Thanks alot
i was only confused coz of the term 'LEFT', otherwise i know how to use this method.
|
|
|
|
|
I mean when I browse webpage in IE browser.There are many URL links on page, when mouse move through webpage,how can i capture the URL address that mouse currently point to?I mean capture the address from my own program!
Hope you catch my meaning.Thanks
|
|
|
|
|
How can I add scientific values such as "2 to the 3rd power" plus "2 to the 4th power"?
Thanks
-Goalie35
|
|
|
|
|
Math.Pow(2, 3) + Math.Pow(2, 4)
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
Hi all,
I wonder if I can find how to set the resolution for a Metafile.
I create Graphics object using
IntPtr hwnd = (IntPtr)Win32.GetDesktopWindow();
return Graphics.FromHwnd(hwnd);
The resolution il always 96x96. Since I need to print the Metafiles, I wish to have at least 600x600.
I tried also to create graphics from a bitmap with resolution 600x600 but it doen't work.
Any suggestion?
|
|
|
|
|
Ok, I know this sounds noob, but I'm learning as I go...
Up to now, I have read in various strings (for prog settings) from a separate text file. I'd like to put all that string info in the program.
I have embedded four [name]=[value] lines into the program (using resgen, going from .txt to .resx to .resources, and using the /resource csc.exe switch), but the ResourceManager only lets me read the information. If I only needed to read the info, I'd just use private string members in the program itself. However, the values change at runtime, and I'd like to save the changes (without using other files outside of the executing file).
I've looked briefly (I try to search first, before running to the forum for help), but didn't see anything that would help me just save this information.
-Daniel
Typing too fast fro my owngood
|
|
|
|
|
Hi there!
I've made some kind of tooltip for items included in a ListBox which appears near the cursor when the it's hovering an item in the ListBox.
Because I didn't knew better how to make a tooltip I did it with a simple Windows-Form with some options to make it look like a nice tooltip. When an item in the listbox is hovered by the mouse the tooltip shows up as I want it to. But I still have a few problems:
1. The tooltip-window is always getting the focus and that makes the main-form flickering. How can I prevent this? I set the focus back manually but it's still flickering a bit. Best thing would be the focus wouldn't change at all or something like that.
2. Setting the location for the tooltip makes it act weird. When I set it in the constructor or after the initialization of the tooltip (with this.Top and this.Left as well es with this.Location both based on Cursor.Position) it always appears at the same starting location. By moving the mouse and triggering the event it's moving along across the screen but always on the same route ignoring the cursor-position (seems like the position is stacking its values somehow). When I set it after displaying the form (with Show()) it works.
3. Is this actually the right way to create a tooltip or something like a flying info-box? (I mean by using a Windows-Form and opening it on demand.) It doesn't seem so for me because of the problems I have it looks like it's not ment to be done this way.
I hope someone can help me.
Maybe you want to see the code of my MouseMove-Event:
int taskIndex = listBoxTasks.IndexFromPoint(e.Location);
if (taskIndex > -1)
{
Task currentTask = ((Task)bindingSourceTasks[taskIndex]);
if (currentTask != lastTask)
{
if (tooltip != null)
{
tooltip.Close();
}
tooltip = new frmTooltipComponent(currentTask);
tooltip.Show();
this.Focus();
tooltip.Top = Cursor.Position.Y;
tooltip.Left = Cursor.Position.X + 50;
lastTask = currentTask;
}
else
{
tooltip.Top = Cursor.Position.Y;
tooltip.Left = Cursor.Position.X + 50;
}
}
else if (tooltip != null)
{
tooltip.Close();
lastTask = null;
}
In MouseOut-Event of the ListBox I'm also closing the tooltip and clearing the last remembered item.
-- modified at 11:15 Tuesday 19th September, 2006
|
|
|
|
|
Hi,
how we can call a method from other .cs file...
I mean I have a method "public void Employee( )" in Default1.aspx.cs file... I want to call this method from Default2.aspx.cs file.. I am using asp.net 2.0. Pls guide me.
Thanks,
Hosanna.
|
|
|
|
|
public static void Employee(
{<br />
<br />
}
on Default2.aspx.cs just call name of method
Employee(//here goes parameters if u have any)
|
|
|
|
|
thanks for reply. Can I call button event handler from another page.
Eg,
protected void btnSave_Click(object sender, EventArgs e)
{
} // which is in Default.aspx.cs file.
If I click on "Submit" button from Default2.aspx file can I call the above event handler?
pls suggest me. If you send sample code I would appreciate.
Thanks,
Hosanna.
|
|
|
|
|
The proces you have sent is not working. I am using asp.net 2.0. Pls see and suggest me.
Regards,
hosanna.
|
|
|
|
|
The core issue here is, if you have methods that are needed across pages, if they relate to the page lifecycle, they should perhaps go in a new base class for all pages. If they relate to business logic, they should be in a seperate class, in your middle tier.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|