Многопоточность + GUI - C#
Формулировка задачи:
Используя метод invoke класса Form я создал вечный цыкл, чтобы другой поток в нем (цыкле) выводил текущее время на Lablel.
Время выводит нормально, но вот через тот же вечный цикл к элементам формы я не имею доступа (закрыть, свернуть окно). Как можно решить эту траблу?
Если нужно, быдло-код кину дальше...
Решение задачи: «Многопоточность + GUI»
textual
Листинг программы
namespace Laba6.OS.UsingDll.cs
{
public partial class Form1 : Form
{
Assembly asm;
MyThread MyThrd;
dynamic Class1Example = null;
public Form1()
{
InitializeComponent();
LetsDoSomething();
}
void LetsDoSomething()
{
asm = Assembly.LoadFrom(@"C:\Users\Sergiy\Documents\Visual Studio 2010\Projects\TestLibrary\TestLibrary\bin\Debug\TestLibrary.dll");
Type[] types = asm.GetTypes();
foreach (Type t in types)
{
if (t.Name == "Class1")
{
ConstructorInfo[] Ci = t.GetConstructors();
foreach (ConstructorInfo c in Ci)
{
if (c.GetParameters().Length == 0)
{
Class1Example = c.Invoke(null);
}
}
}
}
}
void CreateThread()
{
MyThrd = new MyThread(label1, Class1Example);
}
private void Start_Button_Click(object sender, EventArgs e)
{
CreateThread();
}
}
class MyThread
{
delegate void InvokeDelegate();
InvokeDelegate MyDel;
Label label;
Thread thrd;
dynamic ClassExample;
public MyThread(Label l, dynamic c)
{
label = l;
ClassExample = c;
thrd = new Thread(start);
thrd.Start();
}
void start()
{
MyDel = new InvokeDelegate(InvokeMethod);
label.Parent.Invoke(MyDel);
}
void InvokeMethod()
{
MessageBox.Show(ClassExample.ShowTheTime().ToString());
while (true)
{
label.Text = ClassExample.ShowTheTime();
System.Threading.Thread.Sleep(5000);
label.Parent.Refresh();
}
}
}
}