繁体   English   中英

为什么在启动新进程而应用程序(c#)仍在运行时线程停止?

[英]Why thread is stopped when start new process, while application (c#) is still running?

在多线程应用程序中启动新进程时,我必须认真解决任何问题吗?

我在一个简单的项目中尝试过:

static void Main(string[] args)
{
    Process.Start(@"D:\System\Desktop\a.txt");
    MessageBox.Show("Success");
}

它运行完美。 但是,当我在使用多线程的大项目中执行此操作时,线程停止工作(“ a.txt”打开,但未显示“成功”),而我的应用程序(其他线程)运行良好。

在这种情况下有什么问题?

如果您有Windows.Forms应用程序,并且尝试从不是主用户界面线程的线程显示消息框,则该消息框的行为是不确定的。 意思是,它可能会或可能不会显示,不一致或其他问题。

例如,显示BackgroundWorker的DoWork事件中的消息框可能有效,也可能无效。 在一种情况下,无论单击什么按钮,消息框结果始终被取消。

因此,如果您仅将消息框用于调试目的,请使用其他技术。 如果必须显示一个消息框,请从主用户界面线程中调用它。

控制台应用程序通常不应在显示消息框时出现问题。 但是,在某些情况下,必须在消息框调用之前将线程休眠100ms。

注意,正如TomTom指出的那样,主要的用户界面线程是应用程序的Windows消息循环。 这使我想起,我曾经不得不在Console应用程序中创建一个Form才能创建Windows消息循环,以便我的应用程序可以响应Windows消息。

这不是答案 -我无法在注释中添加所有这些代码...

这对我有用。 告诉我您的代码与此有何不同:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.IO;

namespace Test
{
    class Program
    {
        const string OutputFile = @"E:\Output.txt";

        object _lock = new object();

        static void Main(string[] args)
        {
            Program program = new Program();

            Thread thread = new Thread(program.ThreadMethod);
            thread.Start(@"E:\Test.txt");

            thread = new Thread(program.ThreadMethod);
            thread.Start(@"E:\DoesntExist.txt");

            Console.ReadKey();
        }

        void ThreadMethod(object filename)
        {
            String result = RunNormal(filename as string);
            lock (_lock)
            {
                FileInfo fi = new FileInfo(OutputFile);
                if (!fi.Exists)
                {
                    try
                    {
                        fi.Create().Close();
                    }
                    catch (System.Security.SecurityException secEx)
                    {
                        Console.WriteLine("An exception has occured: {0}", secEx.Message);
                        return;
                    }
                }

                StreamWriter sw = fi.AppendText();
                sw.WriteLine(result);
                sw.Close();
            }
        }

        string RunNormal(string fullfilename)
        {
            try
            {
                Process.Start(fullfilename);
                return fullfilename + "|Success";
            }
            catch (Exception e)
            {
                return fullfilename + "|" + e.ToString();
            }
        }
    }
}

Output.txt中的输出为:

E:\DoesntExist.txt|System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified
   at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
   at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start(String fileName)
   at Test.Program.RunNormal(String fullfilename) in E:\Projekti\VS2010\Test\Test\Program.cs:line 59
E:\Test.txt|Success

您的代码有多少不同? 您还调用其他方法吗? 您如何处理结果?

确保Process.Start正常运行。 在某些情况下,传递文件名是不够的。 在示例代码中,您将必须设置use-shell属性。 否则,您将必须使用cmd start <filename>或同等功能。

因此,只需启动NotePad.exe以确保Process.Start正常运行。 如果是这样,那么您的问题就是处理命令和命令行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM