C# 使用另一个线程控制表单,也防止重复表单

发布于 2024-11-06 19:47:34 字数 1266 浏览 9 评论 0原文

首先,我很抱歉,即使有很多给定的主题可能与我的问题相关,我仍然不知道如何解决我的问题..

只是编码中使用的大多数单词对我来说都是新的。 我真的需要你们的指导... =(

解决我的问题..

我的项目中有 2 个表格。 form1 充当我的登录屏幕 如果用户登录正确,则 form2 将打开,

我使用 UDP 作为连接套接字。我做了一个简单的UDP监听器 在计时器内部(原因是因为如果我不关闭它,如果没有什么可读的,它只会 让我的程序挂起,并且仍然继续等待消息

我的 UDP 侦听器正在不同的线程上运行,如下所示

Thread thread1 = new Thread(UDPListener.StartListener);
thread1.SetApartmentState(ApartmentState.STA);
Thread.Sleep(100);
thread1.TrySetApartmentState(ApartmentState.STA);

thread1.Start();

注意:当服务器程序向我发送打开 form2 的信号时, 现在问题出现了..

问题是.. form2出现了..但form1没有隐藏或visible=false; (form1 需要能够做到这一点

当我尝试显示 form2 并隐藏 form1 时,我正在执行此代码 注意:另一个问题是(不太确定)..我注意到它只是复制表单

Form1 f1 = new Form1();
Form2 f2 = new Form2();
f1.Command("009");
f1.MiniTicker.Enabled = false; //need to do this, cant do this without putting Form1 f1 = new Form1();
Login.TBOption.showTaskBar();
TaskbarHide.Taskbar.Hide();
f1.CoreAds.Enabled = false; //same here

f1.Visible = false;
f1.Hide();

f1.Invoke(
(MethodInvoker)(() =>
{
new TimerMode().Show();
}));


f1.Invoke(
(MethodInvoker)(() =>
{
new MainForm().Hide();
}));

最后注意:在我运行上面的代码后..Form1就像关闭一样,但之后它再次打开...

请帮助我解决我的问题。我需要完成我的论文项目=(

first of all, i am sorry that even if there's many given topic that may be related to my problem still i cant figure out how to solve my problem..

its just that most of the words used in the coding is new to me.
i really need your guidance guys.. =(

on to my problem..

i have 2 forms im my project.
the form1 is acting as my Login Screen
and form2 is will open if the users login is correct

im using UDP as my connection socket. i made a simple UDPlistener
inside a Timer(reason is because if i dont close it if theres nothing to read,it will just
make my program hangs, and still keep on waiting for messages
)

Note: my UDP listener is being run on a different thread like this

Thread thread1 = new Thread(UDPListener.StartListener);
thread1.SetApartmentState(ApartmentState.STA);
Thread.Sleep(100);
thread1.TrySetApartmentState(ApartmentState.STA);

thread1.Start();

when the Server Program sends me a signal to Open up form2
the problem now occur..

the problem is.. form2 showed up.. but form1 didn't hide or visible=false; (form1 need to be able to do that)

im doing this code when im trying to show form2 and hide form1
Note: also another problem is (not really sure) .. i notice its just duplicating the forms

Form1 f1 = new Form1();
Form2 f2 = new Form2();
f1.Command("009");
f1.MiniTicker.Enabled = false; //need to do this, cant do this without putting Form1 f1 = new Form1();
Login.TBOption.showTaskBar();
TaskbarHide.Taskbar.Hide();
f1.CoreAds.Enabled = false; //same here

f1.Visible = false;
f1.Hide();

f1.Invoke(
(MethodInvoker)(() =>
{
new TimerMode().Show();
}));


f1.Invoke(
(MethodInvoker)(() =>
{
new MainForm().Hide();
}));

Final Note: after i run the code above.. the Form1 is like closing, but after that it open up again...

please help me solve my problem. i need to finish my thesis project =(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹎☆浅夏丿初晴 2024-11-13 19:47:34

我假设第二批代码正在您在第一批中创建的线程上执行。如果是这种情况,那么大多数问题都会出现,因为您正在从工作线程访问 UI 元素。让我把这一点说得非常清楚。您可以使用工作线程中的 FormControl 执行的唯一操作是调用 InvokeBeginInvoke。就是这样。你绝对不能做任何其他事情。

  • 您不能调用 Form 构造函数。
  • 您无法设置 Form.Visible 属性。
  • 您不能调用Form.Hide
  • 您无法访问与表单相关或包含在其中的任何内容

这值得重复。除了 ISynchronizeInvoke 方法之外,您无法在工作线程的 Form 引用上使用其他方法。如果您尝试从主 UI 线程以外的线程访问任何 UI 元素,那么您的应用程序将会失败;有时是不可预测的,有时是惊人的。

I am going to assume that the second batch of code is executing on the thread you created in the first batch. If that is the case then most of your problems arise because you are accessing UI elements from the worker thread. Let me make this perfectly clear. The only thing you can do with a Form or Control from a worker thread is call Invoke or BeginInvoke. That is it. You absolutely cannot do anything else.

  • You cannot call a Form constructor.
  • You cannot set the Form.Visible property.
  • You cannot call Form.Hide.
  • You cannot access anything relating to or contained within the Form.

This is worth repeating. With the exception of the ISynchronizeInvoke methods there is nothing else you can use on a Form reference from a worker thread. If you do attempt to access any UI element from a thread other than the main UI thread then your application will fail; sometimes unpredictably and sometimes spectacularly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文