码迷,mamicode.com
首页 > 其他好文 > 详细

Thread调用SaveFileDialog

时间:2018-05-27 12:14:26      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:form   copy   点击   task   new   orm   invoke   imp   inf   

        public void ThreadSaveFileDialog(string sourceFileName)
        {
            Thread importThread = new Thread(new ParameterizedThreadStart(SaveDialog));
            importThread.SetApartmentState(ApartmentState.STA); //重点
            importThread.IsBackground = true;
            importThread.Start(sourceFileName);

        }


        public static MainForm ScratchForm;//这个是主窗口
        private void SaveDialog(object sourceFileName)
        {
            SaveDialogShow(sourceFileName as string);

        }

        delegate void SaveDialogShowCallBack(string sourceFileName);
        private void SaveDialogShow(string sourceFileName)
        {
       //判断主窗口是否跨线程调用
if (ScratchForm.InvokeRequired) { SaveDialogShowCallBack stcb = new SaveDialogShowCallBack(SaveDialogShow); ScratchForm.Invoke(stcb, new object[] { sourceFileName }); } else {
          //将saveFileDialog附加上主From上,否则的话点击主窗口会把savefiledialog给遮挡住。重复点击保存会弹出多个savefiledialog result
= ScratchForm.saveFileDialog.ShowDialog(ScratchForm); if (result == DialogResult.OK) { string destFileName = ScratchForm.saveFileDialog.FileName; Task.Run(() => { File.Copy(sourceFileName, destFileName, true); }); } } }

 

Thread调用SaveFileDialog

标签:form   copy   点击   task   new   orm   invoke   imp   inf   

原文地址:https://www.cnblogs.com/xbzhu/p/9095305.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!