Witam,
Mam pewnien problem. Robię aplikację, gdzie muszę mieć serwer razem z gui w jednej aplikacji (tak sobie wymyślili...), i przez to mam Forma z aspNetem.
W przekazywaniu danych itp nie mam problemów, invokuje delegaty.
Problem mam gdy tam chcę wrzucić (2. wątek)webFormsa lub FlashPlayera z obiektów COM. Wyrzuca wyjątek podczas inicjalizacji, że dostęp z innego forma(tworzę go na pewno w tym samym wątku, chyba że on sam w sobie jakieś śmieszne rzeczy robi).
Macie jakieś pomysły jak można to rozwiązać?
Kod
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Net;
using System.Threading;
using System.Windows.Forms;
namespace TWIN_API.Server_GUI.Presentation
{
public class MainForm : Form //must be Singleton
{
internal System.Windows.Forms.Timer DelegateHandler;
private System.ComponentModel.IContainer components;
internal PictureBox pictureBox1;
internal ProgressBar progressBar1;
private WebBrowser webBrowser1;
internal Label TextShower;
public void ChangeProgress(int id)
{
this.progressBar1.Value = id;
}
public void Visiblity(Control control)
{
TextShower.Visible = false;
pictureBox1.Visible = false;
control.Visible = true;
}
public MainForm()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.DelegateHandler = new System.Windows.Forms.Timer(this.components);
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.TextShower = new System.Windows.Forms.Label();
this.webBrowser1 = new System.Windows.Forms.WebBrowser();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(12, 12);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(300, 300);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// progressBar1
//
this.progressBar1.Dock = System.Windows.Forms.DockStyle.Top;
this.progressBar1.Location = new System.Drawing.Point(0, 0);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(643, 10);
this.progressBar1.TabIndex = 2;
//
// TextShower
//
this.TextShower.AccessibleDescription = "TextShower";
this.TextShower.AccessibleName = "TextShower";
this.TextShower.Location = new System.Drawing.Point(9, 26);
this.TextShower.Name = "TextShower";
this.TextShower.Size = new System.Drawing.Size(300, 300);
this.TextShower.TabIndex = 3;
this.TextShower.Text = "TextShower";
this.TextShower.Click += new System.EventHandler(this.TextShower_Click);
//
// webBrowser1
//
this.webBrowser1.Location = new System.Drawing.Point(160, 399);
this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
this.webBrowser1.Name = "webBrowser1";
this.webBrowser1.Size = new System.Drawing.Size(250, 250);
this.webBrowser1.TabIndex = 4;
this.webBrowser1.Url = new System.Uri("https://www.youtube.com/v/qgeaoW55Pks", System.UriKind.Absolute);
//
// MainForm
//
this.ClientSize = new System.Drawing.Size(643, 733);
this.ControlBox = false;
this.Controls.Add(this.webBrowser1);
this.Controls.Add(this.TextShower);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.progressBar1);
this.Name = "MainForm";
this.Load += new System.EventHandler(this.MainForm_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void MainForm_Load(object sender, EventArgs e)
{
}
private void axShockwaveFlash2_Enter(object sender, EventArgs e)
{
}
private void TextShower_Click(object sender, EventArgs e)
{
}
}
Kontroler ;
public class GuiController
{
protected static GuiController GuiControllerInstance {get; private set; }
protected Task Task;
public static GuiController GetGuiController()
{
return GuiControllerInstance;
}
public static bool TryStartGui()
{
if (GuiControllerInstance != null)
return false;
GuiControllerInstance = new GuiController();
return true;
}
public delegate void GuiDoIt(MainForm form);
virtual public void GuiInvoke(GuiDoIt GuiInvoker)
{
HandlerMainForm.Invoke(GuiInvoker, HandlerMainForm);
}
protected MainForm HandlerMainForm;
protected void FormLoop()
{
Application.Run(HandlerMainForm);
}
public void Start()
{
Task = Task.Factory.StartNew(FormLoop);
}
/// <summary>
/// Stop the task NOTE!! Use Dispose instead at this implementation
/// </summary>
public virtual void Stop()
{
throw new NotSupportedException();
}
/// <summary>
/// It end task asap
/// </summary>
public void Dispose()
{
Task.Dispose();
}
internal GuiController()
{
HandlerMainForm = new MainForm();
Start();
}
}
Uruchamianie
GuiController.TryStartGui();
Bez tych toolsów wszystko działa, jak właduję albo WebBrowsera albo Flasha to się psuje.
Gdy tworzę forma w głównym wątku "Application.Run(new MainForm());", to działa.