0
bool hasCopiedArtikelnum = false;

while (hasCopiedArtikelnum == false)
{
    try
    {
        artikelnum = Clipboard.GetText();
        hasCopiedArtikelnum = true;
    }
    catch {}
}

I want the program to keep looping until he has successfully copied. Does putting other things in the "try" work? Or will the programm also >>try<< to set hasCopiedArtikelnum to true?

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
AzulShiva
  • 201
  • 2
  • 17
  • 7
    XY problem. The actual solution is `while (!Clipboard.ContainsText(TextDataFormat.Text))`. If you want help fixing this code, explain what you expect this to do and what it actually does. See also [Clipboard event C#](http://stackoverflow.com/questions/621577/clipboard-event-c-sharp). – CodeCaster Nov 27 '15 at 11:50
  • 1
    Exceptions aren't good for manage usual situations. Better aproach is check for value. In your case empty clipboard is usual situation, not error. – BWA Nov 27 '15 at 11:55
  • 2
    This seems to be [busy wait](https://en.wikipedia.org/wiki/Busy_waiting) and should be avoided, as it will severely waste system resources. – Codor Nov 27 '15 at 11:56
  • It does nothing else but copying a number that the end-user will need to note. So the number will show up on different labels of the Program. If the number has not been copied, the end-user will not know what to do. – AzulShiva Nov 27 '15 at 11:56
  • 2
    I suggest limiting the number of tries to a fixed number like 100 tries, 1000 tries or something, so that it will terminate at some point with a message that explains that it did not work after xyz tries. Just use an int instead of a boolean and check for int < 100 and increment it at the end of your try-block. – Paul Weiland Nov 27 '15 at 12:00

3 Answers3

5

It seems what you really want to do is wait until the user has copied something to the clipboard and then check whether there is text contained in the clipboard using Clipboard.ContainsText (as already suggested by @CodeCaster and @PatrickHofman).

You can receive clipboard events as described in an answer to this question: Clipboard event C#.

Such an approach is much better because you a) won't be using exceptions for control flow and b) reduce CPU load significantly by avoiding to permanent polling of the clipboard.

Community
  • 1
  • 1
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
  • I think I was not clear enough of my intentions. The programm should automatically select what to copy as: MouseClickL(489, 403); Send("^a"); Send("^c"); and than perform copying. PS: how do you get grey text code format for comments? – AzulShiva Nov 27 '15 at 13:41
3

The while loop while continue to execute until hasCopiedArtikelnum is true.

This means you can put anything into the try block and it will keep iterating until everything completes, at long as hasCopiedArtikelnum = true is at the end.

If Clipboard.GetText() throws an exception when it's not ready to complete, the execution will jump hasCopiedArtikelnum and it will remain false

Steve
  • 9,335
  • 10
  • 49
  • 81
2

Yes, you can catch all exceptions using catch (Exception) {} and it will continue. I advice to use a maximum number of tried though, to prevent this action from hanging.

I guess you are solving a problem with retrieving text. Maybe there are other ways to do so, maybe you can sleep for X milliseconds, use a timer to do it once a X seconds for example, etc. I guess there is another solution you are looking for. This code will hang your process until it succeeds retrieving the text. Are you looking for some event maybe?

Also it is better not to rely on an exception if you can, possibly while (!Clipboard.ContainsText(TextDataFormat.Text)), as CodeCaster suggested it a better solution.

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325