|
Look at the following code
<br />
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();<br />
...<br />
ExcelApp.Quit();<br />
The Excel object is not release after 'Quit()' method(Excel is still in the background process list).I think it is because there is still a reference of the object.
I a windows application,this will be solved when I close the application. But I'm using these code in an asp.net application.Every time they execute,the background process list grows.
Is there any way to relase the object or Is there any code to read excel files without using Microsoft Excel?
|
|
|
|
|
Set it to null first.
Ahsan Ullah
Senior Software Engineer
|
|
|
|
|
|
Thank you for your reply.But I stiil face a problem:
<br />
private void NullAndRelease(object runtimeObject)<br />
{<br />
if (runtimeObject == null)<br />
return;<br />
try<br />
{<br />
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(runtimeObject);<br />
}<br />
finally<br />
{ <br />
runtimeObject = null; <br />
}<br />
}<br />
private void button1_Click(object sender, EventArgs e)<br />
{<br />
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();<br />
object NullParam = System.Reflection.Missing.Value;<br />
Workbook Book = ExcelApp.Workbooks.Open(FileName, 0, true, NullParam, NullParam, NullParam,<br />
true, NullParam, NullParam, false, false, NullParam, false, true, NullParam);<br />
Worksheet Sheet = Book.Worksheets[1] as Worksheet;<br />
Range UsedRange = Sheet.UsedRange;<br />
<br />
Range Cell = null;<br />
int RowCount = UsedRange.Rows.Count;<br />
for (int i = 2; i <= RowCount; i++)<br />
{<br />
Cell = UsedRange.Cells[i, 1] as Microsoft.Office.Interop.Excel.Range;<br />
Cell = UsedRange.Cells[i, 2] as Microsoft.Office.Interop.Excel.Range;<br />
Cell = UsedRange.Cells[i, 3] as Microsoft.Office.Interop.Excel.Range;<br />
Cell = UsedRange.Cells[i, 4] as Microsoft.Office.Interop.Excel.Range;<br />
Cell = UsedRange.Cells[i, 5] as Microsoft.Office.Interop.Excel.Range;<br />
Cell = UsedRange.Cells[i, 6] as Microsoft.Office.Interop.Excel.Range;<br />
NullAndRelease(Cell); <br />
}<br />
NullAndRelease(Cell);<br />
NullAndRelease(UsedRange);<br />
NullAndRelease(Sheet);<br />
if (Book != null)<br />
Book.Close(false, NullParam, false);<br />
NullAndRelease(Book);<br />
ExcelApp.Quit();<br />
NullAndRelease(ExcelApp);<br />
GC.Collect();<br />
GC.WaitForPendingFinalizers(); <br />
}<br />
These code works well.But when I add the following code in the for loop:
<br />
Cell = UsedRange.Cells[i, 7] as Microsoft.Office.Interop.Excel.Range;<br />
The server does not shut down. It seems that I can only read not more than 6 cells.That's why?
|
|
|
|
|
Here is a code piece from Visio Code Librarian:
private void NullAndRelease(object runtimeObject)
{
try
{
if (runtimeObject != null)
{
int referenceCount = System.Runtime.InteropServices.
Marshal.ReleaseComObject(runtimeObject);
while (0 < referenceCount)
{
referenceCount = System.Runtime.InteropServices.
Marshal.ReleaseComObject(runtimeObject);
}
}
}
finally
{
runtimeObject = null;
}
}
I think you can also use this one for Excel too.
Regards,
Always keep the Murphy Rules in mind!
modified on Thursday, August 21, 2008 6:15 AM
|
|
|
|
|
There is no reason to call ReleaseComObject in a loop. Call FinalReleaseCOMObject instead, it sets the ref count directly to zero.
|
|
|
|
|
I am using visual studio 2003(FW 1.1).I filled a combobox using value member and display member from a datasource.when I run my project that combo box is hanging ,ie values are there in combobox but I cant select from that.(values are not populating).Comboboxes are working fine if I add Items using Item.Add() method.Any way to solve this problem as I need to get value member from that.Pls help me ....
with regards
thomas
|
|
|
|
|
Best guess is that your code to get the data from the source is hanging. Have you tried debugging to work out where it gets stuck ?
Christian Graus
No longer a Microsoft MVP, but still happy to answer your questions.
|
|
|
|
|
there is no problem while debugging.its a simple code , here it is ...
cmboobx.Datasource=Dataset.Tables[0];
cmboobx.Displaymember=string Display member ;
cmboobx.VauemEMBER=STRING VALUE MEMBER;
this is the code I used.But while running combobox is not hanging.we can select values using scroll in our mouse.But we cant click and select that value.
thank you for your reply friend.
|
|
|
|
|
I don't see how that's possible. You can scroll through the items, but can't select one ? What are you clicking, the items in a drop down list that appears ?
Of course, the code you posted is broken, it would not even compile. what does your code really look like ?
Christian Graus
No longer a Microsoft MVP, but still happy to answer your questions.
|
|
|
|
|
sorry ..friend ,
I cant copy and paste code from here.That code is just a sample.
Here is the format
combobox1.DataSource=dataset1.Tables[0];
combobox1.DisplayMember="Name of column to be displayed";
combobox1.ValueMember="Name of column to be added as value";
|
|
|
|
|
Hi all
I facing problem while connecting C# with SQL server 2000
Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Test;Data Source="source";Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID="source";Use Encryption for Data=False;Tag with column collation when possible=False
in the exception
1. Keyword not suported: 'Provider'
if i remove Provider=SQLOLEDB.1, then exeption is
2. Keyword not suported: 'Use Procedure for Prepare'
and same for Use Encryption for Data=False, Tag with column collation when possible=False.
Plz....... help me!
Thanx in advance.
|
|
|
|
|
Check this.[^]
I Love T-SQL
"Don't torture yourself,let the life to do it for you."
|
|
|
|
|
I'm creating a simple calculator.. so my calculator has only a textbox, it doesnt have any buttons so the user communicates with the calculator using the keyboard... so what i did is to enable the preview key down of the form and on the event of the key down, the computer is able to get the values entered from the keyboard. The problem with key down is that it only works if it has focus on the control or the form... but what i want is that even though the focus is not on the form or lets say the focus is on another application, how do i get my program to still get the keyboard values entered by the user?
|
|
|
|
|
|
yeah cause you know i'm doing like a computation while looking at the values at the other application and dont like doing the short cut keys using alt+tab or keep on clicking the calculator just to enter values. specially if its in full screen mode.
|
|
|
|
|
You have me imagining an applet that's always minimized and displays the calculation its caption.
|
|
|
|
|
Interesting, because I'm envisioning a keylogger.
|
|
|
|
|
I thought that at first, but it seems like a legit concept. I don't like flipping between a calculator and windows all the time.
|
|
|
|
|
There appears to be no way to use Threading.Interlocked methods to change the value of a boolean. There's Increment and Decrement for Ints, and Exchange<t> for reference types, but nothing for changing the value of a boolean in a thread-safe manner.
Is this not required? Are changes to booleans already threadsafe?
|
|
|
|
|
JoeRip wrote: Are changes to booleans already threadsafe?
Half the time, they are :P
Don't you just put a lock block over the code where you change it ?
Christian Graus
No longer a Microsoft MVP, but still happy to answer your questions.
|
|
|
|
|
You mean, have a lock object for every private field in my class? Or have a single lock object that I lock every time I change any private field in my class? The first seems excessive, and the second seems... anti-performant.
I thought the Interlocked methods WERE tiny little lock blocks, used in an atomic fashion....
Or am I missing something fundamental here?
|
|
|
|
|
Declare it as volatile. :P
|
|
|
|
|
I have to admit, I can't tell if either one of you is serious...
|
|
|
|
|
I didn't know there were interlock methods, I'd always just create an object in the same scope as the variable and do
lock(myLockingObject)
{
theBool = false;
}
Christian Graus
No longer a Microsoft MVP, but still happy to answer your questions.
|
|
|
|