暂停线程,直到其他程序/进程完成运行 [英] Pausing thread until other program/process finishes to run

查看:69
本文介绍了暂停线程,直到其他程序/进程完成运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试在映射网络驱动器时暂停我的主线程,但每次网络驱动器映射的时间都不同。我不想使用Thread.Sleep(< static number =>)。

该程序如下:

线程T2 =  new 线程(()= >  
{
System.Diagnostics.Process.Start( net.exe @ 使用Z:\\Server-Share \My_Files \);
}
T2.Start();
T2.Join();
File.Copy( @ C:\daily_files\Updated_data.xlsx @ Z:\ Updated_data.xlsx true );



这里的问题是T2启动网络驱动器的映射,这需要3-5秒,主线程的下一步是复制粘贴文件到新的共享。复制粘贴开始时间早于网络共享映射完成 - 然后我得到错误,该位置不可用...所以 - 看起来网络共享映射正在其自己的线程中运行....

有谁知道如何使主线程等待共享驱动器映射完成?



非常感谢大家!

解决方案

创建一个实例 System.Diagnostics.Process 而是设置 StartInfo 属性并在上注册一个处理程序在调用开始之前退出。然后在进程退出时触发退出的事件,至少我的阅读方式是 this [ ^ ];)


请阅读这篇文章





HTTP:// WWW ?.google.co.in / URL SA = T&安培; RCT = J&安培; q =穿线+ C%23安培;源=纸幅放大器; CD = 4和; CAD = RJA&安培; VED = 0CEsQFjAD&安培; URL = HTTP%3A%2F%2Fwww .albahari.info%2Fthreading%2Fthreading.pdf& ei = aOdrUcXLL4v4sgav04CIAQ& usg = AFQjCNE8s81_81qYKoz9uKBL5qWkd7YzZw& bvm = bv.45175338,d.Yms [ ^ ]

Hello guys,
I''ve been trying to pause my main thread while the network drive is being mapped, but each time the time for network drive mapping is different. I don''t want to use Thread.Sleep(<static number="">).
The program is as follows:

Thread T2 = new Thread(() =>
{
System.Diagnostics.Process.Start("net.exe", @"use Z: \\Server-Share\My_Files\");
}
T2.Start();
T2.Join();
File.Copy(@"C:\daily_files\Updated_data.xlsx", @"Z:\Updated_data.xlsx", true);


The problem here is that T2 starts the mapping of network drive, which takes 3-5 seconds and the next step in main thread is copy-paste file to that new share. And the copy-paste starts earlier then the network share mapping finishes - then I get error that location is not available... So - looks like the network share mapping is running in its own thread....
Does anyone know how to make main thread wait for the shared drive mapping to finish?

Thanks a lot guys!

解决方案

Create an instance of System.Diagnostics.Process instead, set the StartInfo property and register a handler on Exited before calling Start. The Exited event is then fired when the process exits, at least that''s how I read this[^] ;)


please read this article


http://www.google.co.in/url?sa=t&rct=j&q=threading+C%23&source=web&cd=4&cad=rja&ved=0CEsQFjAD&url=http%3A%2F%2Fwww.albahari.info%2Fthreading%2Fthreading.pdf&ei=aOdrUcXLL4v4sgav04CIAQ&usg=AFQjCNE8s81_81qYKoz9uKBL5qWkd7YzZw&bvm=bv.45175338,d.Yms[^]


这篇关于暂停线程,直到其他程序/进程完成运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆