2 yexuanbaby yexuanbaby 于 2015.06.02 19:57 提问

C# 中需要延时,该怎么做.求解

C#在操作word时,将RichTextBox内容粘贴到word时失败,昨天经网友提醒,发现是延时的.
于是今天在copy()之后,延时几秒中以达到效果.但怎么也不成功.求教

 string strDirectory = System.IO.Directory.GetCurrentDirectory().ToString();
                string strTempFileName = strDirectory + @"\temp000.docx";
                Clipboard.Clear();

                _Word._Application _wardApp = new _Word.Application();
                _wardApp.Visible = false;
                _Word._Document _wordDoc = null;
                object missing = System.Reflection.Missing.Value;
                object filename = strTempFileName;
                object readOnly = false;
                object isVisible = false;
                _wordDoc = _wardApp.Documents.Add(missing, missing, missing, isVisible);
                _Word.Paragraph _wordPara = _wordDoc.Content.Paragraphs.Add(ref missing);
                _Word.Range _wordRange = _wordDoc.Paragraphs[1].Range;

                #region 将RichTextBox的内容存储到临时word文档中
                try
                {       
                    //建立word工作

                    rtxbContent.SelectAll();
                    rtxbContent.ClipboardCopy();

                                        **//暂时使用了一个弹出窗口解决的**.                                       
                    MessageBox.Show("正在生成文件,请点击以确认","确信生成");



                    _wordDoc.Activate();       

                    _wordRange.Paste();   

                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "失败啦!<( ̄︶ ̄)>");
                    _wordRange.Paste();     

                }
                //保存word文档
                _wordDoc.SaveAs2(filename, missing, missing, missing, missing, missing, missing, missing,
                    missing, missing, missing, missing, missing, missing, missing, missing, missing);
                //退出word
                _wardApp.Quit(true, missing, missing);

                MessageBox.Show("保存成功", "成功啦");


                #endregion

4个回答

qq_24724387
qq_24724387   2015.06.02 20:53
已采纳

Thread.Sleep(10*1000); 1秒=1000毫秒,可以改成10000。

chenziyuan123456
chenziyuan123456   2015.06.02 20:00

使用线程 线程等待 试一下

caozhy
caozhy   Ds   Rxr 2015.06.02 20:14

Thread.Sleep(1000); //延迟1秒。

miracle_yao
miracle_yao   2015.07.28 14:47

用Thread.Sleep(TimeSpan.FromSeconds(1)); //这样延迟1秒

Csdn user default icon
上传中...
上传图片
插入图片