Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么启动新进程时线程停止,而应用程序(c#)仍在运行?_C#_.net_Multithreading_Process_Process.start - Fatal编程技术网

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

为什么启动新进程时线程停止,而应用程序(c#)仍在运行?,c#,.net,multithreading,process,process.start,C#,.net,Multithreading,Process,Process.start,在多线程应用程序中启动新进程时,是否有需要小心处理的问题 我在一个简单的项目中尝试了这一点: static void Main(string[] args) { Process.Start(@"D:\System\Desktop\a.txt"); MessageBox.Show("Success"); } 它运行得很好。但当我在使用多线程的大项目中执行此操作时,它的线程停止工作(“a.txt”打开,但“Success”未显示),而我的应用程序(其他线程)运行良好 这种情况下会出

在多线程应用程序中启动新进程时,是否有需要小心处理的问题

我在一个简单的项目中尝试了这一点:

static void Main(string[] args)
{
    Process.Start(@"D:\System\Desktop\a.txt");
    MessageBox.Show("Success");
}
它运行得很好。但当我在使用多线程的大项目中执行此操作时,它的线程停止工作(“a.txt”打开,但“Success”未显示),而我的应用程序(其他线程)运行良好


这种情况下会出现什么问题?

如果您有一个Windows.Forms应用程序,并且您试图从不是主用户界面线程的线程显示消息框,则消息框的行为未定义。也就是说,它可能显示也可能不显示、不一致或其他一些问题

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

因此,如果您只是出于调试目的使用消息框,请使用不同的技术。如果必须显示消息框,请从主用户界面线程调用它

控制台应用程序通常不会出现显示消息框的问题。然而,我也有过这样的情况,在消息框调用之前,我必须将线程休眠100毫秒


注意,正如TomTom指出的,主用户界面线程是应用程序的Windows消息循环。这提醒了我,我曾经不得不在控制台应用程序中创建一个窗体,以便创建Windows消息循环,这样我的应用程序才能响应Windows消息。

如果您有一个Windows.Forms应用程序,并且您试图从非主用户界面线程的线程中显示消息框,消息框的行为未定义。也就是说,它可能显示也可能不显示、不一致或其他一些问题

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

因此,如果您只是出于调试目的使用消息框,请使用不同的技术。如果必须显示消息框,请从主用户界面线程调用它

控制台应用程序通常不会出现显示消息框的问题。然而,我也有过这样的情况,在消息框调用之前,我必须将线程休眠100毫秒


注意,正如TomTom指出的,主用户界面线程是应用程序的Windows消息循环。这提醒了我,我曾经不得不在控制台应用程序中创建一个表单来创建Windows消息循环,这样我的应用程序才能响应Windows消息。

确保
进程。启动
工作正常。在某些情况下,传递文件名是不够好的。在示例代码中,必须设置UseShell属性;否则,您必须使用
cmd start
或等效工具


因此,只需启动NotePad.exe即可确保
Process.start
正常工作。如果是这样的话,那么您的问题是进程命令和命令行。

确保
进程。启动
工作正常。在某些情况下,传递文件名是不够好的。在示例代码中,必须设置UseShell属性;否则,您必须使用
cmd start
或等效工具


因此,只需启动NotePad.exe即可确保
Process.start
正常工作。如果是这样,那么您的问题是进程命令和命令行。

这不是答案-我不能将所有这些代码放在注释中

这对我有用。告诉我您的代码与此代码的区别:

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

你的代码有多大不同?你会调用其他方法吗?如何处理结果?

这不是答案-我不能将所有这些代码放在注释中

这对我有用。告诉我您的代码与此代码的区别:

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

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

能否尝试Console.Writeline(“成功”);相反,看看这是否有效?我试过了。同样的结果!启动该进程后,该线程中的所有内容都将停止!嗯,我无法复制这个。。。我制作了一个线程来打开一个进程,并将一条消息放到控制台,它可以正常工作。。。你还有别的事吗?请您尝试在进程之前放置一个断点。启动,然后逐步检查它是否真的停止(因为它不应该停止)。是的,线程没有停止工作。但它不再处理任何代码,之后不再处理任何返回值。。。。与停止时相同。我不明白怎么了。这是我的函数:'私有字符串RunNormal(string fullfilename){try{Process.Start(fullfilename);return fullfilename+“| Success”}catch(异常e){return fullfilename+“|”+e.ToString();}“您的大型项目是控制台应用程序还是Windows应用程序?您希望创建一个半量,如果情况更糟,请抛出一些线程。例如,在那里可以尝试Console.Writeline(“成功”);相反,看看这是否有效?我试过了。同样的结果!启动该进程后,该线程中的所有内容都将停止!嗯,我无法复制这个。。。我制作了一个线程来打开一个进程,并将一条消息放到控制台,它可以正常工作。。。你还有别的事吗?请您尝试在进程之前放置一个断点。启动,然后逐步检查它是否真的停止(因为它不应该停止)。是的,线程没有停止工作。但它不再处理任何代码,之后不再处理任何返回值。。。。与停止时相同。我不明白怎么了。这是我的函数:“私有字符串RunNormal(stringfullfilename){try