Results 1 to 5 of 5

Thread: RaiseEvent in multithreading

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    70

    RaiseEvent in multithreading

    I am using RaiseEvent in a thread and I pass a value to it and try to set a control's text to the value. Except I get a cross-thread error which is expected. I could invoke the control, but I don't want to have to do that.

    My question is, is there a way to invoke/delegate the RaiseEvent itself so it will work on the ui thread without doing anything else?

  2. #2
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: RaiseEvent in multithreading

    Why don't you want to invoke the control?
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2007
    Posts
    70

    Re: RaiseEvent in multithreading

    I could, it's more of is there another way to do it?

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: RaiseEvent in multithreading

    It seems that you are raising the event on a thread other than the UI thread. If so invoke/delegate seems to be the answer.

    http://msdn.microsoft.com/en-us/magazine/cc188732.aspx
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: RaiseEvent in multithreading

    There are two ways to marshal a method call to another thread: the ISynchronizeInvoke interface and the SynchronizingContext class.

    Usually we use the InvokeRequired property and Invoke method of a form or control. That's the ISynchronizeInvoke interface. If you want to do that then the object raising the event has to have a reference to a control that was created on the UI thread. Consider the SynchronizingObject property of the Timers.Timers and FileSystemWatcher classes. That's exactly what that is.

    If you want to do this without a direct reference to a control then you can use the SynchronizingContext class. For an example, follow the Asynchronous link in my signature.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width