'Faster way to type whole strings instead of single chars in another window

I have some code where I need to type in in another Window(VFP) some strings. I already have implemented an foreach to type in the chars but it seems to take too long for everything to be processed.

IntPtr hw = win32.FindWindow(null, deffensterTitel);
foreach (char c in stringToBeSent.ToCharArray())
{
    object o = new System.Windows.Forms.KeysConverter().ConvertFromString(c.ToString());
    System.Windows.Forms.Keys key = (System.Windows.Forms.Keys)o;
    System.Threading.Thread.Sleep(10);
    win32.SendKey(key, hw, false);
}

I could also write everything in a file to be read later on but it creates a huge risk for the other software by doing so.

Is there any other way to make it process faster or any alternative way?



Solution 1:[1]

The most efficient way for my use case to copy the whole string into the clipboard and paste it right on the focused form was like the following codesnippet:

var t = new Thread((ThreadStart)(() =>
{
    Clipboard.SetText(code.Trim());
}));
    
t.SetApartmentState(ApartmentState.STA);
t.Start();
t.Join();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zelforacle