私は顧客情報を持つプログラムを維持しています。これは、それぞれがデータベースからの関連情報を示す多くの形式で構成されています。このエラーは、オープンは、顧客の検索フォーム ウィンドウハンドルが作成されていないため、BeginInvokeが失敗します
- を行った後、単一の形態であり、 draganddropを介して彼にファイル。
- 最後の2つのフォームを閉じ、ランダムな顧客Bを選択して同じ操作を行います。
- 最後の2つのフォームを閉じ、顧客Aを選択して新しいファイルを追加します。エラー!!! 'System.InvalidOperationException' 種類の未処理の例外がのSystem.Windows.Forms.dll
で発生しました追加情報」
private void FireFileCountChanged() { if (FileCountChanged != null) BeginInvoke(new DeferEvent(FireFileCountChangedDeferred), 2); // FAILS
:起動またはBeginInvokeメソッド
ここでは、失敗したコードです。ウィンドウハンドルが作成されるまで、コントロールで呼び出すことはできません。
私は、次を追加してみました:
private void FireFileCountChanged() {
if (FileCountChanged != null && this.Handle != null) // CHANGED AND FAILS.
BeginInvoke(new DeferEvent(FireFileCountChangedDeferred), 2);
}
しかしthis.handleができます:
'をthis.Handle' タイプ 'System.ObjectDisposedException' の例外を投げ、 は「配置されたにアクセスできません。オブジェクト。\ r \ nオブジェクト名: 'AttachmentsControl'。 "
その後、メソッドの最初の行として10秒のタイムアウトを追加しましたが、ハンドルはまだ作成されません。窓の1つが閉じられたときに何とか処理されたハンドルはありますか?これについて何ができるのでしょうか?どんな助けもありがとうございます。私はこだわっている。
private void FireFileCountChangedDeferred(int repostCount) {
if (FileCountChanged != null) {
if (repostCount > 0) {
//black magic is somehow involved in getting this event to fire *after* the filewatcher reports the change.
System.Threading.Thread.Sleep(10);
BeginInvoke(new DeferEvent(FireFileCountChangedDeferred), repostCount - 1);
} else
FileCountChanged(this, null);
}
}
private void CopyFiles(string[] files, bool reload) {
if (CreatePath()) {
foreach (string src in files) {
try {
string dest = MakeSafeFilename(src);
File.Copy(src, dest);
FireFileCountChanged();
} catch (Exception ex) {
//Util.Print("Copy ex: {0}", ex.Message);
ErrMsg("Error while copying:{1}{0}", ex.Message, environment.NewLine);
}
}
}
}
private void Lstv_DragDrop(object sender, DragEventArgs ea) {
if (m_CanAdd) {
string[] files = GetDraggedFiles(ea);
if (files != null)
CopyFiles(files, true);
else if (OutlookDataObject.HoldsOutlookData(ea) && CreatePath()) {
try {
OutlookDataObject.CopyDroppedFiles(ea, m_Path, OutlookFilenameCallback);
} catch (Exception ex) {
//Util.Print("Copy ex: {0}", ex.Message);
ErrMsg("Error copying from Outlook:{1}{0}", ex.Message, Environment.NewLine);
}
}
}
}
ソリューション
private void FireFileCountChanged() {
while (!this.IsHandleCreated) // added
System.Threading.Thread.Sleep(100); //added
if (FileCountChanged != null)
BeginInvoke(new DeferEvent(FireFileCountChangedDeferred), 2);
それを解決しました!私は解決策を質問の最後に追加しました。どうもありがとうございました。 –
私は、あなたがハンドルが作成されるのを待つのに 'Thread.Sleep'を使うべきではないことを静かに示唆しました。 HandleCreatedイベントに基づいたソリューションはより洗練されたものになり、スレッドを永遠に縛ることは必要ありません。 –
私はそれを行います。しかし、FireFileCountChangedDeferredは無視され、ファイルはリストに追加されませんか? –