質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Q&A

解決済

2回答

11337閲覧

「DisconnectedContextが検出されました。」のエラー解消法

chintao1224

総合スコア155

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

0グッド

2クリップ

投稿2018/09/25 00:36

c#でexcelを生成するプログラムを作っています。
サイトを参考に以下のようなプログラムを書きました。
createexcelを1回実行するだけならエラーは出ないのですが、400ファイルとか連続でcreateexcelを呼び出すと「DisconnectedContextが検出されました。」とエラーが出ます。
catchできません。
エラーの出る箇所はフォーカスが当たるので、finallyに入ってからだと思います。

http://www.port135.com/2012/08/18/resolution-for-disconnectedcontext-was-detected-error-when-releasing-excel-object-in-c/
http://hiro-syumi.ldblog.jp/archives/36511362.html

上記を試しましたが、エラーは解消できませんでした。
VB.NetであればDoEventsで解消できるとか書いてありましたが、C#でいうとApplication.DoEventsでエラーが多いと言われているので適用していません。

エラーを解消する方法はないのでしょうか?

C#

1 private void CreateExcel(string filename) 2 { 3 // Excel操作用オブジェクト 4 Microsoft.Office.Interop.Excel.Application xlApp = null; 5 Microsoft.Office.Interop.Excel.Workbooks xlBooks = null; 6 Microsoft.Office.Interop.Excel.Workbook xlBook = null; 7 Microsoft.Office.Interop.Excel.Sheets xlSheets = null; 8 Microsoft.Office.Interop.Excel.Worksheet xlSheet = null; 9 10 try 11 { 12 // Excelアプリケーション生成 13 xlApp = new Microsoft.Office.Interop.Excel.Application(); 14 15 // ◆新規のExcelブックを開く◆ 16 // Addメソッド 17 xlBooks = xlApp.Workbooks; 18 xlBook = xlBooks.Add(); 19 20 // シートを選択する 21 xlSheets = xlBook.Worksheets; 22 xlSheet = xlSheets[1] as Microsoft.Office.Interop.Excel.Worksheet; // 1シート目を操作対象に設定する 23 24 // 表示 25 xlApp.Visible = false; 26 // 警告無視設定 27 xlApp.DisplayAlerts = false; 28 29 // 値を「A1」に代入 30 xlSheet.Range["A1"].Value = "文字列を入力"; 31 32 // 保存 33 xlBook.SaveAs(filename, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 34 Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, 35 Type.Missing, Type.Missing, Type.Missing, Type.Missing); 36 37 // 閉じる 38 xlBook.Close(Type.Missing, Type.Missing, Type.Missing); 39 40 } 41 catch (Exception ex) 42 { 43 string s = ex.Message; 44 } 45 finally 46 { 47 if (null != xlApp) 48 { 49 // アプリケーションの終了 50 xlApp.Quit(); 51 52 // アプリケーションのオブジェクトの解放 53 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheet); 54 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlSheets); 55 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook); 56 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBooks); 57 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp); 58 59 xlSheet = null; 60 xlSheets = null; 61 xlBook = null; 62 xlBooks = null; 63 xlApp = null; 64 } 65 } 66 }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

Solved: “DisconnectedContext was detected” error when releasing Excel object in C#

  1. Make sure you release all objects accurately. Study Runtime Callable Wrapper and ReleaseComObject.

  2. Try to run your application in “release mode” instead of “debug mode“. Sometime this error doesn’t show up in “release mode”. So, you may want not to do anything about it.

  3. If the problem is still going on, publish your application (Project > Properties > Publish). Do you have the same problem? If you don’t, it doesn’t mean you have resolved it. You have just ignored it. If you want to resolve it forever, go back to step 1 and try hard.

  4. RelaseComObject で解放してください。

  5. Release モードでビルドしてください。

  6. 発行してください。

詳しくはリンク先および更にそのリンク先を読んでください。

投稿2018/09/25 01:45

Zuishin

総合スコア28656

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

chintao1224

2018/09/25 03:36

ここも試して何故か解りませんが、駄目でした。ReleaseComObjectをFinalReleaseComObjectに変えてみても駄目でした。解消法はないのでしょうか?
Zuishin

2018/09/25 10:46

400 回 Excel を起動させるのがやりすぎなのかもしれません。 一回一回起動するのではなく、一度取得した COM を再利用するようにしてみてください。
guest

0

Zuishinさんの回答の記事のリンク先と類似する内容ではありますが、日本語情報として。

Office オートメーションで割り当てたオブジェクトを解放する – Part1 – Japan Office Developer Support Blog

リンク先にもあるようにxlApp.Quit()の前に、解放出来るものを全て解放、nullを設定した上で以下を実行。
xlApp.Quit()実行後にも解放&GC.Collect()~の実行をしてみてはどうでしょうか?

C#

1GC.Collect(); 2GC.WaitForPendingFinalizers(); 3GC.Collect();

投稿2018/09/25 11:42

imihito

総合スコア2166

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問