多线程应用程序在 Windows 7 上不起作用

本文关键字:不起作用 Windows 应用程序 多线程 | 更新日期: 2023-09-27 18:35:36

我正在尝试制作一个Windows Form App。该应用程序使用多线程,每个线程调用一个 methos,并更新在主线程上创建的控件。我使用调用来更新控件,该应用程序适用于Windows服务器企业版,但它适用于Windows 7 64位。在 WIndows 7 上,应用程序在更新界面 2 次后停止执行任何操作。我不知道似乎是什么问题。我尝试使用多个线程和任务(Task.Factory.StartNew()),结果相同(更新控件 2 次)。无错误消息。谢谢。

编辑:CallMethod(),我正在呼叫WCF并等待重新发布。似乎 WCF 调用正在为前两个线程返回一些内容,而对于其余线程,它不是......

法典:

主要方法:

            for (int i = 0; i < NoThreads; i++)
            {
                int index = i;
                Thread t = new Thread(CallMethod);
                t.Name = "Thread [" + Cicle + "] Cicle [" + i + "]";
                threads[i] = t;
            }
            for (int i = 0; i < NoThreads; i++)
            {
                threads[i].Start();
            }

调用方法:

private string CallMethod()
{
 try
   {
      //calling a webservice
      string message = .....
      if (txtResult.InvokeRequired)
      { txtResult.Invoke((MethodInvoker)(() => txtResult.AppendText(message))); }
 catch
 {throw;}
}

多线程应用程序在 Windows 7 上不起作用

为了进行调试,请确保通过 CallMethod() 的所有路径都更新 UI(即使它只是带有"到了这一点"文本)。看起来txtResult.InvokeRequired可能是假的,或者您可能从Web请求中获得了异常。

问题已解决在每个线程中调用 WCF 后,我必须关闭连接

在 x64 机器 (Windows 7) 上为线程工作的示例:

class Server
{
    private TcpListener tcpListener;
    private Thread listenThread;
    private static string rootPath = "";
    public Server()
    {
        IPAddress ipAddress = Dns.Resolve("localhost").AddressList[0];
        this.tcpListener = new TcpListener(ipAddress, 9501);
        //this.tcpListener = new TcpListener(RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].IPEndpoint);
        this.listenThread = new Thread(new ThreadStart(ListenForClients));
        Trace.WriteLine("Server Working", "Information");
        this.listenThread.Start();
    }

    private void ListenForClients()
    {
        this.tcpListener.Start();
        Trace.WriteLine("Server TcpListener Started", "Information");
        while (true)
        {
            //blocks until a client has connected to the server
            TcpClient client = this.tcpListener.AcceptTcpClient();
            Trace.WriteLine("Server TcpListener New Client", "Information");
            // create a thread to handle the client
            //ParameterizedThreadStart HandleClientConn;
            Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
            clientThread.Start(client);
        }
    }
    private void HandleClientComm(object client)
    {
        Trace.WriteLine("Server TcpListener New Client Handle Thread", "Information");
        TcpClient tcpClient = (TcpClient)client;
        NetworkStream nStream = tcpClient.GetStream();
        Image img = Image.FromStream(nStream);
        Trace.WriteLine("Server TcpListener Image is received", "Information");
        string imageName = client.GetHashCode() + ".jpg";
        string imagePath = Path.Combine(Environment.GetEnvironmentVariable("RoleRoot") + @"'", @"approot'"+ imageName);
        img.Save(imagePath, ImageFormat.Jpeg);
        rootPath = Environment.GetEnvironmentVariable("RoleRoot");
        Dictionary<string, string> templates = GetTemplates();
        string sDataDir = String.Format("{0}StasmData''", rootPath);
        StasmHelper stasm = new StasmHelper();
        Face retVal = stasm.GetHumanFace(imagePath, sDataDir, templates);
        File.Delete(imagePath);
    }