グローバルに宣言された2つのスレッドがあります。フォームロード時に2つのスレッドを開始します。終了ボタンをクリックします。私はパイプを作成しているwhileループと他のスレッドで接続を待っている私はバックグラウンドでDataGridを更新しています形。C#スレッドで保護されたメモリにアクセスしようとしたときにエラーが発生しました
私の質問が十分ではありませんが、コードが非常に大きい場合は、何が起こっているか考えてください。
おかげ
フォーム負荷のコードは
private void frmStatus_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
RefreshStatus = new Thread(RefreshStatusForm);
RefreshStatus.IsBackground = true;
RefreshStatus.Start();
th = new Thread(this.CreatePipe);
th.IsBackground = true;
th.Start();
}
ボタン上にあるが
private void btnOk_Click(object sender, EventArgs e)
{
try
{
System.Object lockThis = new System.Object();
lock (lockThis)
{
bCheckVal = false;
if (!this.bComplete)
stopOperation();
Thread.Sleep(1000);
////////These are the lines in which i get the error
if (!th.IsAlive)
{
th.Join();
}
else
{
th.Abort();
}
this.bRefresh = false;
Thread.Sleep(1000);
if (RefreshStatus.IsAlive)
RefreshStatus.Abort();
else
RefreshStatus.Join();
/////////////////
this.bFlag = true;
}
this.Close();
}
catch (System.Exception ex)
{
}
}
にスレッドによって実行されるコードをクリックしてフォームを閉じるためのコードである
public void CreatePipe()
{
try
{
PipeSecurity pipeSa = new PipeSecurity();
pipeSa.SetAccessRule(new PipeAccessRule("Everyone",
PipeAccessRights.ReadWrite, AccessControlType.Allow));
NamedPipeServerStream pipeServer = new NamedPipeServerStream(
this.strguid, // The unique pipe name.
PipeDirection.InOut, // The pipe is bi-directional
NamedPipeServerStream.MaxAllowedServerInstances);
string strMessage = string.Empty;
char[] bRequest = new char[BUFFER_SIZE];// Client -> Server
int cbBytesRead, cbRequestBytes;
StreamReader sr = null;
byte[] bReply; // Server -> Client
int cbBytesWritten, cbReplyBytes;
int icount = 0;
List<string> lsRead = new List<string>();
cbBytesRead = 0;
CopyFileThread = new Thread(this.CopyFile);
CopyFileThread.IsBackground = true;
CopyFileThread.Start();
bool bflag = false;
while (true)
{
this.bComplete = false;
bWait = true;
try
{
pipeServer.WaitForConnection();
sr = new StreamReader(pipeServer);
//string message = sr.ReadToEnd();
bWait = false;
cbRequestBytes = BUFFER_SIZE;
string pipeData = string.Empty;
pipeData = sr.Read(bRequest, 0, 255).ToString().Trim();
strMessage = new string(bRequest);
strMessage = strMessage.Replace("\0", string.Empty);
if (strMessage.Contains("Aborted"))
{
if (pipeServer.IsConnected)
{
pipeServer.Flush();
pipeServer.Disconnect();
}
break;
}
else
if (strMessage.Contains("Completed"))
{
if (progressBar1.InvokeRequired)
{
strPercent = "100%";
}
if (pipeServer.IsConnected)
{
pipeServer.Flush();
pipeServer.Disconnect();
}
this.bComplete = true;
break;
}
// 26 dec 2011 Comment code //////
if (strMessage == "")
{
progressBar1.Visible = false;
progressBar2.Visible = true;
}
else
{
progressBar1.Visible = true;
progressBar2.Visible = false;
}
//// 26 dec 2011 Comment code //////
string[] strStatusMessages = strMessage.Trim().Split(',');
// 26 dec 2011 Comment code //////
pipeServer.Flush();
pipeServer.Disconnect();
}
catch (System.Exception ex)
{
//MessageBox.Show(ex.ToString());
}
}
pipeServer.Close();
if (CopyFileThread.IsAlive)
CopyFileThread.Abort();
else
CopyFileThread.Join();
MessageBox.Show("Exiting from createpipe 2 Thread :{0}", Thread.CurrentThread.ToString());
return;
}
catch (Exception ex)
{
return;
}
}
//////////////////////////////
ボイドRefreshStatusForm(){
while (bRefresh)
{
if (iRefresh == 1)
{
GetRefresh();
iRefresh = 0;
}
else if (iRefresh == 2)
break;
Thread.Sleep(1000);
}
MessageBox.Show("Exiting from RefreshStatusForm 2 Thread :{0}", Thread.CurrentThread.ToString());
}
をので*非常に大きな*でないコードを投稿する - 問題を示して短いものの、完全なプログラムに*だけ*あなたのアプリケーションの関連ビットを抽出します。 (途中で "バックグラウンドフォーム"が意味することは明確ではない) –
エラーが発生した行を分離することはできますか? – robob
////////これは、エラーが発生する行です。 if(!th.IsAlive) { th.Join(); } else { th.Abort(); } this.bRefresh = false; Thread.Sleep(1000); if(RefreshStatus.IsAlive) RefreshStatus.Abort(); else RefreshStatus.Join(); これは行で、私は両方のスレッドをグローバルに宣言しています。 – kunal