Simple threading

Yet another question!

Page 1 of 1

8 Replies - 1106 Views - Last Post: 29 August 2008 - 02:59 PM Rate Topic: -----

#1 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Simple threading

Posted 29 August 2008 - 10:12 AM

OK, I've got another question. This time, it's about threading. I've followed a tutorial, but this just doesn't seem to work. It compiles, but my thread never actually does anything (and it should!)

	string currentClipboard = "";
		void monitorClipboard()
		{
			while (true)
				if (Clipboard.GetText() != currentClipboard)
				{
					currentClipboard = Clipboard.GetText();
					// clipboardRing is a ListBox
					clipboardRing.Items.Add(currentClipboard);
				}
		}
			
		public MainForm()
		{
			// The InitializeComponent() call is required for Windows Forms designer support.
			InitializeComponent();	
			updateTreeView();
			System.Threading.Thread thr = new System.Threading.Thread(new System.Threading.ThreadStart(this.monitorClipboard));
			thr.Start();
		}


Thanks in advance.
-Danny

Is This A Good Question/Topic? 0
  • +

Replies To: Simple threading

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Simple threading

Posted 29 August 2008 - 10:38 AM

the problem is that you are trying to update a UI component on a non-UI thread.
Was This Post Helpful? 0
  • +
  • -

#3 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: Simple threading

Posted 29 August 2008 - 10:41 AM

Which means...?
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,441
  • Joined: 23-August 08

Re: Simple threading

Posted 29 August 2008 - 11:04 AM

This is the best C# threading resource available for free on the net. Hope this helps!

And this is to what eclipsed4utoo was referring.
Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Simple threading

Posted 29 August 2008 - 11:20 AM

View Postgabehabe, on 29 Aug, 2008 - 01:41 PM, said:

Which means...?


you aren't allowed to do cross-threaded calls. The listbox is created on the UI thread. So it can't be updated directly from another thread. You will have to use a delegate.

here is what you need to do:

namespace ThreadingExample
{
    public partial class Form1 : Form
    {
        string currentClipboard = string.Empty;

        public delegate void UpdateTextCallback(string text);

        public Form1()
        {
            InitializeComponent();
        }

        void monitorClipboard()
        {
            do
            {
                clipboardRing.Invoke(new UpdateTextCallback(this.UpdateText), new object[] { "testing" });
                
            } while (true);
        }

        private void UpdateText(string text)
        {
            clipboardRing.Items.Add(text);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            System.Threading.Thread thr = new System.Threading.Thread(new System.Threading.ThreadStart(this.monitorClipboard));
            thr.Start();  
        }
    }
}



the "Invoke" method invokes action on the control using the thread that the control was created with.

now...the Clipboard.GetText() doesn't seem to work in this same fashion. if you replace "testing" with Clipboard.GetTest(), nothing happens. I don't exactly know why.

This post has been edited by eclipsed4utoo: 29 August 2008 - 11:21 AM

Was This Post Helpful? 1
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Simple threading

Posted 29 August 2008 - 11:27 AM

@gabehabe:

Here is a tutorial on Cross Thread Communication that may help :)
Was This Post Helpful? 0
  • +
  • -

#7 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: Simple threading

Posted 29 August 2008 - 01:55 PM

*some time later*

Thanks guys, I'm starting to get it.

I've gone with your method, eclipsed4utoo~
I'm having a problem though. When I try to call one of my other functions, the program freezes.

Which leads me to a new question:
Do I need to create a thread for my main program too?
So, effectively, I'll have two threads~ one for the clipboard monitoring, and one for the rest of the program?

Sorry for the dumbass questions, I suck with threads~
Was This Post Helpful? 0
  • +
  • -

#8 djkitt  Icon User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 189
  • Joined: 22-May 08

Re: Simple threading

Posted 29 August 2008 - 02:16 PM

So, I think Clipboard has to run in a single thread.

Anyway, you should take a look at the CPU usage of your program with task manager. You are executing that while loop like crazy. You could fix that with a Thread.Sleep() but you will still have the problem of Clipboard.GetText not working.

If you use the System.Windows.Forms.Timer control you will avoid both problems.

		// Add a Timer named timer1 to your form
		// enable it, 
		//set the interval to 50 or something,
		// and then add the following to the Tick event...

		private void timer1_Tick(object sender, EventArgs e)
		{
			if (Clipboard.GetText() != currentClipboard)
			{
				currentClipboard = System.Windows.Clipboard.GetText();
				// clipboardRing is a ListBox  
				clipboardRing.Items.Add(currentClipboard);
			}
		}




Hope this helps,

Kitt
Was This Post Helpful? 0
  • +
  • -

#9 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: Simple threading

Posted 29 August 2008 - 02:59 PM

What an absolute fucking muppet

I can't believe how stupid I am. I added a label to the tabs, so it was nothing to do with the threads~ it was all to do with me referencing the label as an RTB that made the program freeze.

BELL END!

Oh well, I fixed it now~

Thanks everyone :)

(In the end, I used eclipsed4utoo's method, and it worked like a charm)

Thanks again! :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1