| |

VerySource

 Forgot password?
 Register
Search
View: 696|Reply: 5

Thread issues, expert advice! !! !! !! !!

[Copy link]

2

Threads

7

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-1-25 22:00: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.
MyPubsService is the proxy class and getauthors is the webservice method
serverWebService is a custom class
Reply

Use magic Report

0

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-2-16 23:45:02
| Show all posts
UI controls of different threads cannot access each other and must be called through invoke.
Reply

Use magic Report

0

Threads

9

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-2-18 01:00:01
| Show all posts
Agree upstairs, use hosting
Reply

Use magic Report

2

Threads

7

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-2-19 16:00:01
| Show all posts
Please give a specific plan
Reply

Use magic Report

0

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-2-19 19:30:02
| Show all posts
// The following code assumes a 'ListBox' and a 'Button' control are added to a form,
// containing a delegate which encapsulates a method that adds items to the listbox.

   public class MyThreadClass
   {
      MyFormControl myFormControl1;
      public MyThreadClass (MyFormControl myForm)
      {
         myFormControl1 = myForm;
      }

      public void Run ()
      {
         // Execute the specified delegate on the thread that owns
         // 'myFormControl1' control's underlying window handle.
         myFormControl1.Invoke (myFormControl1.myDelegate);
      }
   }
Reply

Use magic Report

2

Threads

7

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-2-20 14:45:01
| Show all posts
For example: the program just started, the datagrid is instantiated by the main thread, and a thread is started in an event method of the form. So is it impossible to access the datagrid in the delegate method of this thread? But data entities can. Is that so?
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