C#waitforexit问题需要尽快帮助 [英] C# waitforexit issues need help ASAP

查看:83
本文介绍了C#waitforexit问题需要尽快帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直四处寻找这个问题,似乎我不知所措。基本上代码在一个计时器中运行10秒,它应该在一个文件夹中查找并处理每个文件(它确实)并删除该文件。但是在我尝试修复我遇到的UI冻结问题但尝试使用线程(我是新的)之后它停止了工作。一切都很好,直到这一点,所以我不得不注释waitforexit,但如果我没有这样的功能,它将删除文件,然后通过

 prs.FileName = @C:\\处理文件\\dcmtk\bin\storescu-tls.exe; 

。不确定需要什么来解决这个问题。请帮忙。



我的尝试:



 foreach(文件中的字符串)
{
// if(File.Exists(s))
finalpath = Host ++ port ++ s;
流程pr = new Process();
ProcessStartInfo prs = new ProcessStartInfo();
prs.CreateNoWindow = true;
prs.UseShellExecute = false;
prs.RedirectStandardOutput = true;
prs.WindowStyle = ProcessWindowStyle.Hidden;
prs.FileName = @C:\dcmtk \ bin \ storescu-tls.exe;
prs.Arguments = finalpath;
textBox2.Text = s;

pr.StartInfo = prs;

ThreadStart ths = new ThreadStart(()=> pr.Start());
线程th =新线程(ths);
th.Start();
pr.WaitForExit();

File.Delete(s);

解决方案

调用 pr.Start( )在新的线程中不会取消冻结你的UI。这不是启动导致UI冻结的过程的行为;它正在等待完成这个问题的过程。



你不能删除文件,直到过程完成,所以你必须保持 WaitForExit



最简单的解决方案是使用 BackgroundWorker [ ^ ]处理后台线程上的文件:

< pre lang =C#> private void ProcessFiles()
{
< span class =code-keyword> if (!backgroundWorker.IsBusy)
{
string [] files = Directory。 GetFiles(Path, *);
if (files.Length!= 0
{
backgroundWorker.RunWorkerAsync(文件);
}
}
}

private void backgroundWorker_DoWork( object sender,DoWorkEventArgs e)
{
string [ ] files =( string [])e .Argument;
for int index = 0 ; index < files.Length; index ++)
{
string fileToProcess = files [index];
backgroundWorker.ReportProgress( 100 * index / files.Length,fileToProcess);

string finalpath = Host + + port + + fileToProcess;

流程pr = Process.Start( new ProcessStartInfo
{
CreateNoWindow = true
UseShellExecute = false
RedirectStandardOutput = true
WindowStyle = ProcessWindowStyle.Hidden,
FileName = @ C:\dcmtk \\ bin\storescu-tls.exe
Arguments = finalpath
});

pr.WaitForExit();
File.Delete(s);
}
}

私有 void backgroundWorker_ProgressChanged( object sender,ProgressChangedEventArgs e)
{
textBox2.Text =( string ) e.UserState;
}



适用于初学者的BackgroundWorker类示例 [ ^ ]

MultiThreading使用后台工作者,C# [ ^ ]


ok O稍微修改了一下代码:它有点草率但到目前为止我还没有收到任何错误。但是我删除/移动已发送文件的循环仍然阻塞,因为另一个进程仍在使用它:



 void m_oWorker_DoWork(object sender ,DoWorkEventArgs e)
{
//注意:永远不要在这里玩UI线程...

//耗时的操作
for(int i = 0 ; i< 100; i ++)
{

{
string filepath =(@C:\ MyDy);
string [] files = Directory.GetFiles(filepath,*。*,SearchOption.AllDirectories);
for(int index = 0; index< files.Length; index ++)
foreach(文件中的字符串s)
{
string fileToProcess = filepath;
m_oWorker.ReportProgress(100 * index / files.Length,fileToProcess);

字符串端口;
port =106;
string Host;
Host =192.168.99.13;
// string dicompath = @C:\ MyDy;


string finalpath = Host ++ port ++ s;
//textBox1.Text = finalpath;
System.Diagnostics.Process pr = System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
FileName = @C:\dcmtk \ bin\storescu-tls.exe,
Arguments = finalpath

});

pr.Start();

//File.Move(s,@C:\ MyDels);
//提供者源文件夹路径
string SourceFolder = @c:\ MyDy \;
//提供目标文件夹路径
// string DestinationFolder = @C:\ MyDels \;

var filess = new DirectoryInfo(SourceFolder).GetFiles(*。*);
pr.WaitForExit();
if(!m_oWorker.IsBusy)
//循环文件并移动到目标文件夹
foreach(filess中的FileInfo文件)
{
//删除文件if已存在
File.Delete(SourceFolder + file.Name);

//将文件移动到目标文件夹
//file.MoveTo(DestinationFolder + file.Name);

//System.IO.File.Delete(s);
//File.Delete(s);

}

pr.WaitForExit();
Thread.Sleep(50);
m_oWorker.ReportProgress(i);

//如果在执行过程中按下了取消按钮
//从取消改变状态--->取消'
if(m_oWorker.CancellationPending)
{
e.Cancel = true;
m_oWorker.ReportProgress(0);
返回;
}

}

//报告完成操作100%完成
m_oWorker.ReportProgress(100);
}
}


I have been going round and round with this issue and seems Im at a loss. basically the code is in a timer to run ever 10 secs, it should look in a folder and process each file(and it does) and delete that file. But it stopped working after I tried to fix the UI freezing issue I had but trying to use thread(Im new). everything was fine up til this point so I had to comment out the waitforexit but if I dont have a function like this it will delete the files before it processes the files via

prs.FileName = @"C:\dcmtk\bin\storescu-tls.exe";

. Not sure what is needed to fix this. please help.

What I have tried:

foreach (string s in files)
            {
                //if (File.Exists(s))
                finalpath = Host + " " + port + " " + s;
                Process pr = new Process();
                ProcessStartInfo prs = new ProcessStartInfo();
                prs.CreateNoWindow = true;
                prs.UseShellExecute = false;
                prs.RedirectStandardOutput = true;
                prs.WindowStyle = ProcessWindowStyle.Hidden;
                prs.FileName = @"C:\dcmtk\bin\storescu-tls.exe";
                prs.Arguments = finalpath;
                textBox2.Text = s;
                
                pr.StartInfo = prs;
               
                ThreadStart ths = new ThreadStart(() => pr.Start());
                Thread th = new Thread(ths);
                th.Start();
                pr.WaitForExit();

                File.Delete(s);

解决方案

Calling pr.Start() in a new Thread isn't going to un-freeze your UI. It's not the act of starting the process that's causing the UI to freeze; it's waiting for the process to finish that's the problem.

You can't delete the file until the process has finished, so you have to keep the WaitForExit.

The simplest solution would be to use a BackgroundWorker[^] to process the files on a background thread:

private void ProcessFiles()
{
    if (!backgroundWorker.IsBusy)
    {
        string[] files = Directory.GetFiles(Path, "*");
        if (files.Length != 0)
        {
            backgroundWorker.RunWorkerAsync(files);
        }
    }
}

private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    string[] files = (string[])e.Argument;
    for (int index = 0; index < files.Length; index++)
    {
        string fileToProcess = files[index];
        backgroundWorker.ReportProgress(100 * index / files.Length, fileToProcess);

        string finalpath = Host + " " + port + " " + fileToProcess;
        
        Process pr = Process.Start(new ProcessStartInfo
        {
            CreateNoWindow = true,
            UseShellExecute = false,
            RedirectStandardOutput = true,
            WindowStyle = ProcessWindowStyle.Hidden,
            FileName = @"C:\dcmtk\bin\storescu-tls.exe",
            Arguments = finalpath
       });
       
       pr.WaitForExit();
       File.Delete(s);
    }
}

private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    textBox2.Text = (string)e.UserState;
}


BackgroundWorker Class Sample for Beginners[^]
MultiThreading Using a Background Worker, C#[^]


ok O have revamped the code a bit: its a bit sloppy but so far I havent received any errors. However my loop to delete/move files that have been sent keeps getting block because another process is using it still:

void m_oWorker_DoWork(object sender, DoWorkEventArgs e)
       {
           //NOTE : Never play with the UI thread here...

           //time consuming operation
           for (int i = 0; i < 100; i++)
           {

               {
                   string filepath = (@"C:\MyDir");
                   string[] files = Directory.GetFiles(filepath, "*.*", SearchOption.AllDirectories);
                   for (int index = 0; index < files.Length; index++)
                       foreach (string s in files)
                       {
                           string fileToProcess = filepath;
                           m_oWorker.ReportProgress(100 * index / files.Length, fileToProcess);

                           string port;
                           port = "106";
                           string Host;
                           Host = "192.168.99.13";
                           //string dicompath = @"C:\MyDir";


                           string finalpath = Host + " " + port + " " + s;
                           //textBox1.Text = finalpath;
                           System.Diagnostics.Process pr = System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo
                           {
                               CreateNoWindow = true,
                               UseShellExecute = false,
                               RedirectStandardOutput = true,
                               WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
                               FileName = @"C:\dcmtk\bin\storescu-tls.exe",
                               Arguments = finalpath

                           });

                           pr.Start();

                           //File.Move(s, @"C:\MyDels");
                           //Provider Source Folder Path
                           string SourceFolder = @"c:\MyDir\";
                           //Provide Destination Folder path
                           //string DestinationFolder = @"C:\MyDels\";

                           var filess = new DirectoryInfo(SourceFolder).GetFiles("*.*");
                           pr.WaitForExit();
                           if (!m_oWorker.IsBusy)
                               //Loop throught files and Move to destination folder
                               foreach (FileInfo file in filess)
                           {
                               //delete file if already exists
                               File.Delete(SourceFolder + file.Name);

                               //Move the file to destination folder
                               //file.MoveTo(DestinationFolder + file.Name);

                               //System.IO.File.Delete(s);
                               //File.Delete(s);

                           }

                           pr.WaitForExit();
                           Thread.Sleep(50);
                           m_oWorker.ReportProgress(i);

                           //If cancel button was pressed while the execution is in progress
                           //Change the state from cancellation ---> cancel'ed
                           if (m_oWorker.CancellationPending)
                           {
                               e.Cancel = true;
                               m_oWorker.ReportProgress(0);
                               return;
                           }

                       }

                   //Report 100% completion on operation completed
                   m_oWorker.ReportProgress(100);
               }
           }


这篇关于C#waitforexit问题需要尽快帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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