-2
internal static class RtfMerger
    {
        static object lockRtf = new object();
        static RichTextBox richTextBox1 = new RichTextBox();

        static TaskCompletionSource<string> source = new TaskCompletionSource<string>();
        static Thread thread = new Thread(() =>
        {
             //SOME code...
            // HOW TO THERE ACCESS TO THE PARAMETER input ?

        });

        public static Task<string> MergeRtf(params string[] input)
        {
            lock (lockRtf)
            {                               
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();  // <- HERE I CAN PASS input
                return source.Task;
            }
        }

Hello, how to modify declaration of thread, that it can receive parameter string[] input?

and there is full class with associated methods. hope it helps because I've been worried about it for a long time and I don't know what to do because I've been worried about it for a long time and I don't know what to do

{
    internal static class RtfMerger
    {
        static object lockRtf = new object();
        static RichTextBox richTextBox1 = new RichTextBox();

        static TaskCompletionSource<string> source = new TaskCompletionSource<string>();
        static Thread thread = new Thread(() =>
        {
            try
            {
                //var richTextBox1 = new RichTextBox(); //TODO static na úrovni třídy [LOCK] //TODO async
                if (input != null && input.Length > 0)
                {
                    if (input[0].StartsWith(@"{\rtf"))
                    {
                        richTextBox1.Rtf = input[0];
                    }
                    else
                    {
                        richTextBox1.Rtf = ConvertPlainTextToRTF(input[0], "Arial");
                    }
                    richTextBox1.AppendText("\r\n");
                    for (int i = 1; i < input.Length; i++)
                    {
                        richTextBox1.SelectionStart = richTextBox1.TextLength;
                        richTextBox1.SelectionLength = 0;

                        if (input[i].StartsWith(@"{\rtf"))
                        {
                            richTextBox1.SelectedRtf = input[i];
                        }
                        else
                        {
                            richTextBox1.SelectedRtf = ConvertPlainTextToRTF(input[i], "Arial");
                        }
                    }
                }
                source.SetResult(richTextBox1.Rtf);
            }
            catch (Exception ex)
            {
                source.SetException(ex);
            }

        });

        public static Task<string> MergeRtf(params string[] input)
        {
            lock (lockRtf)
            {               
                //TODO přidat synchronizační objekt (může být 
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start(); //Přidat zámek k spuštění threadu
                return source.Task;
            }
        }


    }
}
SonDy
  • 49
  • 5

1 Answers1

0

You can certainly do this:

internal static class RtfMerger
{
    static object lockRtf = new object();

    static TaskCompletionSource<string> source = new TaskCompletionSource<string>();

    public static Task<string> MergeRtf(params string[] input)
    {
        lock (lockRtf)
        {
            Thread thread = new Thread(() =>
            {
                //SOME code...

                // You can access `input` here.
            });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            return source.Task;
        }
    }
}
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • I cant work with static RichTextBox richTextBox1 = new RichTextBox(); getting exception – SonDy Apr 01 '20 at 06:19
  • yes, i need it declared as static in class – SonDy Apr 01 '20 at 06:20
  • @SonDy - Sure, don't use it then. It wasn't needed in the code you provided and you didn't mention it in the question so I left it out. – Enigmativity Apr 01 '20 at 06:20
  • 1
    @SonDy - And, even if you did use a `RichTextBox`, you can't access or update it in a thread other than the one used to create it. – Enigmativity Apr 01 '20 at 06:22
  • @SonDy - You'd need to let us know what `//SOME code...` is trying to do. – Enigmativity Apr 01 '20 at 06:22
  • if i declare it in class, it is count as different thread ? – SonDy Apr 01 '20 at 06:24
  • @SonDy - Yes, the thread that creates the `RichTextBox` is different from the `Thread` you're creating. You can't access or update the `RichTextBox` from the created thread. Usually there's easy ways to get around that issue but we don't know what because you haven't told us what `SOME code...` is doing. – Enigmativity Apr 01 '20 at 06:30
  • @SonDy - I saw your attempted edit. You can't do any of that in the created `Thread`. I rejected your edit. Please add the code to your question at the end (don't edit your existing code) as an edit. – Enigmativity Apr 01 '20 at 06:33
  • "Some code" using richTextBox1.Rtf, richTextBox1.AppendText, richTextBox1.SelectionStart, richTextBox1.SelectionLength, richTextBox1.SelectedRtf and setting result as source.SetResult(richTextBox1.Rtf); – SonDy Apr 01 '20 at 06:33
  • I'm trying to minimize memory leaks which RichTextBox doing, thats why it is that complicated – SonDy Apr 01 '20 at 06:44
  • @SonDy - Using a thread does NOT fix memory leaks. Properly disposing of `IDisposable` objects is the primary way of eliminating memory leaks in managed code. – Enigmativity Apr 01 '20 at 07:26
  • @SonDy - If you do have memory leaks caused by bugs in libraries that you don't control then the ONLY way to fix them is to run up a separate process, run your code there, and then kill the process. – Enigmativity Apr 01 '20 at 07:27
  • how to run separate process in my example? – SonDy Apr 01 '20 at 08:49
  • @SonDy - Look up `Process.Start`. – Enigmativity Apr 01 '20 at 10:59
  • ok by running Process.Start() I will create new process, but how to kill them and how to run it again? – SonDy Apr 01 '20 at 11:21
  • @SonDy - `var process = Process.Start("My.EXE"); process.Kill();`. – Enigmativity Apr 01 '20 at 11:43