|
Thanks N a v a n e e t h,
I am in a multi-threaded environment, some thread will dispose the object, when some other threads executing method on the object, it will have such exception.
Any advice to my situation to resolve this exception?
regards,
George
|
|
|
|
|
Means your code is not thread safe. Here is my suggestions
1 - Create new instance of the disposable object for each thread. So it can dispose when it finishes.
2 - If you are sharing the same disposable instance, use some kind of synchronization mechanism to ensure no threads are disposing it when another one uses.
|
|
|
|
|
Good suggestion, thanks N a v a n e e t h!
regards,
George
|
|
|
|
|
I have checked that the exception is from EndGetContext method of HttpListener class, when will it throw ObjectDisposedException?
regards,
George
|
|
|
|
|
George_George wrote: when will it throw ObjectDisposedException?
When it is disposed. Hard to tell more without seeing your code.
|
|
|
|
|
|
I don't think that it is a good method. When you call End() and catch exception, it's not sure that asynchronous method has finished doing it's job. When an exception rises, it stops processing and throw the application when End() is called. So I suggest you to look through the suggestions which I have in the previous post.
|
|
|
|
|
Thanks N a v a n e e t h,
I agree with your solution. I am more interested in why there is ObjectDisposedException when I call End method. Does it mean some other parties (maybe some backend IO threads by OS or CLR) disposed the HttpListener object instance before I call End method?
regards,
George
|
|
|
|
|
Since you are in a mutithreaded environment and not using any synchronization techniques, there are chances for another thread to dispose the shared instance. This is causing exception. Since you are using asynchronous method pattern, exception won't be thrown immediately, asynchronous methods stops the execution and keep the exception to another variable for future use. This will be thrown when you call End() method.
|
|
|
|
|
Thanks N a v a n e e t h,
I totally agree with your analysis. Is it possible to add some code to monitor which process calls the Dispose method of HttpListener? I have checked my code is not invoking Dispose method explicitly.
My suspecisions,
1. Some code I used will be expanded by C# compiler, which contains Dispose method invocation -- even if I do not call it explicitly.
2. Some CLR internal threads call Dispose method, since the situation happens when the network environment is roaming, and clinet frequently disconnects from server (I am writing an Http server).
Any advice to make further analysis?
regards,
George
|
|
|
|
|
George_George wrote: Is it possible to add some code to monitor which process calls the Dispose method of HttpListener?
Don't know exactly - But if HTTPListener class allows inheriting, you can override Dispose(bool) method and check it is being called.
George_George wrote: Some CLR internal threads call Dispose method, since the situation happens when the network environment is roaming, and clinet frequently disconnects from server (I am writing an Http server).
I don't have any prior experience on using this class. So I don't know what's going wrong here. But CLR calls the Dispose method only when garbage collection is happening. I guess even the client got disconnected, your HTTPListener instance will be still active until garbage collection unless you release it by calling Dispose() explicitly.
|
|
|
|
|
Thanks N a v a n e e t h,
I have checked HttpListerner class is sealed and we can not inherits it.
public sealed class HttpListener : IDisposable
I have some further ideas, I think when we complete asynchronous method call by using End method, all exceptions during the method call will be raised to the End method. Maybe it is some other component is Disposed. I have this idea is because I agree with you that the Listener object should live not regarding of the client connection termination.
Any ideas to find out which object is Disposed? I catch ObjectDisposedException in End method.
regards,
George
|
|
|
|
|
general rule: don't try to use Disposed Objects. If there is the chance this might happen your code is wrong.
simple huh?
|
|
|
|
|
|
I generally use the Disposing pattern which has sort of evolved from the IDisposable interface. The pattern when implemented looks like this in code:
public class MyObject : IDisposable { ~MyObject() { Dispose(false); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } private bool _disposed; // indicates if Dispose has been called private void Dispose(bool disposing) { if (!_disposed) { if (disposing) { // TODO: cleanup managed resources in MyObject } // TODO: cleanup unmanaged resources in MyObject _disposed = true; } } }
Once you have this pattern in place your methods can use something like this:
public void SomeOperation() { if (_disposed) return; // TODO: do something }
Of course you're going to want to do something more meaningful then just return -- like throw your own exception or return an error code. It just depends on what you're doing that might call an object that has been disposed.
|
|
|
|
|
Thanks,
1.
But we have to check _disposed before we invoke any methods for an object implementing IDospose. It will make code clutter and not easy to read?
2.
I have checked that the exception is from EndGetContext method of HttpListener class, when will it throw ObjectDisposedException?
regards,
George
|
|
|
|
|
hi
i have two schemas in an Oracle database schema1 and schema2
and they have related tables eg tbl1 in schema1 is related to table2 in schema2
In my C# program i wanna get related data from the two tables display then in a Dataview
how do i do it?
regards paula
|
|
|
|
|
hi
i use wmplayer in my form but when i run with my application got this error
ActiveX control '6bf52a52-394a-11d3-b153-00c04f79faa6' cannot be instantiated because the current thread is not in a single-threaded apartment.
but when i use it in single form i don't have any error.
please tell me how i can use windows media pyer in my application
TNKS
|
|
|
|
|
Windows mediaplayer uses COM, which is single threaded. So as the error message says, you can't use it in a multi threaded application. However it should work fine if you are running it from the same thread in which your form controls are created.
|
|
|
|
|
I m generating a tree through following items
6,2,8,1,4,3,7,9
but having problem implementing the Depth First and Breadth First Search in this tree
How can i implement the Depth First and Breadth First Search.
|
|
|
|
|
sir i want to insert data in two tables using transaction. thee is error occurs at run time . i am giving u a my code please correct it as soon as possible:
con = new OleDbConnection("provider=Microsoft.Jet.Oledb.4.0;data source=E:\\db1.mdb");
com1 = new OleDbCommand("insert into info(user_id,password,name,role,email,contact,address,designation,ques,ans) values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox4.Text + "','" + comboBox1.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "')");
com2 = new OleDbCommand("insert into login(user_id,password,role) values('" + textBox1.Text + "','" + textBox2.Text + "','" + comboBox1.Text + "')");
com1.Connection = con;
com2.Connection = con;
con.Open();
tr = con.BeginTransaction();
com1.Transaction = tr;
com2.Transaction = tr;
com1.ExecuteNonQuery();
com2.ExecuteNonQuery();
tr.Commit();
MessageBox.Show("inserted");
Signature preview salil_k_singh
|
|
|
|
|
Could you tell what the error is? I'm sure it would help.
My current favourite word is: Bacon!
-SK Genius
Game Programming articles start - here[ ^]-
|
|
|
|
|
Post the error and what you are trying and then we will be able to help you.
You should google "SQL Injection" too as your code is vulnerable to attack, you should use parameters in your query.
|
|
|
|
|
I Have an assembly that has been signed with a strong name key, does anybody know if it is possible to get access to the public key in the assymbly from code within the assyembly.
thanks in advance
william T
|
|
|
|
|
The GetPublicKey method of the AssemblyName class.
static void Main(string[] args) { // get the public key AssemblyName asmName = Assembly.GetExecutingAssembly().GetName(); byte[] publicKey = asmName.GetPublicKey(); // convert the public key to a string StringBuilder builder = new StringBuilder(); Array.ForEach(publicKey, keyByte => builder.Append(keyByte)); // output the results Console.WriteLine("Public Key:"); Console.WriteLine(builder.ToString()); }
|
|
|
|