如何在控件返回主线程的同时在另一个线程上运行无限while循环 [英] How to run an infinite while loop on another thread while control is returned to the main thread

查看:63
本文介绍了如何在控件返回主线程的同时在另一个线程上运行无限while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里度过美好的一天,我很高兴成为这个论坛的一员。有些时候,我已经得到了来自这个论坛的帖子的资源,文章,问题和答案的帮助,这促使我加入。现在我已成为论坛的一员,我知道我会得到更多的帮助。

这是我第一次提问,因为我加入了这群程序员,所以我可能不知道规则,但如果我有任何提交,你可以纠正我。谢谢。

我正在开展一个项目,你们大多数人会在2或3周之前称之为最简单的项目,从那以后我就一直很难过。这个项目就是我所说的NIGHTMODE。我想在晚上降低笔记本电脑的亮度(因为这是我编写代码的最佳时间),但最低的亮度设置仍然太亮了,我想到覆盖一个透明的黑色窗口形状,它将保持在每个其他窗口,将对MouseClick不可见。我开始打开一个名为NiteMode的表单然后导入user32.dll来处理透明度和点击windows窗体并将windowState设置为maximum并将TopMost属性设置为true但它不会总是在顶部,然后我定义了一个线程并为其分配了一个方法,我使用while循环在每半秒钟内将表单的TopMost属性连续设置为true。我打开了另一个表单(mainNiteMode),它包含一个progressBar来设置第一个表单的不透明度(NiteMode)和一个用于打开和关闭的checkBox,即调用第一个表单的NitMode.Show()和NiteMode.Hide()( NiteMode)我实例化为一个名为'niteform'的类型。我处理了checkBox的checkChanges事件,如果检查了它,我会在其中调用niteform.Show()。

现在问题是每当我调用niteform.Show()时控件都没有返回到mainNiteMode,因为我重写NiteMode的onShown和里面我调用了thread.Start()。这意味着秒线程没有将控制权返回给第一个,因为while循环正在运行。

我已经尝试了几件事情,但同样的问题一直在重复自我。

我甚至试图在mainNiteMode表单的另一个线程上创建NiteMode表单的实例,但它不起作用,我尝试创建一个后台工作线程,并从工作线程调用的方法实例化NiteMode但我遇到了同样的问题。请问我该怎么办?非常感谢您的平常帮助。

解决方案

CodeProject是您的朋友:在C#中设置屏幕亮度:[ ^ ]。



您是否了解免费的FLux软件?:[ ^ ]。



坦率地说,我认为在所有其他窗口上放置半透明窗口以使屏幕变暗的想法是......非常奇怪...我会寻找另一个技术

Good day here, am happy to be part of this forum. For some times now I have been helped with resources, articles, questions and answers to posts from this forum and that was what prompted me to join. Now that I am part of the forum, I know that I will enjoy more of your help.
This will be my first time of asking questions since I joined this group of programmers, therefore I may not know the rules but you can correct me should I have any 'commit'. Thanks.
I was working on a project that most of you will call 'the simplest project' 2 or 3weeks ago and since then I've been having tough time with it. The project is what I called NIGHTMODE. I wanted to reduce the brightness of my laptop at night (because that is the best available time for me to code) but the lowest brightness setting was still too bright then I thought of overlaying a transparent black windows form which will stay on top of every other window and will be invisible to MouseClick. I started by opening a form called NiteMode then import user32.dll for handling the transparency and the click through of the windows form and set the windowState to maximize and TopMost property to true but it will not always be on top, then I defined a thread and assigned it a method inside which I used a while-loop to continuously set the form's TopMost property to true every half of a second. I opened another form(mainNiteMode) which contain a progressBar to set the opacity of the first form(NiteMode) and a checkBox to switch on and off, i.e to call NitMode.Show() and NiteMode.Hide() of the first form(NiteMode) which I instantiated as a type called 'niteform'. I handled the checkBox's checkChanges event, inside which I called niteform.Show() if its checked.
Now the problem was that control was not returned to mainNiteMode whenever I call niteform.Show() because I override the NiteMode's onShown and inside it I called thread.Start(). Meaning that the seconds thread is not returning control to the first because of the while-loop its running.
I have tried several things but the same problem kept on repeating it self.
I even tried to create the instance of NiteMode form on another thread from the mainNiteMode form but it will not work, I try to create a backgroundworker thread instead and instantiate the NiteMode from a method called by the worker thread but I encountered same problem. Please what can I do? Thanks so much for your usual help.

解决方案

CodeProject is your friend: "Setting Screen Brightness in C#: [^].

Are you aware of the free FLux software ?: [^].

Frankly, I think the idea of putting a semi-transparent window on top of all the other windows to darken the screen is ... very strange ... I would look for another technique.


这篇关于如何在控件返回主线程的同时在另一个线程上运行无限while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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