0

I have got a program which is controlling a video capturing card. And this card has got several inputs and my task is to write a method which starts capturing from all inputs. Unfortunately in SDK I can only call the method which handles a click event for one button. I've got an idea to write a few threads which simultaneously call this method for each button which is presented by specified input.

Below this is my thread. I call and give a reference of the button which is selected to start.

    private class StartThread
    {
        private static DateTime startTime; //arbitrary start time    

        private Button btnStart;

        public static void initializeTimer()
        {
            startTime = DateTime.Now.AddSeconds(5); //arbitrary start time
        }
        public StartThread(Button btnTmp)
        {
            this.btnStart = btnTmp;
        }
        public delegate void DelegateStandardPattern();
        public void doWork()
        {
            while (DateTime.Now < startTime) ;

            btnStart.PerformClick();
        }



    };

There I check if the specified start button should start and create the thread:

private void btnStartAllClick(object sender, EventArgs e)
        {
            int i = 0, j = 0;
            List<Thread> listThreads = new List<Thread>();
            for (i = 0; i < miInstalledCardNum; i++)
            {
                for (j = 0; j < mCard[i].iDeviceNum; j++)
                {
                    if (mCard[i].Device[j].ChkBoxStartAll.Checked == true &&
                        mCard[i].Device[j].ChkBoxStartAll.Enabled == true)
                    {
                        StartThread tmpThread = new StartThread(mCard[i].Device[j].BtnStart);
                        listThreads.Add(new Thread(tmpThread.doWork));
                    }
                }
            }

            if(listThreads.Count > 0){
                StartThread.initializeTimer();
                foreach(Thread currentThread in listThreads)
                    currentThread.Start();
                foreach (Thread currentThread in listThreads)
                    currentThread.Join();

            }

         }

Unfortunately this operation returns an exception:

Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.

I tried witch InvokeRequired:

  if (this.InvokeRequired)
        {
            this.Invoke(
                new MethodInvoker(
                delegate() { ThreadSafeFunction(intVal, boolVal); }));
        }
        else
        {
            //use intval and boolval
        }

But I get another error:

does not contain a definition for 'Invoke' and no extension method 'Invoke' accepting a first argument of type 'fmb_player_apl.MasterForm.StartThread' could be found (are you missing a using directive or an assembly reference?)

I must start four button parallel, because it causes asynchronous video.

Kristof U.
  • 1,263
  • 10
  • 17
KonradPrg
  • 29
  • 7
  • What does the click event handler for btnStart do? Can you pull that code out into a separate method and call it? – Chris Dunaway Aug 25 '14 at 15:20
  • Yes, I can call the method which is inside event handler, but the same error show up me further. That method has involvement with UI, e.g. change state of button or start another window. So I am stuck in the same point. Has another solution? In the worst case I start call buttons sequentially and in another program I try synchronize my videos, but unfortunately it creates me another problem, which I'd like to escape. – KonradPrg Aug 26 '14 at 07:33

1 Answers1

0

Unfortunately in SDK I can only call the method which handles a click event for one button.

That sound incredibly fishy. You have an SDK that controls a UI? That's horrible. That's not what an SDK should be. Get another provider of whatever you want to do.

Doing things in the UI in parallel is simply not possible. Even if you do it in parallel (which will not work as you figured out) there is no windows technology that would allow them to build a UI to allow you to do that in parallel. All UI works by having a UI thread working on a queue of events... handling them one after another.

Would just calling all of them in sequence without something in between be fast enough for you? Because that's the best thing you can do.

nvoigt
  • 75,013
  • 26
  • 93
  • 142