|
There's no static linking in .NET. Assemblies reference one another, like dynamic linking. As I mentioned in my first reply - and be sure to read the documentation I linked for more information - the assemblies are copied locally for runtime purposes. Yes, if they are copied locally they are referenced during compilation, but if you don't copy them locally than the assemblies' source paths are used instead.
If the CLR cannot resolve the assemblies that are referenced by the currently executing assembly, an exception is thrown. This is not any different than native DLLs not being found in the current directory or in any of the directories in your PATH environment variable, only the CLR does not use the same rules. I also discussed that in my first reply and linked the documentation for "How the Runtime Locations Assemblies", which you should really read to gain a better understand of just what assembly references mean to your libraries and/or applications. After all, if you're new to .NET programming you won't learn if you don't read.
If you use project references using the Projects tab in the Add References dialog, you need not worry about keeping the assemblies up to date, though I recommend that you never use automatic versioning by leaving the asterisk (*) in the AssemblyVersionAttribute . You can loose control quickly, and in .NET the versions are important unlike most native DLLs.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hi all,
I want to lock a particular method of the object, so it allows only
single access of the method at one time. But by using lock(this) to the
method will cause the whole object to be locked, so other methods can't be used. What can I do to lock the particular method only ?
Thanks
|
|
|
|
|
Create a new meber variable (e.g. object lockVarForMethodA) and reference a unique object of your choice (e.g. new object()).
Than use lock(lockVarForMethodA) in your method.
|
|
|
|
|
ting668 wrote:
But by using lock(this) to the
method will cause the whole object to be locked,
That's not true. The object is not locked in such a way that nothing else can use it. Unless a method also calls lock(this) or otherwise locks whatever object this refers to, the method can run in a separate thread (not in the current thread, of course, since you're already executing a method).
What Robert said is good, though. If you want to synchronize each method individually then create separate static instances of objects to lock against. These must be static so that they are the same object for all instances of your object in any thread within a single AppDomain.
If you're going to synchronize each method individually and don't need instance data, I recommend you make the methods static. The CLR will synchronize static methods allowing only one thread of execution to enter at a time within a given AppDomain. This would be the same as locking against separate objects for each method yourself and makes for a better design and uses less memory (additional objects are not necessary).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
HI
"Unless a method also calls lock(this) or otherwise locks whatever object this refers to, the method can run in a separate thread (not in the current thread, of course, since you're already executing a method)."
That means if a class like this below:
Class class1<br />
{<br />
public void MethodA(){<br />
lock(this)<br />
{<br />
...<br />
}<br />
}<br />
<br />
public void MethodB()<br />
{<br />
...<br />
}<br />
}
Then using the same reference of a instance of class1, after calling MethodA and MethodA not yet finished executing within the lock, MethodB still can be called unless lock(this) is also used in MethodB?
Also is that the static method will be automatically synchronized and special keyword and code (e.g lock) is not needed to be implemented?
Is lock useless for single thread programming?
Thanks
|
|
|
|
|
ting668 wrote:
Then using the same reference of a instance of class1, after calling MethodA and MethodA not yet finished executing within the lock, MethodB still can be called unless lock(this) is also used in MethodB?
That is correct. Just try it. The majority of code snippets I post here in this forum I simply type in a simple text editor and compile on the command line, then run. Simple.
ting668 wrote:
Also is that the static method will be automatically synchronized and special keyword and code (e.g lock) is not needed to be implemented?
A single static method is synchronized by the CLR. If you want to synchronize different static methods you will need to use lock or some equivalent.
ting668 wrote:
Is lock useless for single thread programming?
Yes. Within a single thread you can only execute one statement at a time. With that in mind there's no way you could execute the same method (or act on the same context, like getting or setting a serial number) at the same time.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hi Health,
Class class1
{
public void MethodA()
{
lock (this)
{
MethodB();
}
}
public void MethodB()
{
lock (this)
{
...
}
}
}
From the code above, statements in locking block of MethodB still successfully run which is branched within the lock from MethodA. Why it is not a counter example of the locking theroy of the point1 :
Then using the same reference of a instance of class1, after calling MethodA and MethodA not yet finished executing within the lock, MethodB still can be called unless lock(this) is also used in MethodB?
of the last reply?
Thanks
|
|
|
|
|
Because it's executing within the same thread - not a different thread. Synchronization is not a replacement for state. You really should read Threading[^] in the .NET Framework SDK, as well as about the Monitor.Enter[^] method that the lock keyword (SyncLock in VB.NET) compiles to.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Sorry for a lengthy post, but I am very new to events and delegates and want you guys opinion on this.
Is this kind of encapsulation ideal? I have encapsulated the entire event functionality (the related event and delegate) inside the publisher class itself, and I think this is making it a lot cleaner that the MSDN example. (no offense)
using System;
namespace EventsAndDelegates
{
#region The Suscriber
public class WebClient
{
// Note the mathcing signature with the delegate
public void ShowStatus(ServerClass ContentServer, ServerClass.ExecutionCompleteEventArgs ServerEventArgs)
{
Console.WriteLine("Execution is finished. Result = " + ServerEventArgs.ExecutionResult);
}
}
#endregion
#region The Publisher
public class ServerClass
{
// Delegate for event handling
public delegate void ExecutionCompleteHandlerDelegate(ServerClass ContentServer, ExecutionCompleteEventArgs ContentServedEventArgs);
// Event of type the delegate define above
public static event ExecutionCompleteHandlerDelegate EventExecutionComplete;
// Event argument
public class ExecutionCompleteEventArgs : System.EventArgs
{
public int ExecutionResult;
// Constructor accepts Returncode
public ExecutionCompleteEventArgs(int ReturnCode)
{
ExecutionResult = ReturnCode;
}
}
// This class will fire this event when it occurs
public void OnEventExecutionComplete(ExecutionCompleteEventArgs ServerEventArgs)
{
// (Only) if somebody has subscribed to the event, notify them
if(EventExecutionComplete!=null)
EventExecutionComplete(this, ServerEventArgs);
}
// The method which will cause the event
public void ServerBigTask()
{
// Server does something big and on finish raises the event
OnEventExecutionComplete(new ExecutionCompleteEventArgs(10));
}
}
#endregion
#region The Framework
class AppClass
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Main started...");
ServerClass ContentServer = new ServerClass();
WebClient IEExplorer = new WebClient();
ServerClass.EventExecutionComplete+= new ServerClass.ExecutionCompleteHandlerDelegate(IEExplorer.ShowStatus);
ContentServer.ServerBigTask();
Console.WriteLine("Main ended.");
Console.ReadLine();
}
}
#endregion
}userid=1296023">^</a>]
|
|
|
|
|
It's not a bad design, but keep in mind that encapsulation != nested classes. Nesting classes (or any other types, including delegates) is typically when you don't want to share a type or even make it non-public. In the extremely dynamic world of application development, that's a pretty poor assumption.
You should design your object model to be extensible and assume little when possible. To that your delegate and related EventArgs derivative will only be used for this class is probably not a safe assumption. Besides - do you really want your clients to have to type all that every time they want to use it? Making your object model easy to use is also important in many libraries and applications.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Got it... Thanks a lot...
Salil Khedkar [^]
|
|
|
|
|
Hi
I have done a c# program , such that my program reads any schema file and inserts values for that fields in a new xml file. ie, for exx : for the datatype string (john mathew1, john mathew2,..) for decimal(900.90, 900.89/..) like so on for about 200 values for every datatypes.
Now, I require you to build a program further. Imagine that you have an xml file which is just a list of values for a single field. Then within my program if I come across that particualr field , then I have to load the values from that xml file into the output file.
Here is my coding which i had done previously. please give me an idea how to give the coding.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml;
using System.Xml.Schema;
namespace testdatagenerator
{
///
/// Summary description for Form1.
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtSchema;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox txtXml;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Data.DataSet dataSet1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.txtSchema = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.txtXml = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.dataSet1 = new System.Data.DataSet();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit();
this.SuspendLayout();
//
// txtSchema
//
this.txtSchema.Location = new System.Drawing.Point(232, 24);
this.txtSchema.Name = "txtSchema";
this.txtSchema.Size = new System.Drawing.Size(368, 20);
this.txtSchema.TabIndex = 0;
this.txtSchema.Text = "";
//
// button1
//
this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.button1.Location = new System.Drawing.Point(616, 24);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 23);
this.button1.TabIndex = 1;
this.button1.Text = "Browse";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// txtXml
//
this.txtXml.Location = new System.Drawing.Point(232, 64);
this.txtXml.Name = "txtXml";
this.txtXml.Size = new System.Drawing.Size(368, 20);
this.txtXml.TabIndex = 2;
this.txtXml.Text = "";
//
// button2
//
this.button2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.button2.Location = new System.Drawing.Point(616, 64);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(64, 23);
this.button2.TabIndex = 3;
this.button2.Text = "Load";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.button3.Location = new System.Drawing.Point(696, 64);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(64, 23);
this.button3.TabIndex = 4;
this.button3.Text = "Save";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.DataSource = this.dataSet1;
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(0, 120);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(776, 320);
this.dataGrid1.TabIndex = 5;
//
// dataSet1
//
this.dataSet1.DataSetName = "NewDataSet";
this.dataSet1.Locale = new System.Globalization.CultureInfo("en-US");
//
// label1
//
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label1.Location = new System.Drawing.Point(32, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(184, 23);
this.label1.TabIndex = 6;
this.label1.Text = "Enter the Xml Schema";
//
// label2
//
this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label2.Location = new System.Drawing.Point(32, 64);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(192, 23);
this.label2.TabIndex = 7;
this.label2.Text = "Enter the Xml file";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(808, 454);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.txtXml);
this.Controls.Add(this.button1);
this.Controls.Add(this.txtSchema);
this.Name = "Form1";
this.Text = "TestDatagenerator";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit();
this.ResumeLayout(false);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "select schema file" ;
fdlg.InitialDirectory = @"c:\CDEV\testdatagenerator" ;
fdlg.Filter = "schema files(*.xsd) |*.xsd| all files(*.*) | *.*";
fdlg.FilterIndex = 2 ;
fdlg.RestoreDirectory = true ;
if(fdlg.ShowDialog() == DialogResult.OK)
{
txtSchema.Text = fdlg.FileName ;
}
}
private void button2_Click(object sender, System.EventArgs e)
{
dataSet1.ReadXmlSchema(txtSchema.Text);
foreach (DataTable dTbl in dataSet1.Tables)
{
for(int j = 0; j <= 200; j++)
{
object[] oValues = new object[dTbl.Columns.Count];
int i = 0 ;
foreach (DataColumn dColmn in dTbl.Columns)
{
switch(dColmn.DataType.ToString())
{
case "System.String":
oValues[i] = (string) "Castle Hampers" + j;
break;
case "System.Int32":
oValues[i] = (int) 100 + j;
break;
case "System.DateTime":
oValues[i] = new DateTime(2004,01,30).AddDays(j * 1);
break;
case "System.Decimal":
oValues[i] = new Decimal(10000900.99) + j;
break;
case "System.Int16":
oValues[i] = (short) 32767 - j;
break;
case "System.Int64":
oValues[i] = (long) 400 - j;
break;
case "System.Double":
oValues[i] = (double) 888888 - j;
break;
case "System.Single":
oValues[i] = (float) 4.5f + j;
break;
}
i = i+1;
}
dTbl.Rows.Add(oValues);
}
}
}
private void button3_Click(object sender, System.EventArgs e)
{
dataSet1.WriteXml(txtXml.Text,XmlWriteMode.WriteSchema );
MessageBox.Show("Saved");
}
}
}
john santhosh
|
|
|
|
|
Hi Folks!
I was wondering if I could know about any mechanism for selection of language in prgramming e.g. C#. By language selection I mean that if we have more than one languages installed on WindowsXP(Contorl Panel -> regional n .. ***), a language bar appears there to provide a choice of language selection.
Any one knowing if it possible....thanku inadvance;)
+Solo
|
|
|
|
|
The short answer is that you typically shouldn't. Giving users a consistent UI for control tasks is important for user understand. In some cases doing this yourself can lead to problems if you don't do everything required. For example, setting the UI language (MUI language) requires the user session to end via a reboot or having the user simply log off and back on.
On MUI-supported Windows desktop platforms you can't set the UI language (though you can set the locale). You can on Windows CE-based platforms like Pocket PC, however, by P/Invoking SetUserDefaultUILanguage .
Depending on what you're doing, though, you don't need to do this. If you only want to display a different language and/or locale for your application via the built-in localization features, simply set Culture.CurrentUICulture and/or Culture.CurrentCulture to change the language and/or locale, respectively, for the current thread. Do this before displaying any UI, otherwise you'll need to re-initialize (not necessary re-instantiate) any controls.
Read Developing World-Ready Applications[^] in the .NET Framework SDK for more information.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
can someone give me some clues as to how to do this?
|
|
|
|
|
I would recommend searching the forum first by click "Search comments" above. This topic has been covered[^] many times in this forum in the past. In fact, I just answered a similar question earlier today about scrolling to a particular line in the RichTextBox , so you could simply browse for that (probably on the next page of this forum) to see how.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
how would i use a font such as terminal when it is not open type or true type?
|
|
|
|
|
cmd.CommandText="UPDATE CompTav SET "+
@"TeorBohen='"+RbTeorBohen.Text +"' "+
@"TechName='"+tbTech.Text +"' "+
@"TipolDate='"+dt1.ToShortDateString()+"' "+
@"TipolStartHour='"+dt2.ToShortTimeString()+"' "+
@"Statos='"+CbStatos.Text +", "+
"where CartisNum=" +lbCartisNum.Text
|
|
|
|
|
Where do I begin? First of all, why are you using the literal string operator @? The way you're using it doesn't add anything to your statements because they only matter at compile time and will not automatically escape what you have in your text boxes, etc. They simply mean that when the source is compiled the string at that point will not expand escape sequences and can span lines until terminating with another double-quote.
Second, NEVER use string concatentation to build SQL queries. All someone has to do is insert the following into one of your text boxes and you're 0wned:
2 or 1=1; delete from CompTax -- Now you're CompTav table is gone. Worse can happen, and don't think people won't figure it out because disassemblers and decompilers are readily available and obfuscation helps little for seasoned pros. ildasm.exe - a disassembler - even comes with the .NET Framework SDK.
Also use parameterized queries:
cmd.CommandText = @"UPDATE CompTax SET
TeorBohen = @TeorBohen,
TechName = @TechName,
TipolDate = @TipolDate,
TipolStartHouse = @TipolStartHour,
Statos = @Statos
WHERE CartisNum = @CartisNum";
cmd.Parameters.Add("@TeorBohen", SqlDbType.NVarChar, 40).Value =RbTeorBohen.Text;
cmd.Parameters.Add("@TechName", SqlDbType.NVarChar, 128).Value = tbTech.Text;
cmd.Parameters.Add("@TipolDate", SqlDbType.DateTime).Value = dt1;
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
finally
{
conn.Close();
} Notice how we even assign the appropriate types to the parameters? Not only does using parameters strengthen your security (this eliminates most - if not all - SQL injection attacks) but makes it easier to execute commands. You don't have to worry about encoding your commands anymore like you do with ASP, PHP, etc. This makes batch updates a breeze, too: add the parameters once, then loop through your values setting each parameter, executing the command, then repeating.
EDIT: The above parameter syntax is for SQL Server. Oracle uses ":name" and the OLE DB provider uses only "?" (unnamed; you have to add the OleDbParameter s in order of how "?"'s are declared in the SQL expression). Read about the variable IDbCommand implementations like SqlCommand , OleDbCommand , etc., and their respective Parameters properties, for more information.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
the sample didint work
i am on a OleDB database
but thanks anywey .....
if u can help i will be greatful...
|
|
|
|
|
I said you need to read the documentation, and that the OLE DB provider (as in the OleDbCommand ) doesn't support named parameters. You must use "?" in place of the values (without any quotes or other special characters) and then add the parameters in the order they should be. If you read the documentation[^] for the OleDbCommand.Parameters is clearly explains and details this in an example.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
i try that :
IDbCommand cmd = this.oleDbConnection2.CreateCommand();
cmd.CommandText = "UPDATE CompTav SET TeorBohen = ?, WHERE (CartisNum = ?)";
OleDbParameter parm = new OleDbParameter();
parm.ParameterName="@TeorBohen";
parm.Value = RbTeorBohen.Text;
cmd.Parameters.Add(parm);
OleDbParameter parm1 = new OleDbParameter();
parm1.ParameterName="@CartisNum";
parm1.Value = lbCartisNum.Text;
cmd.Parameters.Add(parm1);
oleDbConnection2.Open();
cmd.ExecuteNonQuery();
oleDbConnection2.Close();
but it still dont work ...
|
|
|
|
|
Because you didn't set the type for parm1 , most likely. You need to assign the values using the appropriate type, which means you need to convert lbCartisNum.Text to an integer value.
It's hard to tell, though. It does work, though: thousands of people use it every day. You didn't post the exception being thrown, so how can I know exactly what's wrong? You need to be more specific about your problems, like posting the exception message and type (like System.Data.OleDb.OleDbException and that it returned "couldn't cast to the appropriate type", or something like that).
The following may work - but again this is without knowing what your actually problem is since you didn't tell me:
cmd.Parameters.Add("@TeorBohen", OleDbType.VarChar, 80).Value = RbTeorBohen.Text;
cmd.Parameters.Add("@CartisNum", OleDbType.Integer).Value = int.Parse(lbCartisNum.Text); Since you assigned text to parm1.Value then the value would've been quoted in the resultant command before being executed, meaning that it wasn't representing a number but instead a string containing the number.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
i all send u the file on Mail...
|
|
|
|
|
What good would that do? I don't have your database, nor do I want it over email.
Read the documentation for the OleDbCommand.Parameters property and the OleDbParameter class for information and examples. So long as your SQL expression is right in terms of what your database is expecting, what I told you and what the documentation covers solves your problem.
If you're getting an exception that seems to indicate something else, then reply with the exception type and message - the same thing I tell everyone when they simply say something like "it doesn't work". The other regular responders would do the same. How could we know the problem unless it's painfully obvious? You need to tell us the exact error or helping you is difficult.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|