On 15 Mar, 11:22, "DeveloperX" <nntp...@operamail.comwrote:
On 15 Mar, 10:55, Peted wrote:
Hi
wondering what is the best way to do this
Need a user to click a button, that sends 3 or 4 string based commands
via a TCP/ip socket link
I can connect to the ip device no problems, am using indy sockets, and
can send information ok
but i need a good way to send 3 or 4 consecutive commands/data to the
ip device with variable (200ms to 1000ms) dellay between the commands,
from 1 button click
The number of commands to be sent can vary and the delay time between
the commands can vary depnding on the needs at the time
EG to IP device
command 1
pause 200ms
command 2
pause 250ms
coomand 3
pause 300ms
command 4
pause 200ms
and i need to be able to have the gui not lock up or freeze while
waiting for the routine to finish sending.
Can anyone point me to atechnique or algorithm to do the pausing
without gui lock
thanks
Actually, just reread your post and what I've done isn't quite what
you specified. Give me two minutes and I'll rewrite it :)- Hide quoted text -
- Show quoted text -
Ok, this will Queue the commands and execute them serially. I've
modified the test case so the main thread will pause long enough for
the queue to empty out, then I add another command. The output looks
like this:
In main thread, sleeping for 200ms
Sending Command 1 then sleeping for 500ms
In main thread, woken after 200ms
In main thread, sleeping for 500ms
Slept for 500 moving to next command
Sending Command 2 then sleeping for 1000ms
In main thread, woken after 500ms
In main thread, sleeping for 900ms
Slept for 1000 moving to next command
Sending Command 3 then sleeping for 700ms
In main thread, woken after 900ms
In main thread, sleeping for 800ms
Slept for 700 moving to next command
No more commands
In main thread, woken after 800ms
Sending Command 4 then sleeping for 400ms
Slept for 400 moving to next command
No more commands
Here's the code
using System;
using System.Threading;
using System.Collections;
namespace ConsoleApplication2
{
class Demo
{
[STAThread]
static void Main(string[] args)
{
ThreadLauncher.ThreadBasics("Command 1",500);
ThreadLauncher.ThreadBasics("Command 2",1000);
ThreadLauncher.ThreadBasics("Command 3",700);
Console.WriteLine("In main thread, sleeping for 200ms");
Thread.Sleep(100);
Console.WriteLine("In main thread, woken after 200ms");
Console.WriteLine("In main thread, sleeping for 500ms");
Thread.Sleep(500);
Console.WriteLine("In main thread, woken after 500ms");
Console.WriteLine("In main thread, sleeping for 900ms");
Thread.Sleep(900);
Console.WriteLine("In main thread, woken after 900ms");
Console.WriteLine("In main thread, sleeping for 800ms");
Thread.Sleep(800);
Console.WriteLine("In main thread, woken after 800ms");
ThreadLauncher.ThreadBasics("Command 4",400);
Console.ReadLine();
}
}
public class ThreadLauncher
{
private static ThreadClass _tc = new ThreadClass();
private ThreadLauncher()
{}
public static void ThreadBasics(string pCommandString, int pWait)
{
_tc.Add(pCommandString, pWait);
}
private class ThreadClass
{
private Queue _queue;
private Thread _myThread;
private object _lockMe = new object();
public ThreadClass()
{
_queue = new Queue();
}
public void Add(string pCommandString, int pWait)
{
lock(_lockMe)
{
_queue.Enqueue(new TheCommand(pCommandString,pWait));
if (_myThread == null)
{
ThreadStart myThreadStart = new ThreadStart(ThreadMethod);
_myThread = new Thread(myThreadStart);
_myThread.Start();
}
}
}
private void ThreadMethod()
{
TheCommand tc;
lock(_lockMe)
{
while(_queue.Count 0)
{
tc = (TheCommand)_queue.Dequeue();
Console.WriteLine("Sending {0} then sleeping for
{1}ms",tc.CommandString, tc.WaitForNext);
Thread.Sleep(tc.WaitForNext);
Console.WriteLine("Slept for {0} moving to next
command",tc.WaitForNext);
}
Console.WriteLine("No more commands");
_myThread = null;
}
}
}
private struct TheCommand
{
public TheCommand(string pCommandString, int pWait)
{
CommandString = pCommandString;
WaitForNext = pWait;
}
public string CommandString;
public int WaitForNext;
}
}
}