| |

VerySource

 Forgot password?
 Register
Search
View: 588|Reply: 1

Extremely confused! !! !! !! Extremely confused! !! !! !!

[Copy link]

2

Threads

7

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-1-25 20:20:01
| Show all posts |Read mode
I use winform to call webservice, and I want to achieve asynchronous access (or multi-threading) on ​​the client.
I think there are three ways:
1. I used asynchronous access methods and events (GetMethodAsync and methodCompleted) of the proxy class.
In the Button1_Click of the form, call wsClient.GetMethodAsync () to start asynchronous access to the webservice. Use method parameters in methodCompleted to get the webservice return result. Then bind to the datagrid instantiated in the form. This way is ok.
code segment:
private MyPubsService pubsService; webservice proxy
private DataSet ds;
        public Form1 ()
        {
            InitializeComponent ();
            pubsService = new MyPubsService ();
            pubsService.GetAuthorsCompleted + = new GetAuthorsCompletedEventHandler (pubsService_GetAuthorsCompleted);
            this.FormClosing + = new FormClosingEventHandler (Form1_FormClosing);
        }

        void Form1_FormClosing (object sender, FormClosingEventArgs e)
        {
            pubsService.Dispose ();
        }

        void pubsService_GetAuthorsCompleted (object sender, GetAuthorsCompletedEventArgs e)
        {
            ds = (DataSet) e.Result;
            dataGridView1.DataSource = ds.Tables [0] .DefaultView;
           
        }

        private void button1_Click (object sender, EventArgs e)
        {
     
            pubsService.GetAuthorsAsync ();
           
        } * /
2. I want to use the BeginInvoke () and Endinvoke () methods of the proxy entity and the callback function.
   The dataset entity obtained in the callback function is no problem. But in the callback function, using
  When dataGrid1.datasource = ds.tables [0] .defaultview ;, an exception is reported at runtime
----------- Invalid inter-thread operation, access it from a thread that is not creating the datagrid1 control. ----
Be puzzled, please enlighten me!
code segment:
  using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Web.Services;
using WinFormWebService.WS;
using System.Threading;

namespace WinFormWebService
{
    public partial class Form1: Form
    {



        ServerWebService wbSData;
        private DataSet ds;
        delegateBindData delg;
        public Form1 ()
        {
            InitializeComponent ();
            wbSData = new ServerWebService ();
            delg = new delegateBindData (wbSData.GetBindData);

        }

        private void button1_Click (object sender, EventArgs e)
        {
            IAsyncResult ar = delg.BeginInvoke (new AsyncCallback (CallBackFn), delg);

        }
        void Form1_FormClosing (object sender, FormClosingEventArgs e)
        {
            if (wbSData.pubsService! = null)
            {
                wbSData.pubsService.Dispose ();
            }
        }
        void CallBackFn (IAsyncResult ar)
        {
            delg = (delegateBindData) ar.AsyncState;
            ds = delg.EndInvoke (ar);
            dataGrid1.DataSource = ds.Tables [0] .DefaultView;

        }
        public class ServerWebService
    {
        public MyPubsService pubsService;
        public ServerWebService ()
        {
            pubsService = new MyPubsService ();
        }
        public DataSet GetBindData ()
        {
            DataSet ds = pubsService.GetAuthors ();
            return ds;

        }
        
        
    }
    public delegate DataSet delegateBindData ();
}
3. Use the conventional thread method, thread.start () for asynchronous access. I didn't try this.
Reply

Use magic Report

2

Threads

7

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-2-15 13:15:01
| Show all posts
MyPubsService is the proxy class and getauthors is the webservice method
serverWebService is a custom class
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list