Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8494061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:11:48+00:00 2026-06-10T23:11:48+00:00

I have this project in which i am calling a web service, receiving rates

  • 0

I have this project in which i am calling a web service, receiving rates from the web service, processing it and displaying it in appropriate text boxes,

As you can guess the waiting for response from web service, and processing is too much work on a single thread, and the app gets unresponsive every once in a while so i had to use multi-theading to make the app more responsive. I am naive to multi-threading and i did something like this:
a thread that makes the webservice calls:

Webservicethread = new Thread(() => RefreshRates(marketId1));
            Webservicethread.Start();

this thread in turn calls another thread to do the heavy processing part:

t = new Thread(() => fillData(response.Length - 1, response));
                t.Start();

The problem is that this form is supposed to have a no. of instances running in parallel at the same time, which was working fine untill i tried to apply multithreading in my form,

now that i have tried to multithread it, running a second or a third instance heavily effects the working of all other instances of the app, and it totally freezes after i open the third instance.

I am totally confused what could possibly be done, some1 please help me out here…..:/

the filldata() method extracts the rates from the webservice response and fills them in the respective textboxes

*EDIT

here is my complete code, sorry as it gets too lengthy….

As suggested by Aaleksey, i profiled my code with ANTS profiler, filldata() is consuming even more time than th webservice call.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BetRates_NS.com.betfair.api;
using BetRates_NS.BFExchangeService;
using System.Net;
using System.Threading;

namespace BetRates_NS
{
    public partial class Rates : Form
    {
        public Rates()
        {
            InitializeComponent();
        }
        Thread t;
        Thread Webservicethread;
        private BFGlobalService bfg1 = new BFGlobalService();
        private BFExchangeService.BFExchangeService bfg2 = new BetRates_NS.BFExchangeService.BFExchangeService();
        int marketId1 = 0;
        public int MarketId1
        {
            get { return marketId1; }
            set { marketId1 = value; }
        }
        string match_name="";
        public string Match_name
        {
            get { return match_name; }
            set { match_name = value; }
        }
        string match_date = "";
        public string Match_date
        {
            get { return match_date; }
            set { match_date = value; }
        }
        string sessionToken = "";
        public string SessionToken
        {
            get { return sessionToken; }
            set { sessionToken = value; }
        }
        int max_len = 0;
        public int Max_len
        {
            get { return max_len; }
            set { max_len = value; }
        }
        bool inPlay=false;

        protected override bool ShowWithoutActivation
        {
            get { return true; }
        }
        public void ClearTextBoxes(Control control)
        {
            foreach (Control c in control.Controls)
            {
                if (c is TextBox)
                {
                    ((TextBox)c).Clear();
                }
                if (c.HasChildren)
                {
                    ClearTextBoxes(c);
                }
            }
        }

        private void RefreshRates(int marketid_temp)
        {
            try
            {
                //ClearTextBoxes(this);
                BFExchangeService.APIRequestHeader header2 = new BetRates_NS.BFExchangeService.APIRequestHeader();
                header2.clientStamp = 0;
                header2.sessionToken = sessionToken;

                //**************************** GET PRICES ****************************

                GetMarketPricesCompressedReq price_req1 = new GetMarketPricesCompressedReq();
                price_req1.header = header2;
                price_req1.marketId = marketId1;
                price_req1.currencyCode = "GBP";
                GetMarketPricesCompressedResp price_resp = new GetMarketPricesCompressedResp();
                price_resp = bfg2.getMarketPricesCompressed(price_req1);
                string prices = price_resp.marketPrices;
                string[] Parts = prices.Split(':');
                is_InPlay(Parts[0]);
                t = new Thread(() => fillData(Parts.Length - 1, Parts));
                t.Start();
                //fillData(Parts.Length - 1, Parts);
            }
            catch (Exception ep)
            {
                //MessageBox.Show(ep.Message);
            }
        }

        private void is_InPlay(string temp)
        {
            string[] check = temp.Split('~');
            if (Convert.ToInt32(check[3]) != 0)
            {
                inPlay = true;
                pictureBox2.Visible = true;
                ToolTip t1 = new ToolTip();
                t1.SetToolTip(this.pictureBox2, "InPlay");
            }
            else
            {
                inPlay = false;
                pictureBox2.Visible = false;
            }
        }

        private void Rates_Load(object sender, EventArgs e)
        {
            this.Text = match_name;
            //lbl_match.Text = match_name;
            BFExchangeService.APIRequestHeader header2 = new BetRates_NS.BFExchangeService.APIRequestHeader();
            header2.clientStamp = 0;
            header2.sessionToken = sessionToken;

            GetMarketReq market_req1 = new GetMarketReq();
            market_req1.header = header2;
            market_req1.marketId = Convert.ToInt32(marketId1);
            GetMarketResp market_resp1= bfg2.getMarket(market_req1);
            Market market1 = market_resp1.market;
            Runner[] runners_list = market1.runners;
            makeMyRows(runners_list.Length);
            this.Height=60+(36*runners_list.Length);
            int max_length = 0;
            for (int i = 0; i < runners_list.Length; i++)
            {
                if (max_length < runners_list[i].name.Length)
                {
                    max_length = runners_list[i].name.Length;
                }
                int label_counter = i + 1;
                string ctrl_name = "lbl" + label_counter.ToString();
                Label temp = null;
                foreach (Control ctl in this.Controls)
                {
                    if ((ctl is Label) && (ctl.Name == ctrl_name))
                   { temp = (Label)ctl; }
                }
                if (temp != null)
                { temp.Text = runners_list[i].name; }
            }
            //max_len = max_length;
            //MessageBox.Show(max_len.ToString());
            System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
            myTimer.Interval = 200;
            myTimer.Tick += new EventHandler(myTimer_Tick);
            myTimer.Enabled = true;
            this.Focus();
            if (this.VerticalScroll.Visible == true)
            {
                this.Width = this.Width + 24;
            }
        }

        private void myTimer_Tick(object sender, EventArgs e)
        {
            Webservicethread = new Thread(() => RefreshRates(marketId1));
            Webservicethread.Start();
            //RefreshRates(marketId1);
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            this.Refresh();
            this.RefreshRates(marketId1);
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }




        //public delegate void FillData1(int count1, string[] parts1);
        private void fillData(int count,string[] Parts)
        {
            for (int i = 1; i <= count; i++)
            {
                string[] subParts = Parts[i].Split('|');
                for (int j = 1; j <= subParts.Length-1; j++)
                {
                    string[] Fields = subParts[j].Split('~');
                    for (int k = 1; k <= 6; k++)
                    {
                        string compare_txt = "";
                        string ctrl_name = "txt" + i.ToString() + k.ToString();
                        TextBox temp = null;// = (TextBox)findCtrl(ctrl_name);
                        foreach (Control ctl in this.Controls)
                        {
                            if ((ctl is TextBox) && (ctl.Name == ctrl_name))
                            { temp = (TextBox)ctl; }
                        }
                        if (j == 1)
                        {
                            switch (k)
                            {
                                case 1:
                                    compare_txt = temp.Text;
                                    //temp.Text = "";
                                    temp.Invoke(new AddText1(AddText), new object[] { ctrl_name, "" });
                                    if (Fields.Length >= 9)
                                    {
                                        if (!string.IsNullOrEmpty(Fields[8]))
                                        {
                                            this.Invoke(new AddText1(AddText), new object[] { ctrl_name, Fields[8] });
                                            //temp.Text = Fields[8];
                                        }
                                    }
                                    if (Fields.Length >= 10)
                                    {
                                        if (!string.IsNullOrEmpty(Fields[9]))
                                        {
                                            this.Invoke(new AddText1(AddText), new object[] { ctrl_name, temp.Text + "\r\n£" + Fields[9] });
                                            //temp.Text = temp.Text + "\r\n£" + Fields[9]; 
                                        }
                                    }
                                    if (temp.Text != compare_txt)//check if value has changed, if yes blink
                                    { temp.BackColor = Color.LightGreen; }
                                    else
                                    { temp.BackColor = Color.Lavender; }
                                    break;
                                case 2:
                                    compare_txt = temp.Text;
                                    this.Invoke(new AddText1(AddText), new object[] { ctrl_name, "" });
                                    //temp.Text = "";
                                    if (Fields.Length >= 5)
                                    {
                                        if (!string.IsNullOrEmpty(Fields[4]))
                                        {
                                            this.Invoke(new AddText1(AddText), new object[] { ctrl_name, Fields[4] });
                                            //temp.Text = Fields[4];
                                        }
                                    }
                                    if (Fields.Length >= 6)
                                    {
                                        if (!string.IsNullOrEmpty(Fields[5]))
                                        {
                                            this.Invoke(new AddText1(AddText), new object[] { ctrl_name, temp.Text + "\r\n£" + Fields[5] });
                                            //temp.Text = temp.Text + "\r\n£" + Fields[5];
                                        }
                                    }
                                    if (temp.Text == compare_txt)
                                    { temp.BackColor = Color.Thistle; }
                                    else
                                    { temp.BackColor = Color.LightGreen; }
                                    break;
                                case 3:
                                    compare_txt = temp.Text;
                                    this.Invoke(new AddText1(AddText), new object[] { ctrl_name, "" });
                                    //temp.Text = "";
                                    if (Fields.Length >= 1)
                                    {
                                        if (!string.IsNullOrEmpty(Fields[0]))
                                        {
                                            this.Invoke(new AddText1(AddText), new object[] { ctrl_name, Fields[0] });                                            
                                            //temp.Text = Fields[0];
                                        }
                                    }
                                    if (Fields.Length >= 2)
                                    {
                                        if (!string.IsNullOrEmpty(Fields[1]))
                                        {
                                            this.Invoke(new AddText1(AddText), new object[] { ctrl_name, temp.Text + "\r\n£" + Fields[1] });                                                                                        
                                            //temp.Text = temp.Text + "\r\n£" + Fields[1];
                                        }
                                    }
                                    if (temp.Text == compare_txt)
                                    { temp.BackColor = Color.FromArgb(192, 192, 255); }
                                    else
                                    { temp.BackColor = Color.LightGreen; }
                                    break;
                            }
                        }
                        else if (j == 2)
                        {
                            switch (k)
                            {
                                case 4:
                                    compare_txt = temp.Text;
                                    this.Invoke(new AddText1(AddText), new object[] { ctrl_name, "" });
                                    //temp.Text = "";
                                    if (Fields.Length >= 1)
                                    {
                                        if (!string.IsNullOrEmpty(Fields[0]))
                                        {
                                            this.Invoke(new AddText1(AddText), new object[] { ctrl_name, Fields[0] });
                                            //temp.Text = Fields[0]; 
                                        }
                                    }
                                    if (Fields.Length >= 2)
                                    {
                                        if (!string.IsNullOrEmpty(Fields[1]))
                                        {
                                            this.Invoke(new AddText1(AddText), new object[] { ctrl_name, temp.Text + "\r\n£" + Fields[1] });                                                                                                                                    
                                            //temp.Text = temp.Text + "\r\n£" + Fields[1];
                                        }
                                    }
                                    if (temp.Text == compare_txt)
                                    { temp.BackColor = Color.FromArgb(255, 192, 192); }
                                    else
                                    { temp.BackColor = Color.LightGreen; }
                                    break;
                                case 5:
                                    compare_txt = temp.Text;
                                    this.Invoke(new AddText1(AddText), new object[] { ctrl_name, "" });
                                    //temp.Text = "";
                                    if (Fields.Length >= 5)
                                    {
                                        if (!string.IsNullOrEmpty(Fields[4]))
                                        {
                                            this.Invoke(new AddText1(AddText), new object[] { ctrl_name, Fields[4] });
                                            //temp.Text = Fields[4]; 
                                        }
                                    }
                                    if (Fields.Length >= 6)
                                    {
                                        if (!string.IsNullOrEmpty(Fields[5]))
                                        {
                                            this.Invoke(new AddText1(AddText), new object[] { ctrl_name, temp.Text + "\r\n£" + Fields[5] });                                                                                                                                    
                                            //temp.Text = temp.Text + "\r\n£" + Fields[5]; 
                                        }
                                    }
                                    if (temp.Text == compare_txt)
                                    { temp.BackColor = Color.MistyRose; }
                                    else
                                    { temp.BackColor = Color.LightGreen; }
                                    break;
                                case 6:
                                    compare_txt = temp.Text;
                                    this.Invoke(new AddText1(AddText), new object[] { ctrl_name, "" });
                                    //temp.Text = "";
                                    if (Fields.Length >= 9)
                                    {
                                        if (!string.IsNullOrEmpty(Fields[8]))
                                        {
                                            this.Invoke(new AddText1(AddText), new object[] { ctrl_name, Fields[8] });
                                            //temp.Text = Fields[8];
                                        }
                                    }
                                    if (Fields.Length >= 10)
                                    {
                                        if (!string.IsNullOrEmpty(Fields[9]))
                                        {
                                            this.Invoke(new AddText1(AddText), new object[] { ctrl_name, temp.Text + "\r\n£" + Fields[9] });
                                            //temp.Text = temp.Text + "\r\n£" + Fields[9];
                                        }
                                    }
                                    if (temp.Text == compare_txt)
                                    { temp.BackColor = Color.Lavender; }
                                    else
                                    { temp.BackColor = Color.LightGreen; }
                                    break;
                            }
                        }
                    }
                }
            }
        }

        public delegate void AddText1(string ctrl_n, string text_a);
        private void AddText(string ctrl_name, string text_assign)
        {
            TextBox temp = null;// = (TextBox)findCtrl(ctrl_name);
            foreach (Control ctl in this.Controls)
            {
                if ((ctl is TextBox) && (ctl.Name == ctrl_name))
                { temp = (TextBox)ctl; }
            }
            temp.Text = text_assign;
        }

        private void makeMyRows(int count)
        {
            int X = 10, Y = 23;
            for (int i = 1; i <= count; i++)
            {
                Label label = new Label();
                label.Name = "lbl" + i.ToString();
                label.Location = new Point(X, Y);
                label.BackColor = Color.Transparent;
                label.Font=new Font("Calibri Sans-Serif", 10);
                label.Font = new Font(label.Font, FontStyle.Bold);
                label.ForeColor = Color.White;
                this.Controls.Add(label);
                X = X + 118;
                for (int j = 1; j <= 6; j++)
                {
                    TextBox tb = new TextBox();
                    tb.Name = "txt" + i.ToString() + j.ToString();
                    tb.ReadOnly = true;
                    tb.Location = new Point(X,Y);
                    tb.Size = new Size(55, 30);
                    tb.Font = new System.Drawing.Font("Arial, Helvetica, sans-serif", (float)7.5);
                    tb.TextAlign = HorizontalAlignment.Center;
                    tb.BorderStyle = BorderStyle.FixedSingle;
                    tb.Font = new Font(tb.Font, FontStyle.Regular);
                    tb.Multiline = true;
                    switch (j)
                    { 
                        case 1:
                            tb.BackColor = Color.Lavender;
                            break;
                        case 2:
                            tb.BackColor = Color.Thistle;
                            break;
                        case 3:
                            tb.BackColor = Color.FromArgb(192, 192, 255);
                            break;
                        case 4:
                            tb.BackColor = Color.FromArgb(255, 192, 192);
                            break;
                        case 5:
                            tb.BackColor = Color.MistyRose;
                            break;
                        case 6:
                            tb.BackColor = Color.Lavender;
                            break;

                    }
                    this.Controls.Add(tb);
                    X = X + 61;
                }
                X = 10; Y = Y + 36;
            }
        }

        //************preventing flicker*************
        int originalExStyle = -1;
        bool enableFormLevelDoubleBuffering = true;

        protected override CreateParams CreateParams
        {
            get
            {
                if (originalExStyle == -1)
                    originalExStyle = base.CreateParams.ExStyle;

                CreateParams cp = base.CreateParams;
                if (enableFormLevelDoubleBuffering)
                    cp.ExStyle |= 0x02000000;   // WS_EX_COMPOSITED
                else
                    cp.ExStyle = originalExStyle;

                return cp;
            }
        }

        private void TurnOffFormLevelDoubleBuffering()
        {
            enableFormLevelDoubleBuffering = false;
            this.MaximizeBox = true;
        }

        private void Rates_Shown(object sender, EventArgs e)
        {
            TurnOffFormLevelDoubleBuffering();
        }

        //******************************************

        private void pictureBox2_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.Refresh();
            RefreshRates(marketId1);
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.Refresh1));
        }

        private void pictureBox1_MouseEnter(object sender, EventArgs e)
        {
            pictureBox1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.Refresh_hover));
        }

        private void Rates_FormClosing(object sender, FormClosingEventArgs e)
        {
            t.Abort();
            Webservicethread.Abort();
        }

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:11:49+00:00Added an answer on June 10, 2026 at 11:11 pm

    This might help you

    1. Create a background thread which will call the web service
      asynchronously.

    2. Define a delegate which contains the result web service returns to
      you.

    3. When you receive the result, using delegate/event pass the result to
      the form.

    4. On the form display values received from event (use InvokeRequired)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Web Service project and a WPF project (which serves as a
I have a project which has a calling structure similar to this: main project/application
I have a project called Data which is a data layer. In this project,
I have a django project which is laid out like this... myproject apps media
I have a Visual Studio 2010 C# project which creates an .exe and this
Please excuse the noob-ish nature of this question. I have a project in which
This is a simplified task which I have to solve in real project. In
in my project i have one registration form which is developed in C#.net.to this
I have BaseView which implement UIViewController. Every view in project must implement this BaseView.
I have this project explained in Wiley-Embedded Signal Processing with the Micro Signal Architecture.2007

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.