I haven't done multi-threading before, so I need a bit of help with this.
I have a form with a button. When user clicks on a button for the first time, a recursive function is called. When a certain condition in the function is true, I want the function to wait where it's at for the next button click.
An example:
void ButtonClick(object sender, EventArgs e) { if (running) { //notify the thread (the recursive function) to continue
}
else functionCall(0); }
void functionCall(int num) {
if (num % 2 == 0)
{
displaySomething();
//wait
}
functionCall(num + 1); }
|
How would I do this?
Benjamin ScharbachPosted Oct 14, 2017, 2:42 PM
Sam HobbsPosted Sep 7, 2010, 12:14 PM
Normally the BackgroundWorker uses ReportProgress to update the form.
JurePosted Sep 7, 2010, 10:59 AM
I tried by creating a separate class that holds the data and creating an event (->displaying the data on the form) for that class. But the event still executes in the 2nd thread.
So, in short, rather than making the main thread wait (GUI-freeze), I want the 2nd thread to send the 1st thread to a function. OR, I want the 2nd thread to have control over the GUI (if that's even possible, I assume it's not).
I have been programming for only two months now (started with C++), so all this may be a bit too much for me. But I'm the type of person that learns better from his own projects rather than just by studying tutorials. Can you point me in the right direction?
Thanks in advance!
Sam HobbsPosted Sep 6, 2010, 3:14 PM
JurePosted Sep 6, 2010, 8:22 AM
An unhandled exception of type 'System.Threading.SynchronizationLockException' occurred in mscorlib.dll
Additional information: Object synchronization method was called from an unsynchronized block of code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace C_Sharp_Multi_Threading_with_Form
{
public partial class Window : Form
{
bool running = false;
public SecondThread SecThread;
public Recursion recursion;
public Window()
{
InitializeComponent();
this.button.Click += new EventHandler(ButtonClick);
}
void ButtonClick(object sender, EventArgs e)
{
if (running)
{
SecThread.wait();
}
else
{
SecThread = new SecondThread();
running = true;
SecThread.wait();
}
}
}
public class SecondThread : Window
{
Thread thread;
public SecondThread()
{
thread = new Thread(new ThreadStart(run));
thread.Start();
}
void run()
{
recursion = new Recursion();
}
public void wait()
{
Monitor.Pulse(this);
Monitor.Wait(recursion);
label.Text = recursion.data.ToString();
}
}
public class Recursion : Window
{
public int data;
public Recursion()
{
function(0);
}
void function(int num)
{
if (num % 4 == 2)
{
data = num;
Monitor.Pulse(this);
Monitor.Wait(SecThread);
}
function(num + 1);
}
}
}
I know I'm doing something laughably-obvious completely wrong, because I don't have any understanding about multi-threading. I hope it's at least a little bit clearer what I'm trying to accomplish.
JurePosted Sep 6, 2010, 5:07 AM
Thanks in advance!
ShankeyPosted Sep 6, 2010, 4:50 AM
You have to write some thing like below :