|
Hi,
what is the use of parameter passed in lock block and why it must be always reference type?
Thanks.
|
|
|
|
|
It's just something to hold onto. Only one thread can hold it at a time, all the others have to wait for it to be passed.
|
|
|
|
|
Thanks. But you know it is not expected that the parameter should be used inside code, the protected variable can be something else then parameter. So there is no sense in passing parameter like we normally use in methods.
|
|
|
|
|
What I think you are saying is that it makes no sense to use "lock(parameter)" when we could just use "lock". If so, then you are wrong - "lock(parameter)" allows multiple, different locks: "lock(myBuffersSection)" and "lock(myXMLOutputSection)". In this way, only the threads that need to be frozen until a specific lock is available are frozen - all the others can continue.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
OK, so can you tell me that when we are sending parameter to the lock block is it treated as an input parameter similar to method? Like we send parameter to the method and method will use it.
|
|
|
|
|
This is going to be a bit complicated, and I strongly recommend you find a book and read up on multithreading, because it is quite important that you understand what (and why) it happens.
The lock keyword requires you to specify a token (an object reference) that must be acquired by a thread to enter within the lock scope. When you are attempting to lock down a private instance-level method, you can simply pass in a reference to the current type:
private void SomePrivateMethod()
{
lock(this)
{
}
}
However, if you are locking down a region of code within a public member, it is safer (and better practice) to declare a private object member variable to serve as the lock token:
public class MyClass
{
private object threadLock = new object();
public void MyMethod()
{
lock (threadLock)
{
...
}
}
}
Why does it have to be an object? Why can't it be an integer? Or a bool?
This is more complex, and it goes back to the beginnings of what you probably learnt when you were starting. Do you remember all that suff about Value types and Reference types? Well, the lock token must be a reference type, because otherwise when you pass it to the lock block, it would get re-packaged into a reference type container, which would then be locked. If a second thread came along and passed it to a lock block again, it gets re-packaged again, and a different reference type container gets locked.
Remember that (like so many things in C#) "lock(token)" is a "short cut" for a method call - in this case Monitor.Enter and Monitor.Exit - so you are actually passing the token to a method. The parameter works teh same as it would for any other method, packaging and all.
Does this make sense? It is a bit complicated!
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
The parameter is a way of identifying a particular synchronization scope. It must be visible to every method that participates in that synchronization. It is usually declared in a class as:
class UsesSynchronization
{
private Object key = new Object();
public SafeMethod()
{
lock( key )
{
...
}
}
}
It must be a reference type because a value type would be boxed in a different object for each call to lock , which is actually syntactic sugar for Monitor.Enter and Monitor.Exit .
Nick
----------------------------------
Be excellent to each other
|
|
|
|
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
using System.Reflection;
namespace testtesttestExcel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//Declare these two variables globally so you can access them from both
//Button1 and Button2.
Microsoft.Office.Interop.Excel.Application objApp;
Microsoft.Office.Interop.Excel._Workbook objBook;
Microsoft.Office.Interop.Excel.Workbooks objBooks;
Microsoft.Office.Interop.Excel.Sheets objSheets;
Microsoft.Office.Interop.Excel._Worksheet objSheet;
Microsoft.Office.Interop.Excel.Range range;
private void button1_Click(object sender, System.EventArgs e)
{
try
{
// Instantiate Excel and start a new workbook.
objApp = new Microsoft.Office.Interop.Excel.Application();
objBooks = objApp.Workbooks;
objBook = objBooks.Add(Missing.Value);
objSheets = objBook.Worksheets;
objSheet = (Microsoft.Office.Interop.Excel._Worksheet)objSheets.get_Item(1);
//Get the range where the starting cell has the address
//m_sStartingCell and its dimensions are m_iNumRows x m_iNumCols.
range = objSheet.get_Range("A1", Missing.Value);
range = range.get_Resize(5, 5);
//Create an array.
double[,] saRet = new double[5, 5];
//Fill the array.
for (long iRow = 0; iRow < 5; iRow++)
{
for (long iCol = 0; iCol < 5; iCol++)
{
//Put a counter in the cell.
saRet[iRow, iCol] = iRow * iCol * iCol;
}
}
//Set the range value to the array.
range.set_Value(Missing.Value, saRet);
objApp.Visible = true;
objApp.UserControl = true;
}
catch( Exception theException )
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat( errorMessage, theException.Message );
errorMessage = String.Concat( errorMessage, " Line: " );
errorMessage = String.Concat( errorMessage, theException.Source );
MessageBox.Show( errorMessage, "Error" );
}
Microsoft.Office.Interop.Excel.Range currentFind = null;
Microsoft.Office.Interop.Excel.Range firstFind = null;
string A = "16";
// You should specify all these parameters every time you call this method,
// since they can be overridden in the user interface.
currentFind = objSheet.Cells.Find(A, Type.Missing,
Microsoft.Office.Interop.Excel.XlFindLookIn.xlValues, Microsoft.Office.Interop.Excel.XlLookAt.xlWhole,
Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows, Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, false,
Type.Missing, Type.Missing);
while (currentFind != null)
{
// Keep track of the first range you find.
if (firstFind == null)
{
firstFind = currentFind;
//textBox1.Text = currentFind.get_Address(true, true, Microsoft.Office.Interop.Excel.XlReferenceStyle.xlA1, false, Missing.Value);
}
// If you didn't move to a new range, you are done.
else if (currentFind.get_Address(Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlReferenceStyle.xlA1, Type.Missing, Type.Missing)
== firstFind.get_Address(Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlReferenceStyle.xlA1, Type.Missing, Type.Missing))
{
break;
}
currentFind.Font.Color = System.Drawing.ColorTranslator.ToOl
(System.Drawing.Color.Red);
currentFind.Font.Bold = true;
currentFind = objSheet.Cells.FindNext(currentFind);
}
}
}
After finding the cell containing "16", i want to write in string data to the right of it. how should i go about doin this. I did some research on the codes, but i kept getting errors. Please advise me on my problem. Thanks alot!!
|
|
|
|
|
Hi,
How can I pass a Guid as a Parameter? I have the following method in my DAL but I cannot pass the value as Guid and I don't know how to do the conversion. I get this error "Specialized cast in not valid".
My method looks like this:
public dsWBGT GetPersonByUserId(Guid userId)
{
dsWBGT dsPerson = new dsWBGT();
WBGT.DAL.dsWBGTTableAdapters.PersonTableAdapter taPerson = new WBGT.DAL.dsWBGTTableAdapters.PersonTableAdapter();
taPerson.FillByUserId(dsPerson.Person, UserId);
return dsPerson;
}
Basically I am using ASPNET as the membership provider and I need to retrieve the user details based on the userID.
This is my method:
public void GetPersonByUserId()
{
frmUsers usersForm = frmUsers.GetChildInstance();
this.dsWBGT = personDataManager.GetPersonByUserId(UserId);
this.personBindingSource.DataSource = this.dsWBGT;
}
and finally my event looks like this:
private void dgvUsers_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 1)
{
Forms.Person.frmPerson personForm = new WBGT.Client.Forms.Person.frmPerson();
personForm.UserId = (Guid)dgvUsers.Rows[e.RowIndex].Cells[0].FormattedValue;
THIS IS WHERE IT FAILS :((
personForm.GetPersonByUserId();
personForm.MdiParent = frmMain.ActiveForm;
personForm.Show();
personForm.BringToFront();
}
}
Thank you!
Illegal Operation
|
|
|
|
|
Wouldn't a FormattedValue be a string? Not a GUID?
|
|
|
|
|
Not to worry, I have fixed it.
I Converted the Guid to a String and then Passed the string to the property, worked like a charm.
Thank you!
Illegal Operation
|
|
|
|
|
Taking a value, making it a string, then parsing it back to the value it was is wasteful and considered to be poor coding practice.
|
|
|
|
|
Does anyone know how to copy a transparent image out of a C# application into the clipboard, and then paste it into Photoshop? When I try this the transparency (alpha) is lost. Here's as far as I got, but it doesn't work (transparency is still lost):
IDataObject dataObject = new DataObject();
dataObject.SetData(bMap);
MemoryStream ms = new MemoryStream();
bMap.Save(ms, ImageFormat.Png);
dataObject.SetData("PNG", false, ms);
Clipboard.SetDataObject(dataObject, true);
|
|
|
|
|
See if this[^] helps.
Or maybe this[^] one. You'll have to read through the replies, to find one that works for you, I hope!
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Thank you very much for the links! I had been working on this problem all day and had came across them. The code snippet I posted was actually based on the second thread. The code snippet adds a PNG to the clipboard which allows transparent images to be posted to Excel. Unfortunately Photoshop, as well as most programs, don't seem to look for PNGs on the clipboard so it didn't help specifically with Photoshop.
|
|
|
|
|
Oh well.
Hope you find a solution.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
I want to know which one is bets to use with Windows Form? Crystal Reports or RDLC? and why plz?
|
|
|
|
|
|
hey , i am new to c# , with a bit of experience and i am wondering how i would go about making a back up program, such as syncing one folder with another.... For example; copying new data in folder 1 to folder 2 ... how would i go about making this ? and if you could direct me to a useful tutorial or tell me some of the code i would be very happy thanks !!!
|
|
|
|
|
No one will really give you such code, because it is readily available by using Google, or reading a book for beginnings. This is basic functions that are taught at a very early level, when you said a bit of experience I assume you where overstating, to say the least.
Please Google for introductory articles on C# and .Net, make use of MSDN (as the examples posted are extremely helpful to beginners and professionals alike) and become accustomed to researching first, trying second and then asking for help, but on specific areas where you get stuck. Not an entire project.
|
|
|
|
|
sorry, i did search , just didn't know what to look for , hence why i was asking for advice and support ... thanks anyway
|
|
|
|
|
Regardless, your initial post still asked for code, which is generally frowned upon around here. You will see people here willing to help you a great deal if your question revolves around a specific bug and you've sincerely tried to accomplish something, anything other then coming here first.
Good luck with your work.
|
|
|
|
|
Well obviously i am new around here and didn't know what to put so i generally asked for code , regradless or not of peoples feelings to the topic of asking for source code , the term code could of meant that you can give me an example or you can tell me what type of code is required for me to research to reach my goal, i am very sorry , i have learnt my lesson to not use the term "code" in general circumstances , thank you
|
|
|
|
|
weirdsteve wrote: just didn't know what to look for
Let me help[^]
only two letters away from being an asset
|
|
|
|
|
Thank you very much for your help Mark Nischalke ! at least there are some kind people on here who will just even quote or tell me what i am suppose to search Thank you ...
|
|
|
|
|