none
Error occur when load form RRS feed

  • Question

  • From the timer, call a static method which load a form like following.

    if (chattingsession == null)//Chatting is not opened yet
    {
        chattingsession = new ChattingSession(incomingphoneno, first_name, last_name);
        newform = true;
    }
    
    chattingsession.Name = incomingphoneno;
    chattingsession.MessageList = messagelist;
    chattingsession.MessageList.Add(msg);
    chattingsessionlist.Add(chattingsession);
    if (newform)
    {
        chattingsession.Show();
    }

    howevr error occurs like following. Can anybody give me some advice?

    ************** Exception Text ************** System.InvalidOperationException: DragDrop registration did not succeed. ---> System.Threading.ThreadStateException: Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. at System.Windows.Forms.Control.SetAcceptDrops(Boolean accept) --- End of inner exception stack trace --- at System.Windows.Forms.Control.SetAcceptDrops(Boolean accept) at System.Windows.Forms.Control.OnHandleCreated(EventArgs e) at System.Windows.Forms.TextBoxBase.OnHandleCreated(EventArgs e) at System.Windows.Forms.TextBox.OnHandleCreated(EventArgs e) at System.Windows.Forms.Control.WmCreate(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.TextBoxBase.WndProc(Message& m) at System.Windows.Forms.TextBox.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

    Monday, June 24, 2019 4:42 AM

Answers

All replies

  • Maybe use System.Windows.Forms.Timer instead of other kinds of timers.

    Monday, June 24, 2019 5:43 AM
  • Hello,

    Read the information in the following post.

    https://stackoverflow.com/questions/135803/dragdrop-registration-did-not-succeed


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Jeff0803 Tuesday, June 25, 2019 6:10 PM
    Monday, June 24, 2019 2:35 PM
    Moderator
  • Hi Jeff0803,

    Thank you for posting here.

    I noted that the code you provided is not complete. For example, could you tell me what ChattingSession is. Based on my search, I could not find it.

    Therefore, I hope that you could provide the missing code, which will help us to solve the problem.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, June 25, 2019 1:40 AM
    Moderator
  • The ChattingSession is inherited from Form like following and exist in the separate dll.

    public partial class ChattingSession : Form

    Tuesday, June 25, 2019 2:28 AM
  • Hello,

    Thanks for the feedback.

    I hope that you could provide a complete code.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Jeff0803 Tuesday, June 25, 2019 6:10 PM
    • Unmarked as answer by Jeff0803 Tuesday, June 25, 2019 6:10 PM
    Tuesday, June 25, 2019 2:32 AM
    Moderator
  • I'm sorry that I can't provide whole source code. 

    However I can tell you rough picture of the structure.

    - FormA has a timer.

    - This timer calls method(method_A) which check incoming message. this method is in the A.DLL.(the code I posted first is this static method)

    - method_A is called like following way

       A.Instance.method_A()

       and A.Instance is like following.

              public static A Instance
              {
                  get
                  {
                      if (m_Common != null)
                     {
                           return m_Common;
                     }
                     else
                     {
                          m_Common = new A();
                          return m_Common;
                     }
                 }
             }

    - If new message is arrived from some cellphone, open new window, which is chattingSession class.

       chattingSession is inherited from Form and is in the B.DLL.

    In my opinion, an owner object should be passed to the chattingSession.Show(). but I don't know how to detect owner object.



    • Edited by Jeff0803 Tuesday, June 25, 2019 12:39 PM
    Tuesday, June 25, 2019 4:15 AM
  • Karen gave you the right answer, which was to follow the very clear instructions in the original error message.  Add the [StaThread] attribute to your Main function.  Have you done that?  If not, then I don't know what magic you're expecting.

    Who would you expect chattingSession to be owned by?


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Tuesday, June 25, 2019 5:56 PM
  • Thanks for answer.

    In the post you recommended, there is a comment like following.

       Look at the Threads window in Visual Studio (Debug | Windows | Threads) when the exception occurs  and see i you are on a thread other than the main thread.

    I can't find this menu.

    Could you let me know in detail how to do that from Visual Studio?

    Tuesday, June 25, 2019 6:17 PM
  • I used System.Timer.

    Will try System.Windows.Forms.Timer.

    Thanks!

    Wednesday, June 26, 2019 5:44 AM