【问题标题】:OpenFileDialog issue with release resource发布资源的 OpenFileDialog 问题
【发布时间】:2016-03-22 11:57:40
【问题描述】:

我在 win 形式的 OpenFileDialog 中遇到了这个问题..

private void btnAllegato_Click(object sender, EventArgs e)
    {

        try
        {

            using (OpenFileDialog openFileDialog1 = new OpenFileDialog())
            {
                string path = string.Empty;

                openFileDialog1.Title = "Seleziona richiestaIT (PDF)..";
                openFileDialog1.Filter = ("PDF (.pdf)|*.pdf");
                openFileDialog1.FilterIndex = 1;
                openFileDialog1.FileName = "";


                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {

                    //salva l'intero path
                    path = openFileDialog1.FileName;

                    //nome file + estensione
                    string temp = openFileDialog1.SafeFileName;

                    //elimina l'estensione del file con IgnoreCase -> case Unsensitive
                    temp = Regex.Replace(temp, ".pdf", " ", RegexOptions.IgnoreCase);

                    //datatime + replace
                    string timenow = System.DateTime.Now.ToString();

                    //replace data da gg//mm/aaaa  ss:mm:hh  ----->  ad   gg-mm-aaaa_ss-mm-hh  
                    timenow = timenow.Replace(":", "-").Replace("/", "-");//.Replace(" ", " ");     

                    //effettua una copia dal path origine alla cartella nel NAS
                    _url = @"\\192.168.5.7\dati\SGI\GESTIONE IT\RichiesteIT\" + temp + timenow + ".pdf";

                    this.Cursor = Cursors.WaitCursor;

                    System.IO.File.Copy(path, _url);


                    this.Cursor = Cursors.Default;
                }


            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

第一次运行...一切正常... 但是第二次点击 btn ...进程进入循环.. OpenFileDialog 是打开的,但完全是白色的...

我认为这是处置资源的问题..但我不知道如何解决它。

... ... @EDIT

经过几次尝试......我意识到问题出在我点击按钮后Inserisci>>。 第一次运行良好,但是当我在 btnInserisci 之后第二次点击 ... btnAllegato 时,我有循环过程。

代码 btnInserisci:

        private void btnInserisci_Click(object sender, EventArgs e)
    {
        try
        {
            if ((_IDRichiedente != -1) && (_data != string.Empty) && (_url != string.Empty))
            {
                //messageBox
                MessageBox.Show(_url);
                QueryAssist qa = new QueryAssist();
                string query = "INSERT INTO RICHIESTA_IT(ID_Risorsa, descrizione_richiesta, modulo_pdf, data_richiesta) VALUES('" + _IDRichiedente + "', '" + txtBreveDescrizione.Text + "', '" + _url + "', '" + _data + "');";
                MessageBox.Show(query);

                qa.runQuery(query);
                // qa.runQuery("INSERT INTO RICHIESTA_IT (ID_Risorsa, data_richiesta) VALUES ('" + _IDRichiedente + "','" + _data + "');");
            }
            else
            {
                MessageBox.Show("Selezionare il richiedente,data o allegato!");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

其中private int _IDRichiedente = -1; private string _data = String.Empty; private string _url = string.Empty; 是类的字段。

QueryAssist 是我创建连接、运行查询和断开连接的个人课程。 代码:

    class QueryAssist
{
    System.Data.OleDb.OleDbConnection _OleDBconnection;

    public QueryAssist()
    {
        this._OleDBconnection = null;
    }

    //riferimento di connessione al db
    private bool connectionDB()
    {
        string connection = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=\"\\\\192.168.5.7\\dati\\Scambio\\Sviluppo\\Impostazioni temporanea db Censimento\\CensimentoIT.accdb\"";
        try
        {
            _OleDBconnection = new System.Data.OleDb.OleDbConnection(connection);
            _OleDBconnection.Open();
            return true;

        }
        catch(Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
            return false;
        }
    }

    private void disconnectDB()
    {
        try
        {
            _OleDBconnection.Close();
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);

        }
    }

    public System.Data.DataTable runQuery(string query)
    {
        try
        {
            if (connectionDB())
            {
                System.Data.DataTable dataTable = new System.Data.DataTable();
                System.Data.OleDb.OleDbCommand sqlQuery = new System.Data.OleDb.OleDbCommand(query, _OleDBconnection);
                System.Data.OleDb.OleDbDataAdapter adapter = new OleDbDataAdapter(sqlQuery);
                adapter.Fill(dataTable);
                disconnectDB();
                return dataTable;
            }
        }
        catch(Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
        }
        return null;

    }

    public int countRowsQueryResult(string query)
    {
        try
        {
            if (connectionDB())
            {
                System.Data.DataTable dataTable = new System.Data.DataTable();
                System.Data.OleDb.OleDbCommand sqlQuery = new System.Data.OleDb.OleDbCommand(query, _OleDBconnection);
                System.Data.OleDb.OleDbDataAdapter adapter = new OleDbDataAdapter(sqlQuery);
                adapter.Fill(dataTable);
                disconnectDB();
                return dataTable.Rows.Count;
            }
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
        }
        return -1;

    }
}

}

对不起我的英语

【问题讨论】:

  • 您的应用程序中有GDI+Win32 api 代码吗?
  • 只有windows窗体...
  • 您的 program.cs 的 Main 上方是否有 [STAThread] 属性?
  • 当您的程序的 Main() 入口点缺少 [STAThread] 属性或您在工作线程上运行此代码时,可能会发生这样的死锁。安装在您的机器上的不可靠的 shell 扩展也可能导致它。
  • [STAThread] 存在 link image ...

标签: c# winforms openfiledialog


【解决方案1】:

我认为 File.Copy 可能会挂起。尝试使用“c:\filename.pdf”之类的本地文件路径作为 URL 的相同代码。

//Crea il nome del file di destinazione
string _url = string.Format(@"c:\{0}{1}.pdf", temp, timenow);

如果此操作正常,则问题出在与 NAS 的连接上。

【讨论】:

  • 它可能是网络资源的连接,确实......但File.Copy不应该是那里的罪魁祸首......它是同步的,所以如果它挂起,它应该在给之前挂起再次单击按钮的选项。
  • @Jcl:我同意。我猜测这第一次尝试正常,但重试时连接丢失。
  • 不是连接NAS的问题。在本地我有同样的问题。在这个程序中,我有两种不同形式的相同代码(由 showDialog 模式使用)......一个运行良好,但这个循环......令人难以置信......
【解决方案2】:

当您的应用程序不使用单线程单元 COM 模型时,通常会发现此问题。

通常,对于 WinForms,在 Program.cs 中的 Main 方法上,您需要有一个 [STAThread] 属性,例如:

[STAThread]
static void Main()
...

如果你没有那个,并且你确定你不需要 MTA,你可以在那里添加它。

否则,我有这个实用程序代码(我确定不是我写的,我只是从某个地方复制它,但不能真正给予信任,它在我的“winforms 实用程序类”中有一段时间了)普通对话框,它使用 STA 创建一个新线程并在那里调用对话框:

public class DialogInvoker
{
   public CommonDialog InvokeDialog;
   private Thread InvokeThread;
   private DialogResult InvokeResult;

   public DialogInvoker(CommonDialog dialog)
   {
      InvokeDialog = dialog;
      InvokeThread = new Thread(new ThreadStart(InvokeMethod));
      InvokeThread.SetApartmentState(ApartmentState.STA);
      InvokeResult = DialogResult.None;
   }

   public DialogResult Invoke()
   {
      InvokeThread.Start();
      InvokeThread.Join();
      return InvokeResult;
   }

   private void InvokeMethod()
   {
      InvokeResult = InvokeDialog.ShowDialog();
   }
}

要使用它,请更改:

if (openFileDialog1.ShowDialog() == DialogResult.OK) 

为:

if ((new DialogInvoker(openFileDialog1)).Invoke() == DialogResult.OK) 

【讨论】:

  • !image STAThread 存在......理解你所说的......我应该知道线程 - 线程应用程序等......?!无论如何,我的程序中有两个 OpenFileDialog .. 具有相同的代码.. 一个运行良好,但这个 ... 进入循环 ..
  • 现在我意识到 ...btnAllegato_Click 函数在我单击时总是运行良好...但是当我单击另一个按钮(图像中的 Inserisci >>)时,我有循环过程此表单创建并执行对 db 的查询。第一次运行没问题...但是当我在查询后单击 btnAllegato 时,我有一个循环..form ...我的按钮 Inserisci btnInserisci 的代码我不知道为什么,但循环仅在查询 db 之后好的,接下来我点击 btnAllegato ..
  • 我不知道QueryAssist 是什么
  • 如何发布代码?对于图像来说太大了,对于评论来说太大了:/ ... QuertAssist 是一个个人类,它打开到 Db 的连接...方法 RunQuery(query) 返回一个 DataTable 相同的查询结果..
  • 如果您认为它与问题相关,请编辑您的问题并添加该信息
【解决方案3】:

将其从“使用”语句中删除。改变

using (OpenFileDialog openFileDialog1 = new OpenFileDialog())

OpenFileDialog openFileDialog1 = new OpenFileDialog();

看看这是否会有所作为。如果是这样,问题出在声明的处置上。

【讨论】:

  • 我没有区别.. :(
  • 那么我会说它不是 OpenFileDialog 本身。我同意那个说尝试将文件复制到本地并查看是否有所作为的人的观点。您也可以尝试在对话框 OK 后注释掉所有内容(只需在消息框中输入以显示文件名),然后慢慢取消注释,直到您可以进一步缩小范围。
  • 在本地我有同样的问题。问题是(在我看来).. 一个打开对话框...因为我无法单击确定或其他...因为窗口是全白的.. 是去循环...
【解决方案4】:

Io suggerirei di provare così:使用非 si utilizza con le 对话

try
{

    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    string path = string.Empty;

    openFileDialog1.Title = "Seleziona richiestaIT (PDF)..";
    openFileDialog1.Filter = ("PDF (.pdf)|*.pdf");
    openFileDialog1.FilterIndex = 1;
    openFileDialog1.FileName = "";


    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {

        //salva l'intero path
        path = openFileDialog1.FileName;

        //Togli l'estensione dal file
        string temp = Path.GetFileNameWithoutExtension(openFileDialog1.FileName);

        //Crea un nome univoco usando data e ora
        string timenow = System.DateTime.Now.ToString("dd-MM-yyyy_ss-mm-hh");


        //Crea il nome del file di destinazione
        string _url = string.Format(@"\\192.168.5.7\dati\SGI\GESTIONE IT\RichiesteIT\{0}{1}.pdf", temp, timenow);

        this.Cursor = Cursors.WaitCursor;

        File.Copy(path, _url);


        this.Cursor = Cursors.Default;
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

【讨论】:

  • 这是一个英文知识分享网站。这里的大多数人不会说意大利语,因此不会明白你在说什么。请用英文回答。
  • 对不起意大利线,但问题是意大利人提出的,我的回答是“我建议试试这个”:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多