I have considerable programming knowledge but this is the first time I'm working on a multi-threaded application on C#, so I'm asking for help regarding my problem.
Firstly, the codes,
public frmCEX()
{
InitializeComponent();
refreshTicker();
}
private void btnRefresh_Click(object sender, EventArgs e)
{
refreshTicker();
}
private void refreshTicker()
{
ssStatus.Text = "Updating ticker..";
btnRefresh.Text = "Updating";
btnRefresh.Enabled = false;
ssUpdated.Text = "Last updated: -";
APIManager apim = new APIManager();
Ticker tk = apim.getTicker();
//blablabla, do some work
ssUpdated.Text = "Last updated: " + DateTime.Now.ToString();
ssStatus.Text = "";
btnRefresh.Text = "Refresh";
btnRefresh.Enabled = true;
}
private void cbxRefresh_CheckedChanged(object sender, EventArgs e)
{
if (cbxRefresh.Checked)
{
Thread thread1 = new Thread(() => BGRefreshThread(Convert.ToInt32(nupRefreshSecs.Value)));
thread1.Start();
}
else
{
// IF REFRESH CHECKBOX IS UNCHECKED, STOP THE THREAD THAT IS REFRESHING
}
}
private void BGRefreshThread(int delay)
{
refreshTicker();
System.Threading.Thread.Sleep(delay * 1000);
}
My main problem is in the cbxRefresh_CheckedChanged
method, basically, how this works is that when the user check the "Auto refresh" checkbox in the main UI, the checkedchanged method will create a new thread BGRefreshThread
that runs in the background and refresh the ticker, and once the checkbox is unchecked again, it will end the thread that refreshes the ticker.
However, I am having problem ending the thread once it is started, since once the checkedchanged
method ends, the thread no longer exists in the context when the checkbox is unchecked the next time.
Can anybody advice how I can get this working? Really new to multi-threading programming.
**EDIT: I've found a solution for this problem, but right now, when the newly created thread tries to call "refreshTicker" which updates labels and buttons on the main UI (which is on the main thread), it gives me this error:
Cross-thread operation not valid: Control 'btnRefresh' accessed from a thread other than >the thread it was created on.
any advice on this?**
You need to save the thread in a variable that does not go out of scope.
private Thread thread1;
private void cbxRefresh_CheckedChanged(object sender, EventArgs e)
{
if (cbxRefresh.Checked)
{
thread1 = new Thread(() => BGRefreshThread(Convert.ToInt32(nupRefreshSecs.Value)));
thread1.Start();
}
else
{
thread1.Abort();
}
}
Please note that there are some checks missing and abort is not a nice way to end a thread.