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:
C# |
How would I do this?


ShankeyRank #972 · 2.1K reputationPosted Sep 6, 2010, 12:50 AM EDT
You have to write some thing like below :
using System; using System.Threading; // Following is the member of MyThread class public void recursion(String btnText) { Boolean nextclickflag = (btnText.Equals("Next")?true:false); if(yourcondition == true && (! btn.Text.Equals("Next")))Thread.Sleep(5000); } } class MultiThread { public static void Main() { // First, construct a MyThread object. MyThread myThread = new MyThread(); //Next, construct a thread from that object. Thread newThread = new Thread(new ThreadStart(myThread.recursion(btn.Text)); //Finally, start execution of the thread. newThread.Start(); } }JureRank #2,128 · 670 reputationPosted Sep 6, 2010, 1:07 AM EDT
Thanks in advance!
JureRank #2,128 · 670 reputationPosted Sep 6, 2010, 4:22 AM EDT
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.
Sam HobbsRank #107 · 20.5K reputationPosted Sep 6, 2010, 11:14 AM EDT
JureRank #2,128 · 670 reputationPosted Sep 7, 2010, 6:59 AM EDT
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 HobbsRank #107 · 20.5K reputationPosted Sep 7, 2010, 8:14 AM EDT
Normally the BackgroundWorker uses ReportProgress to update the form.
Benjamin Scharbach20 reputationPosted Oct 14, 2017, 10:42 AM EDT