Results 1 to 7 of 7

Thread: Multithreadding

  1. #1
    Senior Member
    rikkimartin is offline

    Posts
    184

    Multithreadding

    i have a thread

    Thread demo = new Thread(Analyse);
    demo.Start();



    private Void Analyse();
    {

    messagebox.show("hi");

    timer1.Enabled = true;



    }

    why doesnt the timer enable? can i run a timer on its own threaad?
    Skype: blazikin6

  2. #2
    #Return
    Scarecrow is offline

    Posts
    3,443
    Code:
    Thread demo;
    
            private void Analyse() {
    
                //MessageBox.Show("HI");
                timer1.Enabled = true;
    
                MessageBox.Show("Timer1 is Enabled: " + timer1.Enabled.ToString());
            }
    
            private void button1_Click(object sender, EventArgs e) {
                demo = new Thread(new ThreadStart(Analyse));
                demo.Start();
            }

  3. #3
    Senior Member
    rikkimartin is offline

    Posts
    184
    None of that worked. ive rea aranged what i needed but now its calling

    LinearSmoothMove(pos, new TimeSpan(0, 0, 0, 0, tbSpeed.Value));

    and it cant access tbSpeed.value.

    read up a bit on this involke thing but cant seem to figure out how to call on tbSpeed.Value from another thread
    Skype: blazikin6

  4. #4
    #Return
    Scarecrow is offline

    Posts
    3,443
    Quote Originally Posted by rikkimartin View Post
    None of that worked. ive rea aranged what i needed but now its calling

    LinearSmoothMove(pos, new TimeSpan(0, 0, 0, 0, tbSpeed.Value));

    and it cant access tbSpeed.value.

    read up a bit on this involke thing but cant seem to figure out how to call on tbSpeed.Value from another thread
    Errr.. Worked fine for me... Perhaps a bit more context on what you're trying to do may help. ^^ That was all new information for me.

  5. #5
    Senior Member
    rikkimartin is offline

    Posts
    184
    what ive done now is this
    i was searching for multiply bitmaps within an area on 1 thread. taking some time 4 bitmaps taking 0.04 s .

    eg

    Bitmap eg = bmpScreen();


    bool pic1 = findbitmap() blah
    bool pic2 = findbitmap() blah
    bool pic3 = findbitmap() blah
    bool pic4 = findbitmap() blah

    if (pic1)
    {
    }

    if (pic2)
    {
    {
    etc.

    I changed them into tasks like so

    :


    Task<bool> taskSuccessPowder = Task.Factory.StartNew<bool>(TaskCannon4Powder);
    bool successPowder = taskSuccessPowder.Result;

    then under each task

    private bool TaskCannon4Powder()

    {
    Bitmap eg = bmpScreen();

    bool pic1 = findBitmap(blah)

    return pic1

    }


    so originally was trying to find each bitmap on one function, now put it into multiple tasks.

    however it takes 0.182 s to complete using task than it did previous.

    any idea why?

    Not sure if i should change the tasks to threads usng Thread eg = new Thread(TaskCannon4Powder);

    pretty much trying to make it more resource friendly.

    It is a gunning bot, which is why it needs to be faster, it is searhc for bitmaps in a small area before the cannon. changes the arrows when the peice it needs is detected
    Skype: blazikin6

  6. #6
    Server Admin
    Sticky is offline

    Posts
    966
    The actual answer is that .NET Controls aren't "thread-safe". That means you can't alter the value of a control (TextField, CheckBox, Timer) directly from a second thread.

    However, you can still alter them via a different method. See the answer here: c# - Thread Control.Invoke - Stack Overflow

  7. #7
    Senior Member
    rikkimartin is offline

    Posts
    184
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;


    namespace Multithread
    {



    public partial class Form1 : Form
    {

    public delegate void ProcessResultDelegate(string result);
    public void ProcessResult(string result)
    {
    if (textBox1.InvokeRequired)
    {
    var d = new ProcessResultDelegate(ProcessResult);
    d.Invoke(result);
    }
    else
    {
    textBox1.Text = result;
    }
    }


    private static void Invoke(this Control control, MethodInvoker action)
    {
    control.Invoke(action);
    }




    public Form1()
    {
    InitializeComponent();
    }


    private void Form1_Load(object sender, EventArgs e)
    {

    }

    Thread newT;

    private void newThread()
    {

    //textBox1.Invoke(textBox1.Text);
    ProcessResult("Hi there");





    }




    private void button1_Click(object sender, EventArgs e)
    {
    Thread newT = new Thread(new ThreadStart(newThread));
    newT.Start();
    }
    }
    }


    im getting an error on Form1

    Extention method must be defined as a non generic method
    Skype: blazikin6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •