在C#.net的用户控件中使用线程 [英] Using Thread in User Control in C#.net

查看:87
本文介绍了在C#.net的用户控件中使用线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


如何在C#.net的用户控件中使用线程?
在我的任务中;
在我的用户控件中:
以下是用户控件中的控件:
1)2图片框[1)开始2)停止]
2)1图片盒[已连接]
3)2个图片框[1)在线2)offline]
4)1个图片框[处理图片]
5)标签[字节-文件传输字节]
5)计时器(如果需要)

处理中:
当用户单击开始"按钮时,线程将启动.
用户单击停止"按钮时,线程将停止
用户单击开始"按钮时的在线图片视图
用户单击停止"按钮时的离线图片视图
文件传输字节显示在标签中.

注意:
1)可以增加用户控制(例如;可能是2或3个)
2)线程处理完成后,线程开始.(自动).

请帮助我.我该如何实现?

Hi,
How can i use Thread in User Control in C#.net?
In my Task;
In my User control :
The following are the Controls in User Control:
1) 2 Picture Box [1)Start 2)Stop]
2) 1 Picture Box [Connected]
3) 2 Picture Box [ 1) Online 2)offline]
4) 1 Picture Box [ Processing Picture ]
5) Label [Byte -- file Transferring byte ]
5) Timer (If it is Needed)

Processing :
Thread will start when User clicks a Start button
Thread will stop when User clicks a Stop button
online Picture views when user clicks a Start button
offline Picture views when user clicks a Stop button
file transferring bytes display in the Label.

Note:
1) user control can be increased (like ;It may 2 or 3 like)
2) Thread start when Thread Processing is finished.(it is automatically ).

Please help me .How can i achieve this?

推荐答案

您看到的,问题是:这个问题不完全是一个问题.

这是某些设计的骨架.您喜欢询问问题,但实际上您首先要声明:这是我的设计,我想完成它".问题是:如何?".这不是一个好方法:如果您知道已经设想的设计部分是好的,那是有道理的,但是如果您还不知道如何完成它,怎么知道呢?

你听懂了吗?如果我必须跟随你的带领,我们将进行巡回演出.问题是:您开始从错误的角度来解决问题.

这是我的建议:您需要将控制与线程完全脱钩.您可以在本文中找到一些动机: http://en.wikipedia.org/wiki/Loose_coupling [ ^ ].

而且,UI开发与代码的其他部分隔离的要求比任何其他领域的要求都更为重要.这是因为UI库有很大的不同,尤其是在不同的平台上,并且彼此之间非常不兼容.它们也经常被替换,因此通常针对不同的表示基础"和框架"重新实现相同的功能.

因此,您需要开发两个完全独立的部分:

1)线程包装器实现线程行为的某些功能,或多或少是专门的,具体取决于所需的灵活性.它可能会实现即用型行为,或者需要使用子类进行进一步的专门化.我提供了此类包装的示例代码,并解释了为什么它们在我过去的解决方案中非常重要:
如何将ref参数传递给线程 [ ^ ],
启动后更改线程(生产者)的参数 [ ^ ].

2)您可以从按钮的功能中抽象控制.您需要公开在按钮按下时引发的事件(并且您可以隐藏诸如Click的内部事件.您的事件将类似于StartPressedStopPressed.您的功能应包括在按下另一个按钮时启用/禁用按钮等

完成此操作后,您将在某些绑定代码中使用这两个部分,这会将事件处理程序添加到用户控件的事件的调用列表中.这些事件处理程序应控制线程的启动等.

现在是奖励建议:我建议您学习并分析以下建筑模式( ^ ]):

MVVM —模型视图视图模型,
http://en.wikipedia.org/wiki/Model_View_ViewModel [> http://en.wikipedia.org/wiki/Model-view-controller [ ^ ])),

MVA —模型视图适配器,
http://en.wikipedia.org/wiki/Model–view–adapter [ ^ ],

MVP —模型视图呈现器,
> http://en.wikipedia.org/wiki/Model-view-presenter [ ^ ].
请注意这些架构的动机.如果您了解它,就可以提出更好的设计思路.

—SA
You see, the problem is: this question is not exactly a question.

This is a skeleton of some design. You are like asking question, but in fact you are making a statement first: "this is my design, I want to complete it". And the question is: "how?". This is not a good approach: it would make sense if you knew that the part of your design you already envisioned was good, but how could you know that if you don''t yet know how to complete it?

Do you follow me? We would be making rounds if I had to follow your lead. The problem is: you started to attack a problem from a wrong side.

Here is what I would suggest: you need to decouple control from threading completely. You can find some motivation in this article: http://en.wikipedia.org/wiki/Loose_coupling[^].

What''s more, UI development requirement of isolation from other parts of the code is more important than that for any other fields. This is because there are UI libraries are very different, especially on different platforms and are very incompatible with each other; they are also often replaced, so the same functionality is often re-implemented for different "presentation foundations" and "frameworks".

So, you need to develop two completely independent parts:

1) Thread wrapper implementing some functionality of the behavior of the thread, more or less specialized, depending on required flexibility. It might implement ready-to-use behavior or require further specialization using subclassing. I provided the sample codes of such wrappers and explained why they are very important in my past solutions:
How to pass ref parameter to the thread[^],
change paramters of thread (producer) after it started[^].

2) You control abstracted from the functionality of the buttons. You need to expose events you raise on the button presses (and you can hide internal events like Click. Your events would be like StartPressed, StopPressed. Your functionality should include enable/disable of buttons when another button is pressed, etc.

When this is done, you use both parts in some binding code which adds the event handlers to the invocation lists of the events of your user control. These event handlers should control thread''s start, etc.

Now a bonus advice: I suggest you learn and analyze applicability of the following architectural patterns (http://en.wikipedia.org/wiki/Architectural_pattern_(computer_science)[^]):

MVVM — Model View View Model,
http://en.wikipedia.org/wiki/Model_View_ViewModel[^],

MVC — Model-View-Controller,
http://en.wikipedia.org/wiki/Model-view-controller[^]),

MVA — Model-View-Adapter,
http://en.wikipedia.org/wiki/Model–view–adapter[^],

MVP — Model-View-Presenter,
http://en.wikipedia.org/wiki/Model-view-presenter[^].
Pay attention for the motivation of those architectures. If you understand it, you would be able to create better design ideas.

—SA


这篇关于在C#.net的用户控件中使用线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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