0

I have two threads that are declared global.On the form load i start the two threads.On the exit button click i am trying to abort these two threads if they are alive or else join.But i get this error "Attempted to read write protected memory" and the applications hangs sometimes.In one thread i am creating a pipe and waiting for connection in a while loop and in the other thread i am updating a datagrid on the background form.

Any ideas what is happening.I can post the code if my question is not clear enough but the code is very big.

Thanks

The code for form load is

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();
}

The code for closing the form is on a button click

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)
    {
    }
}

The code which is executed by the threads

        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;
        }
    }

//////////////////////////////

void 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());
    }
3
  • 1
    So post code which isn't very big - extract just the relevant bits of your application into a short but complete program which demonstrates the problem. (It's not clear what you mean by a "background form" by the way.) Commented Jan 6, 2012 at 7:23
  • Can you isolate the rows where the error is risen? Commented Jan 6, 2012 at 7:26
  • ////////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(); These are the lines and i have declared both the threads globally. Commented Jan 6, 2012 at 7:41

2 Answers 2

1

You don't need to abort or join the thread. It is better to just let them finish normally. In your thread that does "waiting for connection in a while loop" have the while loop condition set as a bool and when you exit the program set the bool to false. Do something similar with the other thread as well.

EDIT1 So first of all:

while (true)
            {
                this.bComplete = false;
                bWait = true;

                try
                {

instead should be something like

while(Running) {

You use

pipeServer.WaitForConnection(); 

which is going to block the thread so I would change it an async task

    BeginWaitForConnection(
    AsyncCallback callback,
    Object state
)

but if you do not want to do that then add a timeout every so often.

Then in

void btnOk_Click

all you need to do is set

Running = false;
bRefresh = false; 

then the threads will finish themselves.

EDIT2

Set it up by using:

AsyncCallback myCallback = new AsyncCallback(AsyncPipeCallback);  
pipeServer.BeginWaitForConnection(myCallback, null); 

Add this method:

private void AsyncPipeCallback(IAsyncResult Result)  
{   
    try  
    {  
        pipeServer.EndWaitForConnection(Result);  
            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 { }
    if(Running)
        pipeServer.BeginWaitForConnection(myCallback, null); 
} 

Now when a connection occurs AsyncPipeCallback is called. Then when it finishes it check to see if it is still Running if it is it BeginWaitForConnection again.

In btnOk_Click change it to this:

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);

            Running  = false;
            bRefresh = false;

            this.bFlag = true;
        }

        this.Close();           
    }
    catch (System.Exception ex)
    {
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

If what I said is confusing post your thread methods and I will show you what I mean.
I have posted the code the thread methods above just below load and ok code.
If you need help setting up the async task or a timeout let me know.
i'll try what you said i think for timeout i need to set some value for wait timeout but how to set up the async task??
Thanks now the code is more stable and it didnt hang also till now.
0

"Attempting to read or write protected memory" usually means you have a garbage pointer or pointer arithmetic that is way out of bounds. A garbage pointer can also cause app hangs or crashes in hard to reproduce ways.

To answer your threading question, the simplest solution IMO is to use .NET 4.0 Tasks instead of threads. Tasks can be constructed with a cancellation token. Your main thread can use that cancellation token to signal the task threads that it's time to close up shop and go home.

5 Comments

we have made the Ui in .net framework 3.5.Is it possible to use the 4.0 features.
I recall an "attempt to read write protected memory" exception in a function that had more than 128k used by local variables on the stack. Accessing stack memory more than 64k beyond the last stack address accessed would fail with a page fault. Only the one page beyond the top of the stack is set to allocate on reference. The solution was to change our compiler codegen to "touch" each page in the stack allocated variables in sequence to force all of the needed stack pages to be allocated before the body of the function could touch anything. I don't recall if this was .net or x86 though.
Sorry, the TPL is only available in .NET 4.0
So does it mean the Ok_btn click is using more than 128k.
if any one of your large complex calculation functions declares a lot of local variables, this anecdote might be applicable. The test case for the example above was from machine-generated code. Pages upon pages of local vars all in one function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.