1

我正在尝试制作一个 Windows 窗体应用程序。该应用程序使用多线程,每个线程调用一个方法并更新在主线程上创建的控件。我使用调用来更新控件,该应用程序在 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;}

}
4

3 回答 3

0

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

于 2012-05-29T09:48:16.357 回答
0

对于调试,请确保通过 CallMethod() 的所有路径更新 UI(即使它只是带有“到达这一点”文本)。似乎 txtResult.InvokeRequired 可能是假的,或者您可能从 Web 请求中获得了异常。

于 2012-05-28T15:35:01.167 回答
-1

在 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);
    }
于 2012-05-28T15:13:50.410 回答