سؤال

I have a window(say main window) with a frame which has a page in it. A button on the page opens another window(say popup window). Now i am trying to invoke a method in the main window from a button on the popup window. The method has to be multi-threaded, i had a similar solution running in windows forms but i keep getting the calling thread must be STA because many UI components require this in WPF.

The method on the page which opens the popup window modally

Scripts showStocks = new Scripts();
            showStocks.ShowInTaskbar = false;
            showStocks.ShowDialog();
            if (showStocks.DialogResult==true)
            {
                Window1 wd1 = new Window1();
                wd1.doneDeal();
            }

Here window1 is our main window. The doneDeal method is

public void doneDeal()
        {
            // **Some Code**
            BackgroundWorker wworks1 = new BackgroundWorker();
            wworks1.DoWork += Tickes;
            wworks1.RunWorkerCompleted += Tickes2;
            wworks1.RunWorkerAsync();

           // Page1 pg1 = frame1.Content as Page1;
            //NextPrimeDelegate dd=new NextPrimeDelegate(okreport);
           // pg1.addScriptBtn.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
               // new NextPrimeDelegate(okreport));
            //startStopButton.Dispatcher.BeginInvoke(
                //    DispatcherPriority.Normal,
                //    new NextPrimeDelegate(CheckNextNumber));

            //new Thread(() => Tick(stock, rowID, exchange)) { IsBackground = false }.Start();


        }

Finally the method that i am trying to run in the background

public void Tickes(object sender, DoWorkEventArgs e)
        {

         }

Also i want to populate a gridview from the result of the tickes method, this will be looping and running over and over in the background but periodically returning data to be added to the grid. SHould i do that in the progress update event ? Have tried a lot to wrap my head around the dispatcher and background worker in wpf but am failing to understand the STA apartment state bit. If someone can help me to get my tickes method going or point me in the right direction, i would be very very thankful.

هل كانت مفيدة؟

المحلول

Well i finally was able to solve the STA thread problem am posting the answer just in case somebody comes across a similar problem in the future.

    public void doneDeal()
    {
        if (StockData.flag == 1)
        {
            row1 = table.NewRow();
            row1[col1] = "";
            row1[col2] = "";
            row1[col3] = "";
            row1[col4] = "";
            row1[col5] = "";
            row1[col6] = "";
            row1[col7] = "";
            row1[col8] = "";
            row1[col9] = "";
            row1[col10] = "";
            row1[col11] = "";
            row1[col12] = "";
            table.Rows.Add(row1);
            string stock = StockData.stock;
            int rowID = (table.Rows.Count - 1);
            string exchange = StockData.exchange;
            Thread bh = new Thread(delegate()
            {
                Tick7(stock, rowID, exchange);
            });
            bh.SetApartmentState(ApartmentState.STA);
            bh.IsBackground = true;
            bh.Start();
            StockData.flag = -1;
        }
    }

The Tick7 method which is being called is declared like this

    [STAThread]
    public void Tick7(string stock, int rowID, string exchange)
    {

        int rowNum = rowID;
        int counter = -1;

        deletecounter = StockData.deletecounter;
        Thread.CurrentThread.Name = StockData.stock;
        .
        .
        .
     }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top