C# System.ArgumentException: 'Object of type 'System.String' cannot be converted to type 'System.String[]'.' when used Dispatcher

Code Wanderer 396 Reputation points
2020-10-02T09:34:23.677+00:00

I got this error System.ArgumentException: 'Object of type 'System.String' cannot be converted to type 'System.String[]'.' when I use this code

    public void mf_ShowCheckUpdateInfoT(int v, string[] info = null)
    {
        this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal
            , new delegates.d_vCheckUpdateOnStartup(mf_ShowCheckUpdateInfo), v, info); // error is thrown when info contain text
    }

btw. the info is from C++/CLI code, but when I create new string in this function and copy a text, same error is thrown. From debugger the passed info from C++/CLI is ok and readable, error is thrown when Dispatcher try BeginInvoke.


Update:

here is code from C++/CLI

ShowCheckUpdateInfo(int v, std::wstring info[], int infoSize) {
array<String ^> ^cliInfo = nullptr;
if (info != nullptr && infoSize > 0)
{
    cliInfo = gcnew array<String ^>(infoSize);
    for (int i = 0; i < infoSize; i++)
    {
        cliInfo[i] = gcnew String(info[i].c_str());
    }
}
g->gui->c_Options->mf_ShowCheckUpdateInfoT(v, cliInfo); // WPF
}
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,755 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Andy ONeill 361 Reputation points
    2020-10-02T10:31:10.18+00:00

    You have a parameter which is

    string[]

    An array of strings.

    You are passing a string.

    These are different types.

    You should pass an array of string, even if it's only one string and the array is length of 1.

    Looks like you have an explicit delegate you're using there.

    I don't know exactly what you're doing but I usually find capturing variables in a closure is a convenient way to work for this sort of thing.
    I also recommend invokeasync over begininvoke.

    Dispatcher.InvokeAsync((Action)(() =>
    {
    // Your code to do things on the ui thread goes here.
    }));

    You can pass in a parameter to that anonymous action

    Dispatcher.InvokeAsync((Action)((param) =>
    {
    something = param;
    }));

    And or capture variables. So long as these are thread safe such as freezables.

    foo = whatever;
    Dispatcher.InvokeAsync((Action)(() =>
    {
    something = foo;
    }));


  2. Viorel 116.6K Reputation points
    2020-10-06T12:46:20.287+00:00

    Try this alternative:

    Dispatcher.BeginInvoke( new delegates.d_vCheckUpdateOnStartup( mf_ShowCheckUpdateInfo ), DispatcherPriority.Normal, v, info );
    

    Or show more details.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.