UI /线程问题 [英] UI / Thread question

查看:81
本文介绍了UI /线程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了得到以下问题的答案,已经搜索了t'internet,似乎很多人多年来一直在问这个问题;这是问题: -



我的图书馆工作正常。我现在想要启动一个用户界面,以便有人可以模拟某些功能或其他功能,引入错误,更改变量等...



所以,我有我的表格

 Form1 simUi; 



和iv'e尝试用

 [STAThread] 

(似乎是t'internets最喜欢的答案)

 simUi =  new  Form1(); 





现在,问题在于:如果我使用

 simUi.ShowDialog(); 



方法永不返回;至少在我关闭表格之前。然而,如果我使用

 simUi.Show(); 



它返回但没有完成渲染表格等同样没用。

我希望能够在它自己的线程中启动ShowDialogue,但是编译器抱怨返回类型不正确。

那么,最好怎么做呢?请注意,我的表单上有许多控件,所以我不只是想更新一个拨号框。计划是使用我的form1.cs中的属性来访问我的UI上的响应并更新任何值;我有一个计时器事件要刷新。



希望我忽略了一些对你们所有人来说都很明显的东西'





谢谢理查德,

当我调用Show()方法时,窗口出现但没有控件或按钮;只是一个空白的灰色框。



@Lukeer

 guiThread =  new  System.Threading.Thread( new  System.Threading.ThreadStart(simUi.ShowDialog)); 

guiThread.Start();







更新:

也许我需要详细说明: - 我的项目本质上是一个乐器类驱动程序;根据连接的仪器,它选择正确使用的驱动程序并开始在它自己的线程中工作。通常不需要UI,但这个dll现在正被其他人和团队使用。我已经包含了一个模拟功能来实现我的开发,同时没有附加到任何硬件,但这是相对简单的。我的内部客户正在使用我的模拟器以相同的方式开发,但是想要调用每个错误条件/状态;这需要我添加人机界面。我知道这是可能的,因为我之前已经看过类似的事情了。

所以,如果我能找到一种方法来运行ShowDialogue()在它的获胜线程或获取Show()来绘制控件,我有它舔...

解决方案

这个适用于我:

 使用系统; 
使用 System.Collections.Generic;
使用 System.Text;

命名空间 KillmeConsole
{
class Program
{
静态 void Main( string [] args)
{
Console.WriteLine( 现在,开窗......);


System.Threading.ThreadStart ts = new System.Threading.ThreadStart(ShowForm);
System.Threading.Thread uiThread = new System.Threading.Thread(ts);
uiThread.Start();



Console.WriteLine( 它发生了吗? );
}


私有 静态 void ShowForm()
{
System.Windows.Forms.Application.Run( new MainForm ());
}
}

public class MainForm: System.Windows.Forms.Form
{
System.Windows.Forms.Button btNix = new System.Windows.Forms.Button();

public MainForm()
{
btNix.Location = new System.Drawing.Point( 20 20 );
btNix.Name = btNix;
btNix.Text = Nix;
this .Controls.Add(btNix);
}
}
}

只有 ShowForm(),一个没有参数的void返回方法。因此它匹配 ThreadStart 的构造函数所期望的。然后它运行应用程序窗口。它仍然是一个阻塞调用,但由于该方法本身在一个单独的线程(uiThread)上运行,主程序可以继续。



应用程序不会退出之前UI线程退出(只是关闭窗口)。



这个需要添加到 System.Windows.Forms.dll System.Drawing 。


ShowDialog()方法永远不会返回,因为它是一个对话框。其目的是允许用户在各种控件中输入信息,然后按确定按钮(或类似按钮)表示完成。相比之下, Show()方法提供了一个完全交互式的窗口,不需要完成指示器,它通常在应用程序终止时关闭。



但是,我从你的描述中看不到任何明显的问题;我认为你需要编辑你的问题并提供更多细节。


Having trawled t'internet for the answer to the following question, it seems that a lot of people have asked this over the years; here's the issue:-

I have my library working away just fine. I would now like to launch a UI so that someone can simulate some function or other, introduce errors, change variables etc...

So, I have my form

Form1 simUi;


and iv'e tried creating this in a method which is

[STAThread]

(seems to be t'internets favourite answer)

simUi = new Form1();



Now, here's the problem: If I use

simUi.ShowDialog();


The method never returns; at least until I close the form. Whereas, if I use

simUi.Show();


It returns but doesn't finish rendering the form and so is equally useless.
I want to be able to start the "ShowDialogue" in it's own thread but the compiler complains that the return type is incorrect.
So, how is best to do this ? Note that I have a number of controls on my form and so I don't just want to update a dialoge box. The plan is to use the properties in my form1.cs to access the responces on my UI and update any value's; I have a timer event to refresh.

Hopefully I am overlooking something that is obvious to all of you gurus'


Thanks Richard,
When I call the Show() method, the window appears but with no controls or buttons; just a blank grey box.

@Lukeer

guiThread = new System.Threading.Thread(new System.Threading.ThreadStart(simUi.ShowDialog));

guiThread.Start();




UPDATE:
Perhaps I need to elaborate a bit more:- My project is essentially an instrument class driver ; depending on which instrument is connected, it picks the correct driver to use and begins it’s work in it’s own thread. There wouldn’t ordinarily be any need for a UI but this dll is now being used by other people and teams. I had included a "simulation" feature to enable my development whilst not attached to any hardware but this was/is relatively simplistic. My internal "customers" are developing in the same way using my simulator but would like to invoke every error condition / state possible; this requires me to add a human interface. I know this is possible because I have seen similar done before.
So, if I could just find a way of running the ShowDialogue() in it’s won thread or getting the Show() to draw the controls, I’d have it licked…

解决方案

This one works for me:

using System;
using System.Collections.Generic;
using System.Text;

namespace KillmeConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("And now, starting a window...");


            System.Threading.ThreadStart ts = new System.Threading.ThreadStart(ShowForm);
            System.Threading.Thread uiThread = new System.Threading.Thread(ts);
            uiThread.Start();
            


            Console.WriteLine("Did it happen?");
        }


        private static void ShowForm()
        {
            System.Windows.Forms.Application.Run(new MainForm());
        }
    }

    public class MainForm : System.Windows.Forms.Form
    {
        System.Windows.Forms.Button btNix = new System.Windows.Forms.Button();

        public MainForm()
        {
            btNix.Location = new System.Drawing.Point(20, 20);
            btNix.Name = "btNix";
            btNix.Text = "Nix";
            this.Controls.Add(btNix);
        }
    }
}

There's just ShowForm(), a void-returning method without parameters. As such it matches what ThreadStart's constructor expects. It then runs the application window. It is still a blocking call, but since the method itself runs on a separate thread (uiThread), the main program can continue.

The application doesn't exit before the UI thread exits (just close the window).

This one needs references added to System.Windows.Forms.dll and System.Drawing.


The ShowDialog() method never returns, because it is a dialog. It's purpose is to allow the user to enter information in the various controls, and then signify completion by pressing the "OK" button (or similar). By contrast the Show() method offers a fully interactive window that does not require a "completion" indicator, it usually closes when the application is terminated.

However, I cannot see any obvious problem from your description; I think you need to edit your question and provide a bit more detail.


这篇关于UI /线程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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