0

I have a scenario, in witch i need to open new WPF window form Web Forms page, and ten bind to properities in that window and display it in update panel, so the data on web page, changes with user input in WPF window.

I tried something like this:

public partial class WorkerPanel : System.Web.UI.Page
{
    private MainWindow _mainWindow;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            _mainWindow = new MainWindow();
            _mainWindow.Show();
        }
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        rptTransactions.DataSource = _mainWindow.Distributors;
        rptTransactions.DataBind();
    }
}

But it gives me the following error:

The calling thread must be STA, because many UI components require this.

Accoridingly to this question https://stackoverflow.com/questions/2329978 i changed my code to:

            Thread t = new Thread(() =>
            {
                _mainWindow = new MainWindow();
                _mainWindow.Show();
                System.Windows.Threading.Dispatcher.Run();
            });
            t.SetApartmentState(ApartmentState.STA);
            t.IsBackground = true;
            t.Start();

With this, my web page and WPF window loads fine, but i cannot bind to this window properties, as it runs in new thread. Is such binding even possible or I should take different approach ?

Draeggiar
  • 19
  • 7
  • Are you aware this would only happen on the web server? Anyone connecting to your page over a network will not see the WPF window. – Crowcoder Mar 19 '17 at 14:40
  • To answer your question, take a different approach. Create a web service that a client (WPF, for instance) can post to. You can do so with a simple HttpHander (.asmx) if you want to keep it in webforms. – Crowcoder Mar 19 '17 at 14:44
  • I'm aware that this will only occur on server, it have a purpose. Thank you for suggiestion, will try with web service then. – Draeggiar Mar 19 '17 at 15:59

1 Answers1

0

I managed to do it my way using callbacks. For anyone with the same problem here's what i did:

First i added a delegate in my WPF window class

public delegate void DistributorsDataCallback(List<DistributorHandler> distributors);
private DistributorsDataCallback _callback;

then i created a new constructor for my window accepting this delegate as a parameter

        public MainWindow(DistributorsDataCallback callbackDelegate)
    {
        InitializeComponent();
        InitializeDistributors();
        _callback = callbackDelegate;
    }

and somwhere in code i call it with data i want to pass

_callback(Distributors);

On my Web Forms page:

                Thread t = new Thread(() =>
            {                    
                MainWindow _mainWindow = new MainWindow(GetDistributorsData);
                _mainWindow.Show();
                System.Windows.Threading.Dispatcher.Run();
            });
            t.SetApartmentState(ApartmentState.STA);
            t.IsBackground = true;
            t.Start();

And now data can be accesed without any problems in GetDistributorsData function. This solution should work in any multi-thread application.

Draeggiar
  • 19
  • 7