このプロジェクトでは、Webサービスを呼び出し、Webサービスから料金を受け取り、処理して適切なテキストボックスに表示しています。
Webサービスからの応答を待っていることを推測できますが、単一のスレッドでは処理が多すぎて、アプリがときどき応答しなくなるため、アプリの応答性を高めるためにマルチヘッダーを使用する必要がありました。私はマルチスレッドにナイーブで、次のようなことをしました:Webサービスを呼び出すスレッド:
Webservicethread = new Thread(() => RefreshRates(marketId1));
Webservicethread.Start();
このスレッドは、別のスレッドを呼び出して、重い処理部分を実行します。
t = new Thread(() => fillData(response.Length - 1, response));
t.Start();
問題は、このフォームには番号がないことになっていることです。同時に並行して実行されているインスタンスの数は、フォームにマルチスレッドを適用しようとするまでは正常に機能していました。
マルチスレッド化を試みたので、2番目または3番目のインスタンスを実行すると、アプリの他のすべてのインスタンスの動作に大きな影響があり、3番目のインスタンスを開くと完全にフリーズします。
私はおそらく何ができるのか完全に混乱しています、some1はここで私を助けてください.....:/
filldata()メソッドは、Webサービスの応答からレートを抽出し、それぞれのテキストボックスに入力します
*編集
これが私の完全なコードです。長くなりすぎて申し訳ありません。
Aalekseyが提案したように、ANTSプロファイラーを使用してコードのプロファイリングを行いました。filldata()は、Webサービスの呼び出しよりもさらに多くの時間を消費しています。
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();
}
}
}