Потоки в WinForm - C#
Формулировка задачи:
Ребята, подскажите пожалуйста
Имеем 3 класса:
1 Класс - Program, который содержит метод Main в нём создаются 2 объекта:
Presenter - класс управления
и объект gameForm - соответственно унаследованный от Form
Собственно вопрос можно ли из Presenter запустить в отдельном потоке gameForm:
Вот такой код вылетает с ошибкой.
2 класс:
И не замысловатый интерфейс:
InvalidOperationException.
Недопустимая операция в нескольких потоках: попытка доступа к элементу управления '' не из того потока, в котором он был создан.
using System;
using System.Windows.Forms;
using Arachne3.Presenter;
using Arachne3.View;
namespace Arachne3
{
class Program
{
public static int Main(string[] args)
{
ArachnePresenter presenter = new ArachnePresenter();
GameWindow gameForm = new GameWindow(ArachnePresenter.Width,
ArachnePresenter.Height,
ArachnePresenter.Name);
presenter.GetView(gameForm);
presenter.Start(gameForm);
return 0;
}
}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Arachne3.View;
using System.Threading;
using System.Windows.Forms;
namespace Arachne3.Presenter
{
class ArachnePresenter
{
public static int Width, Height;
public static string Name;
private IView _view;
private bool running;
public ArachnePresenter()
{
Width = 480;
Height = 800;
Name = "Untitled";
}
public void GetView(IView view)
{
_view = view;
}
public void Start(Form gameForm)
{
Thread GameThread = new Thread( new ThreadStart(ViewRun));
GameThread.Start();
_view.Render();
}
private void ViewRun()
{
Application.Run((Form)_view);
}using System;
using System.Drawing;
namespace Arachne3
{
interface IView
{
void Render();
}
}Решение задачи: «Потоки в WinForm»
textual
Листинг программы
using System;
namespace Project2
{
interface IView
{
void Render();
bool BeRender { get; set; }
}
}