Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting a Cross Thread excpetion here as i am trying to access the control other than the thread it has been created. I tried achieving this using System.ComponentModel.BackgroundWorker class. Beacuse some internal issue in our app, i cannot use this. Please help

private System.Timers.Timer m_SystemTimer;
private Form m_ProgressBox;

public MyClass()
{
InitializeComponent();

m_SystemTimer = new System.Timers.Timer();
m_SystemTimer.Interval = 3000;
m_SystemTimer.AutoReset = false;

m_SystemTimer.Elapsed += new System.Timers.ElapsedEventHandler(m_SystemTimer_Elapsed);
}

private void m_SystemTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (m_ProgressBox == null)
{
m_ProgressBox = new Form();
m_ProgressBox.Show();
}
}

private void MyEventHandler1(object sender, EventArgs e)
{
m_SystemTimer.Start();
}


private voMyEventHandler2(object sender, EventArgs e)
{
m_ProgressBox.Close(); // throws a cros thread exception here
}
Posted
Comments
[no name] 2-Apr-14 8:54am    
Hi.Thanks for the response. I want my timer tick to happen only once thats why i have set m_SystemTimer.AutoReset = false;
In this case Your solution will not be helpful.In my case, i dont have any Form, to call BeginInvoke on the control.

1.You cannot access that form because was created in other thread.

2.You should design a new solution for intercommunicate between thread, for example to use a class member name closeProgress that will be set on true from your voMyEventHandler2 and will be checked in your m_SystemTimer_Elapsed and if is true and the form is shown to close the form and put the variable on false.
 
Share this answer
 
Since you don't show us the code where you add the handlers or signal the events I can only assume that you are raising your events from a different thread - in which case you cannot access UI controls without Invoking them.
C#
if (InvokeRequired)
    {
    Invoke(new MethodInvoker(delegate { m_ProgressBox.Close(); }));
    }
else
    {
    m_ProgressBox.Close();
    }
 
Share this answer
 
Comments
[no name] 2-Apr-14 8:59am    
Thanks for the response. I have just simulated my requirement through Button1 click for shoing Progress and after some time, i click Button2 to close progressBox. I n my actual scenarion i will only get 2 event handlers to do the above functionlaity.
On What control do i need to do Invoke the progressBox? (the solution you proposed).

Here is full program, but please note that i dont have access to Form in my real scenario.

public partial class Form1 : Form
{
private System.Timers.Timer m_SystemTimer;
private Form m_ProgressBox;

public Form1()
{
InitializeComponent();

m_SystemTimer = new System.Timers.Timer();
m_SystemTimer.Interval = 3000;
m_SystemTimer.AutoReset = false;

m_SystemTimer.Elapsed += new System.Timers.ElapsedEventHandler(m_SystemTimer_Elapsed);
}

private void m_SystemTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
// Timer starts after 3 seconds and happend only once.
if (m_ProgressBox == null)
{
m_ProgressBox = new Form();
m_ProgressBox.Show();
}
}

private void button1_Click(object sender, EventArgs e)
{
m_SystemTimer.Start();
}


private void button2_Click(object sender, EventArgs e)
{
m_ProgressBox.Close(); // throws a cros thread exception here
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900